1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.lm.workflow;
17
18
19 import org.apache.cxf.common.util.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.kuali.hr.job.Job;
22 import org.kuali.hr.lm.leavepayout.LeavePayout;
23 import org.kuali.hr.lm.leaveblock.LeaveBlock;
24 import org.kuali.hr.time.assignment.Assignment;
25 import org.kuali.hr.time.calendar.CalendarEntries;
26 import org.kuali.hr.time.roles.TkRole;
27 import org.kuali.hr.time.roles.service.TkRoleService;
28 import org.kuali.hr.time.service.base.TkServiceLocator;
29 import org.kuali.hr.time.util.TKUtils;
30 import org.kuali.hr.time.util.TkConstants;
31 import org.kuali.hr.time.workarea.WorkArea;
32 import org.kuali.rice.kew.api.exception.WorkflowException;
33 import org.kuali.rice.kew.api.identity.Id;
34 import org.kuali.rice.kew.api.identity.PrincipalId;
35 import org.kuali.rice.kew.api.rule.RoleName;
36 import org.kuali.rice.kew.engine.RouteContext;
37 import org.kuali.rice.kew.routeheader.DocumentContent;
38 import org.kuali.rice.kew.rule.AbstractRoleAttribute;
39 import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
40 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
41 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
42 import org.kuali.rice.krad.util.ObjectUtils;
43
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.List;
47
48 public class LeavePayoutWorkflowAttribute extends AbstractRoleAttribute {
49 private static final Logger LOG = Logger.getLogger(LeavePayoutWorkflowAttribute.class);
50
51 @Override
52 public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
53 List<String> roles = new ArrayList<String>();
54 String documentNumber = documentContent.getRouteContext().getDocument().getDocumentId();
55 MaintenanceDocument document = null;
56 try {
57 document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentNumber);
58 } catch (WorkflowException e) {
59
60 e.printStackTrace();
61 }
62 LeavePayout leavePayout = null;
63 if(document != null
64 && document.getNewMaintainableObject() != null)
65 leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
66
67 if (leavePayout != null) {
68 List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignments(leavePayout.getPrincipalId(), leavePayout.getEffectiveDate());
69 for(Assignment assignment : assignments) {
70 String roleStr = roleName+"_"+assignment.getWorkArea();
71 if(!roles.contains(roleStr))
72 roles.add(roleStr);
73 }
74
75 }
76 return roles;
77 }
78
79
80
81
82 @Override
83 public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
84 ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
85 Long workAreaNumber = null;
86
87 try {
88 int pos = qualifiedRole.lastIndexOf("_");
89 if (pos > -1) {
90 String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
91 workAreaNumber = Long.parseLong(subs);
92 }
93 } catch (NumberFormatException nfe) {
94 LOG.error("qualifiedRole did not contain numeric data for work area.");
95 }
96
97 if (workAreaNumber == null) {
98 throw new RuntimeException("Unable to resolve work area during routing.");
99 }
100
101 List<Id> principals = new ArrayList<Id>();
102 String routeHeaderId = routeContext.getDocument().getDocumentId();
103 MaintenanceDocument document = null;
104 try {
105 document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(routeHeaderId);
106 } catch (WorkflowException e) {
107 LOG.error("unable to retrieve the Maintenance Document with route header id: " + routeHeaderId);
108 e.printStackTrace();
109 }
110 TkRoleService roleService = TkServiceLocator.getTkRoleService();
111 LeavePayout leavePayout = null;
112 if(ObjectUtils.isNotNull(document))
113 leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
114 if(ObjectUtils.isNotNull(leavePayout)) {
115 WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, leavePayout.getEffectiveDate());
116
117
118 List<TkRole> approvers = roleService.getWorkAreaRoles(workAreaNumber, roleName, TKUtils.getCurrentDate());
119 List<TkRole> approverDelegates = roleService.getWorkAreaRoles(workAreaNumber, TkConstants.ROLE_TK_APPROVER_DELEGATE, TKUtils.getCurrentDate());
120 List<TkRole> roles = new ArrayList<TkRole>();
121 roles.addAll(approvers);
122 roles.addAll(approverDelegates);
123
124 for (TkRole role : roles) {
125
126 if(StringUtils.isEmpty(role.getPrincipalId())){
127 String positionNumber = role.getPositionNumber();
128 List<Job> lstJobsForPosition = TkServiceLocator.getJobService().getActiveJobsForPosition(positionNumber, leavePayout.getEffectiveDate());
129 for(Job job : lstJobsForPosition){
130 PrincipalId pid = new PrincipalId(job.getPrincipalId());
131 if (!principals.contains(pid)) {
132 principals.add(pid);
133 }
134 }
135 } else {
136 PrincipalId pid = new PrincipalId(role.getPrincipalId());
137 if (!principals.contains(pid)) {
138 principals.add(pid);
139 }
140 }
141 }
142
143 if (principals.size() == 0) {
144 throw new RuntimeException("No principals to route to. Push to exception routing.");
145 }
146 rqr.setRecipients(principals);
147 rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
148 return rqr;
149 }
150 else {
151 throw new RuntimeException("no business object could be retreived");
152 }
153 }
154
155 @Override
156 public List<RoleName> getRoleNames() {
157 return Collections.EMPTY_LIST;
158 }
159
160 private CalendarEntries getCalendarEntry(LeaveBlock leaveBlock) {
161 return TkServiceLocator.getCalendarEntriesService().getCalendarEntries(leaveBlock.getCalendarId());
162 }
163 }