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