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