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.DateTime;
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.payout.LeavePayout;
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 LeavePayoutWorkflowAttribute extends AbstractRoleAttribute {
47  
48  	private static final long serialVersionUID = 540529528227045564L;
49  	
50  	private static final Logger LOG = Logger.getLogger(LeavePayoutWorkflowAttribute.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  		LeavePayout leavePayout = null;
63  		if (document != null && document.getNewMaintainableObject() != null) {
64  			leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
65  		}
66  		
67          if (leavePayout != null) {
68              List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(leavePayout.getPrincipalId(), leavePayout.getEffectiveLocalDate());
69              for (Assignment assignment : assignments) {
70                  String roleStr = roleName + "_" + assignment.getWorkArea();
71                  if (!roles.contains(roleStr)) {
72                  	roles.add(roleStr);
73              	}
74              }
75          }
76          
77          return roles;
78      }
79  
80      @Override
81      public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
82          ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
83          Long workAreaNumber = null;
84  
85          try {
86              int pos = qualifiedRole.lastIndexOf("_");
87              if (pos > -1) {
88                  String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
89                  workAreaNumber = Long.parseLong(subs);
90              }
91          } catch (NumberFormatException nfe) {
92              LOG.error("qualifiedRole did not contain numeric data for work area.");
93          }
94  
95          if (workAreaNumber == null) {
96              throw new RuntimeException("Unable to resolve work area during routing.");
97          }
98  
99          List<Id> principals = new ArrayList<Id>();
100         String routeHeaderId = routeContext.getDocument().getDocumentId();
101         MaintenanceDocument document = null;
102 		try {
103 			document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(routeHeaderId);
104 		} catch (WorkflowException e) {
105 			LOG.error("unable to retrieve the Maintenance Document with route header id: " + routeHeaderId);
106 			e.printStackTrace();
107 		}
108 		
109         LeavePayout leavePayout = null;
110         if (document != null && document.getNewMaintainableObject() != null) {
111         	leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
112         }
113         
114         if (leavePayout != null) {
115 	        WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, leavePayout.getEffectiveLocalDate());
116 	
117 	        List<RoleMember> roleMembers = new ArrayList<RoleMember>();
118 	        
119 	        if (TkConstants.ROLE_TK_APPROVER.equals(roleName)) {
120 		        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workAreaNumber, new DateTime(), true));
121 		        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workAreaNumber, new DateTime(), true));
122 			}
123 			
124 	        for (RoleMember roleMember : roleMembers) {
125 	        	principals.add(new PrincipalId(roleMember.getMemberId()));
126 		    }
127 	
128 	        if (principals.size() == 0)  {
129 	            throw new RuntimeException("No principals to route to. Push to exception routing.");
130             }
131 	        
132 	        rqr.setRecipients(principals);
133 	        rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
134 	        
135 	        return rqr;
136         } else {
137         	throw new RuntimeException("no business object could be retreived");
138         }
139     }
140 
141     @Override
142     public List<RoleName> getRoleNames() {
143         return Collections.emptyList();
144     }
145 
146 }