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