1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package edu.sampleu.travel.workflow;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.kuali.rice.core.util.KeyLabelPair;
26 import org.kuali.rice.kew.engine.RouteContext;
27 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
28 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
29 import org.kuali.rice.kew.identity.Id;
30 import org.kuali.rice.kew.routeheader.DocumentContent;
31 import org.kuali.rice.kew.rule.GenericRoleAttribute;
32 import org.kuali.rice.kew.rule.QualifiedRoleName;
33 import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
34 import org.kuali.rice.kew.rule.Role;
35 import org.kuali.rice.kew.user.AuthenticationUserId;
36 import org.kuali.rice.kew.user.UserId;
37 import org.kuali.rice.kew.user.WorkflowUserId;
38 import org.kuali.rice.kim.bo.entity.KimPrincipal;
39 import org.kuali.rice.kim.service.KIMServiceLocator;
40 import org.kuali.rice.kns.web.ui.Field;
41 import org.kuali.rice.kns.web.ui.Row;
42
43
44
45
46
47 public class EmployeeAttribute extends GenericRoleAttribute {
48 private static final Role EMPLOYEE_ROLE = new Role(EmployeeAttribute.class, "employee", "Employee");
49 private static final Role SUPERVISOR_ROLE = new Role(EmployeeAttribute.class, "supervisr", "Supervisor");
50 private static final Role DIRECTOR_ROLE = new Role(EmployeeAttribute.class, "director", "Dean/Director");
51 private static final List<Role> ROLES;
52 static {
53 List<Role> tmp = new ArrayList<Role>(1);
54 tmp.add(EMPLOYEE_ROLE);
55 tmp.add(SUPERVISOR_ROLE);
56 tmp.add(DIRECTOR_ROLE);
57 ROLES = Collections.unmodifiableList(tmp);
58 }
59
60 private static String USERID_FORM_FIELDNAME = "userid";
61
62
63
64
65 private String traveler;
66
67
68
69 public EmployeeAttribute() {
70 super("employee");
71 }
72
73 public EmployeeAttribute(String traveler) {
74 super("employee");
75 this.traveler = traveler;
76 }
77
78
79 public void setTraveler(String traveler) {
80 this.traveler = traveler;
81 }
82
83
84 public List<Role> getRoleNames() {
85 return ROLES;
86 }
87
88 protected boolean isValidRole(String roleName) {
89 for (Role role: ROLES) {
90 if (role.getBaseName().equals(roleName)) {
91 return true;
92 }
93 }
94 return false;
95 }
96
97
98 @Override
99 protected List<String> getRoleNameQualifiers(String roleName, DocumentContent documentContent) {
100 if (!isValidRole(roleName)) {
101 throw new WorkflowRuntimeException("Invalid role: " + roleName);
102 }
103
104 List<String> qualifiers = new ArrayList<String>();
105 qualifiers.add(roleName);
106
107
108
109
110
111
112
113
114
115
116
117
118
119 return qualifiers;
120 }
121
122 @Override
123 protected ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
124 List<Id> recipients = resolveRecipients(routeContext, qualifiedRoleName);
125 ResolvedQualifiedRole rqr = new ResolvedQualifiedRole(getLabelForQualifiedRoleName(qualifiedRoleName),
126 recipients,
127 qualifiedRoleName.getBaseRoleName());
128 return rqr;
129 }
130
131 @Override
132 protected List<Id> resolveRecipients(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
133 List<Id> members = new ArrayList<Id>();
134 UserId roleUserId = null;
135 String roleName = qualifiedRoleName.getBaseRoleName();
136 String roleTraveler = qualifiedRoleName.getQualifier();
137
138
139 if (StringUtils.equals(EMPLOYEE_ROLE.getBaseName(), roleName)) {
140 roleUserId = new WorkflowUserId(roleTraveler);
141
142
143 } else if (StringUtils.equals(SUPERVISOR_ROLE.getBaseName(), roleName)) {
144
145
146
147
148
149 roleUserId = new AuthenticationUserId("supervisr");
150
151
152 } else if (StringUtils.equals(DIRECTOR_ROLE.getBaseName(), roleName)) {
153
154
155
156
157
158 roleUserId = new AuthenticationUserId("director");
159 } else {
160
161 throw new WorkflowRuntimeException("unable to process unknown role '" + roleName + "'");
162 }
163 members.add(roleUserId);
164
165 return members;
166 }
167
168 public Map<String, String> getProperties() {
169 Map<String, String> properties = new HashMap<String, String>();
170 properties.put("traveler", traveler);
171 return properties;
172 }
173
174
175
176
177
178
179 public List getFieldConversions() {
180 List conversionFields = new ArrayList();
181 conversionFields.add(new KeyLabelPair("userid", USERID_FORM_FIELDNAME));
182 return conversionFields;
183 }
184
185 public List<Row> getRoutingDataRows() {
186 List<Row> rows = new ArrayList<Row>();
187
188 List<Field> fields = new ArrayList<Field>();
189 fields.add(new Field("Traveler username", "", Field.TEXT, false, USERID_FORM_FIELDNAME, "", false, false, null, null));
190 rows.add(new Row(fields));
191
192 return rows;
193 }
194
195 public List validateRoutingData(Map paramMap) {
196 List errors = new ArrayList();
197
198 String userid = StringUtils.trim((String) paramMap.get(USERID_FORM_FIELDNAME));
199 if (isRequired() && StringUtils.isBlank(userid)) {
200 errors.add(new WorkflowServiceErrorImpl("userid is required", "uh.accountattribute.userid.required"));
201 }
202
203 KimPrincipal principal = null;
204 if (!StringUtils.isBlank(userid)) {
205 principal = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(userid);
206 }
207 if (principal == null) {
208 errors.add(new WorkflowServiceErrorImpl("unable to retrieve user for userid '" + userid + "'", "uh.accountattribute.userid.invalid"));
209 }
210
211 if (errors.size() == 0) {
212 traveler = principal.getPrincipalId();
213 }
214
215 return errors;
216 }
217 }