View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.lm.workflow;
17  
18  import org.apache.log4j.Logger;
19  import org.joda.time.LocalDate;
20  import org.kuali.kpme.core.api.assignment.Assignment;
21  import org.kuali.kpme.core.api.namespace.KPMENamespace;
22  import org.kuali.kpme.core.api.workarea.WorkArea;
23  import org.kuali.kpme.core.assignment.AssignmentBo;
24  import org.kuali.kpme.core.assignment.AssignmentBo;
25  import org.kuali.kpme.core.role.KPMERole;
26  import org.kuali.kpme.core.service.HrServiceLocator;
27  import org.kuali.kpme.tklm.api.common.TkConstants;
28  import org.kuali.kpme.tklm.leave.transfer.BalanceTransfer;
29  import org.kuali.rice.kew.api.exception.WorkflowException;
30  import org.kuali.rice.kew.api.identity.Id;
31  import org.kuali.rice.kew.api.identity.PrincipalId;
32  import org.kuali.rice.kew.api.rule.RoleName;
33  import org.kuali.rice.kew.engine.RouteContext;
34  import org.kuali.rice.kew.routeheader.DocumentContent;
35  import org.kuali.rice.kew.rule.AbstractRoleAttribute;
36  import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
37  import org.kuali.rice.kim.api.role.RoleMember;
38  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
39  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
40  
41  import java.util.ArrayList;
42  import java.util.Collections;
43  import java.util.List;
44  
45  @Deprecated
46  public class BalanceTransferWorkflowAttribute extends AbstractRoleAttribute {
47  
48  	private static final long serialVersionUID = -1473150153290419492L;
49  	
50  	private static final Logger LOG = Logger.getLogger(BalanceTransferWorkflowAttribute.class);
51  
52      @Override
53      public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
54          List<String> roles = new ArrayList<String>();
55          String documentNumber = documentContent.getRouteContext().getDocument().getDocumentId();
56          MaintenanceDocument document = null;
57  		try {
58  			document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentNumber);
59  		} catch (WorkflowException e) {
60  			e.printStackTrace();
61  		}
62  		
63  		BalanceTransfer balanceTransfer = null;
64  		if (document != null && document.getNewMaintainableObject() != null) {
65  			balanceTransfer = (BalanceTransfer) document.getNewMaintainableObject().getDataObject();
66  		}
67  		
68          if (balanceTransfer != null) {
69              List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(balanceTransfer.getPrincipalId(), balanceTransfer.getEffectiveLocalDate());
70              for (Assignment assignment : assignments) {
71                  String roleStr = roleName + "_" + assignment.getWorkArea();
72                  if (!roles.contains(roleStr)) {
73                      roles.add(roleStr);
74                  }
75              }
76          }
77          
78          return roles;
79      }
80  
81      @Override
82      public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
83          ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
84          Long workAreaNumber = null;
85  
86          try {
87              int pos = qualifiedRole.lastIndexOf("_");
88              if (pos > -1) {
89                  String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
90                  workAreaNumber = Long.parseLong(subs);
91              }
92          } catch (NumberFormatException nfe) {
93              LOG.error("qualifiedRole did not contain numeric data for work area.");
94          }
95  
96          if (workAreaNumber == null) {
97              throw new RuntimeException("Unable to resolve work area during routing.");
98          }
99  
100         List<Id> principals = new ArrayList<Id>();
101         String routeHeaderId = routeContext.getDocument().getDocumentId();
102         MaintenanceDocument document = null;
103 		try {
104 			document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(routeHeaderId);
105 		} catch (WorkflowException e) {
106 			LOG.error("unable to retrieve the Maintenance Document with route hearder id: " + routeHeaderId);
107 			e.printStackTrace();
108 		}
109         
110         BalanceTransfer balanceTransfer = null;
111         if (document != null && document.getNewMaintainableObject() != null) {
112         	balanceTransfer = (BalanceTransfer) document.getNewMaintainableObject().getDataObject();
113         }
114         
115         if (balanceTransfer != null) {
116 	        WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, balanceTransfer.getEffectiveLocalDate());
117 	
118 	        List<RoleMember> roleMembers = new ArrayList<RoleMember>();
119 	        
120 			if (TkConstants.ROLE_TK_APPROVER.equals(roleName)) {
121 		        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workAreaNumber, LocalDate.now().toDateTimeAtStartOfDay(), true));
122 		        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workAreaNumber, LocalDate.now().toDateTimeAtStartOfDay(), true));
123 			}
124 	
125 	        for (RoleMember roleMember : roleMembers) {
126 	        	principals.add(new PrincipalId(roleMember.getMemberId()));
127 		    }
128 	
129 	        if (principals.size() == 0)  {
130 	            throw new RuntimeException("No principals to route to. Push to exception routing.");
131             }
132 	        
133 	        rqr.setRecipients(principals);
134 	        rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
135 	        
136 	        return rqr;
137         } else {
138         	throw new RuntimeException("no business object could be retreived");
139         }
140     }
141 
142     @Override
143     public List<RoleName> getRoleNames() {
144         return Collections.emptyList();
145     }
146 
147 }