1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.time.workflow;
17
18 import java.util.ArrayList;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.lang.math.NumberUtils;
26 import org.apache.log4j.Logger;
27 import org.joda.time.DateTime;
28 import org.kuali.kpme.core.KPMENamespace;
29 import org.kuali.kpme.core.assignment.Assignment;
30 import org.kuali.kpme.core.role.KPMERole;
31 import org.kuali.kpme.core.service.HrServiceLocator;
32 import org.kuali.kpme.tklm.time.service.TkServiceLocator;
33 import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
34 import org.kuali.rice.kew.api.identity.Id;
35 import org.kuali.rice.kew.api.identity.PrincipalId;
36 import org.kuali.rice.kew.api.rule.RoleName;
37 import org.kuali.rice.kew.engine.RouteContext;
38 import org.kuali.rice.kew.routeheader.DocumentContent;
39 import org.kuali.rice.kew.rule.GenericRoleAttribute;
40 import org.kuali.rice.kew.rule.QualifiedRoleName;
41 import org.kuali.rice.kim.api.role.RoleMember;
42
43 @SuppressWarnings("unchecked")
44 public class TimesheetRoleAttribute extends GenericRoleAttribute {
45
46 private static final long serialVersionUID = 6133675090144279606L;
47
48 private static final Logger LOG = Logger.getLogger(TimesheetRoleAttribute.class);
49
50 @Override
51 public List<RoleName> getRoleNames() {
52 List<RoleName> roleNames = new ArrayList<RoleName>();
53
54 roleNames.add(RoleName.Builder.create(getClass().getName(), KPMERole.APPROVER.getRoleName(), KPMERole.APPROVER.getRoleName()).build());
55 roleNames.add(RoleName.Builder.create(getClass().getName(), KPMERole.APPROVER_DELEGATE.getRoleName(), KPMERole.APPROVER_DELEGATE.getRoleName()).build());
56
57 return roleNames;
58 }
59
60 @Override
61 protected List<String> getRoleNameQualifiers(String roleName, DocumentContent documentContent) {
62 Set<String> roleNameQualifiers = new HashSet<String>();
63
64 Long routeHeaderId = new Long(documentContent.getRouteContext().getDocument().getDocumentId());
65 TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(routeHeaderId.toString());
66
67 if (timesheetDocument != null) {
68 List<Assignment> assignments = timesheetDocument.getAssignments();
69 for (Assignment assignment : assignments) {
70 roleNameQualifiers.add(String.valueOf(assignment.getWorkArea()));
71 }
72 }
73
74 return new ArrayList<String>(roleNameQualifiers);
75 }
76
77 @Override
78 protected List<Id> resolveRecipients(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
79 List<Id> recipients = new ArrayList<Id>();
80
81 String roleName = qualifiedRoleName.getBaseRoleName();
82 String qualifier = qualifiedRoleName.getQualifier();
83
84 if (StringUtils.isNotBlank(roleName) && NumberUtils.isNumber(qualifier)) {
85 Long workArea = Long.valueOf(qualifier);
86
87 List<RoleMember> roleMembers = HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), roleName, workArea, new DateTime(), true);
88
89 for (RoleMember roleMember : roleMembers) {
90 recipients.add(new PrincipalId(roleMember.getMemberId()));
91 }
92 } else {
93 LOG.error("Could not route to role " + roleName + " with work area " + qualifier);
94 }
95
96 return recipients;
97 }
98
99 @Override
100 public Map<String, String> getProperties() {
101 return null;
102 }
103
104 }