View Javadoc

1   /**
2    * Copyright 2004-2013 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 java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.apache.log4j.Logger;
23  import org.joda.time.DateTime;
24  import org.kuali.kpme.core.KPMENamespace;
25  import org.kuali.kpme.core.assignment.Assignment;
26  import org.kuali.kpme.core.role.KPMERole;
27  import org.kuali.kpme.core.service.HrServiceLocator;
28  import org.kuali.kpme.core.workarea.WorkArea;
29  import org.kuali.kpme.tklm.common.TkConstants;
30  import org.kuali.kpme.tklm.leave.payout.LeavePayout;
31  import org.kuali.rice.kew.api.exception.WorkflowException;
32  import org.kuali.rice.kew.api.identity.Id;
33  import org.kuali.rice.kew.api.identity.PrincipalId;
34  import org.kuali.rice.kew.api.rule.RoleName;
35  import org.kuali.rice.kew.engine.RouteContext;
36  import org.kuali.rice.kew.routeheader.DocumentContent;
37  import org.kuali.rice.kew.rule.AbstractRoleAttribute;
38  import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
39  import org.kuali.rice.kim.api.role.RoleMember;
40  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
41  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
42  
43  @Deprecated
44  public class LeavePayoutWorkflowAttribute extends AbstractRoleAttribute {
45  
46  	private static final long serialVersionUID = 540529528227045564L;
47  	
48  	private static final Logger LOG = Logger.getLogger(LeavePayoutWorkflowAttribute.class);
49  
50      @Override
51      public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
52          List<String> roles = new ArrayList<String>();
53          String documentNumber = documentContent.getRouteContext().getDocument().getDocumentId();
54          MaintenanceDocument document = null;
55  		try {
56  			document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentNumber);
57  		} catch (WorkflowException e) {
58  			e.printStackTrace();
59  		}
60  		LeavePayout leavePayout = null;
61  		if (document != null && document.getNewMaintainableObject() != null) {
62  			leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
63  		}
64  		
65          if (leavePayout != null) {
66              List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(leavePayout.getPrincipalId(), leavePayout.getEffectiveLocalDate());
67              for (Assignment assignment : assignments) {
68                  String roleStr = roleName + "_" + assignment.getWorkArea();
69                  if (!roles.contains(roleStr)) {
70                  	roles.add(roleStr);
71              	}
72              }
73          }
74          
75          return roles;
76      }
77  
78      @Override
79      public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
80          ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
81          Long workAreaNumber = null;
82  
83          try {
84              int pos = qualifiedRole.lastIndexOf("_");
85              if (pos > -1) {
86                  String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
87                  workAreaNumber = Long.parseLong(subs);
88              }
89          } catch (NumberFormatException nfe) {
90              LOG.error("qualifiedRole did not contain numeric data for work area.");
91          }
92  
93          if (workAreaNumber == null) {
94              throw new RuntimeException("Unable to resolve work area during routing.");
95          }
96  
97          List<Id> principals = new ArrayList<Id>();
98          String routeHeaderId = routeContext.getDocument().getDocumentId();
99          MaintenanceDocument document = null;
100 		try {
101 			document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(routeHeaderId);
102 		} catch (WorkflowException e) {
103 			LOG.error("unable to retrieve the Maintenance Document with route header id: " + routeHeaderId);
104 			e.printStackTrace();
105 		}
106 		
107         LeavePayout leavePayout = null;
108         if (document != null && document.getNewMaintainableObject() != null) {
109         	leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
110         }
111         
112         if (leavePayout != null) {
113 	        WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkAreaWithoutRoles(workAreaNumber, leavePayout.getEffectiveLocalDate());
114 	
115 	        List<RoleMember> roleMembers = new ArrayList<RoleMember>();
116 	        
117 	        if (TkConstants.ROLE_TK_APPROVER.equals(roleName)) {
118 		        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workAreaNumber, new DateTime(), true));
119 		        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workAreaNumber, new DateTime(), true));
120 			}
121 			
122 	        for (RoleMember roleMember : roleMembers) {
123 	        	principals.add(new PrincipalId(roleMember.getMemberId()));
124 		    }
125 	
126 	        if (principals.size() == 0)  {
127 	            throw new RuntimeException("No principals to route to. Push to exception routing.");
128             }
129 	        
130 	        rqr.setRecipients(principals);
131 	        rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
132 	        
133 	        return rqr;
134         } else {
135         	throw new RuntimeException("no business object could be retreived");
136         }
137     }
138 
139     @Override
140     public List<RoleName> getRoleNames() {
141         return Collections.emptyList();
142     }
143 
144 }