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