1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.workflow;
17
18 import org.apache.cxf.common.util.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.hr.job.Job;
21 import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
22 import org.kuali.hr.time.assignment.Assignment;
23 import org.kuali.hr.time.roles.TkRole;
24 import org.kuali.hr.time.roles.service.TkRoleService;
25 import org.kuali.hr.time.service.base.TkServiceLocator;
26 import org.kuali.hr.time.util.TKUtils;
27 import org.kuali.hr.time.util.TkConstants;
28 import org.kuali.hr.time.workarea.WorkArea;
29 import org.kuali.rice.kew.api.identity.Id;
30 import org.kuali.rice.kew.api.identity.PrincipalId;
31 import org.kuali.rice.kew.api.rule.RoleName;
32 import org.kuali.rice.kew.engine.RouteContext;
33 import org.kuali.rice.kew.routeheader.DocumentContent;
34 import org.kuali.rice.kew.rule.AbstractRoleAttribute;
35 import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
36
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
40
41 public class TkWorkflowLeaveCalendarAttribute extends AbstractRoleAttribute {
42
43 private static final Logger LOG = Logger.getLogger(TkWorkflowLeaveCalendarAttribute.class);
44
45 @Override
46 public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
47 List<String> roles = new ArrayList<String>();
48 String routeHeaderId = documentContent.getRouteContext().getDocument().getDocumentId();
49 LeaveCalendarDocument leaveDocument = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(routeHeaderId);
50
51 if (leaveDocument != null) {
52 List<Assignment> assignments = leaveDocument.getAssignments();
53 for (Assignment assignment : assignments) {
54 String roleStr = roleName + "_" +assignment.getWorkArea();
55 if(!roles.contains(roleStr)){
56 roles.add(roleStr);
57 }
58 }
59 }
60 return roles;
61 }
62
63
64
65
66 @Override
67 public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
68 ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
69 Long workAreaNumber = null;
70
71 try {
72 int pos = qualifiedRole.lastIndexOf("_");
73 if (pos > -1) {
74 String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
75 workAreaNumber = Long.parseLong(subs);
76 }
77 } catch (NumberFormatException nfe) {
78 LOG.error("qualifiedRole did not contain numeric data for work area.");
79 }
80
81 if (workAreaNumber == null) {
82 throw new RuntimeException("Unable to resolve work area during routing.");
83 }
84
85 List<Id> principals = new ArrayList<Id>();
86 String routeHeaderId = routeContext.getDocument().getDocumentId();
87 TkRoleService roleService = TkServiceLocator.getTkRoleService();
88 LeaveCalendarDocument leaveCalendarDocument = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(routeHeaderId);
89 WorkArea workArea = null;
90 if (leaveCalendarDocument != null) {
91 workArea = TkServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, leaveCalendarDocument.getAsOfDate());
92 }
93
94
95 List<TkRole> approvers = roleService.getWorkAreaRoles(workAreaNumber, roleName, TKUtils.getCurrentDate());
96 List<TkRole> approverDelegates = roleService.getWorkAreaRoles(workAreaNumber, TkConstants.ROLE_TK_APPROVER_DELEGATE, TKUtils.getCurrentDate());
97 List<TkRole> roles = new ArrayList<TkRole>();
98 roles.addAll(approvers);
99 roles.addAll(approverDelegates);
100
101 for (TkRole role : roles) {
102
103 if(StringUtils.isEmpty(role.getPrincipalId())){
104 String positionNumber = role.getPositionNumber();
105 List<Job> lstJobsForPosition = TkServiceLocator.getJobService().getActiveJobsForPosition(positionNumber, leaveCalendarDocument.getCalendarEntry().getEndPeriodDateTime());
106 for(Job job : lstJobsForPosition){
107 PrincipalId pid = new PrincipalId(job.getPrincipalId());
108 if (!principals.contains(pid)) {
109 principals.add(pid);
110 }
111 }
112 } else {
113 PrincipalId pid = new PrincipalId(role.getPrincipalId());
114 if (!principals.contains(pid)) {
115 principals.add(pid);
116 }
117 }
118 }
119
120 if (principals.size() == 0)
121 throw new RuntimeException("No principals to route to. Push to exception routing.");
122
123 rqr.setRecipients(principals);
124 rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
125
126 return rqr;
127 }
128
129 @Override
130 public List<RoleName> getRoleNames() {
131 return Collections.EMPTY_LIST;
132 }
133
134 }