1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.rule.web; |
18 | |
|
19 | |
import org.apache.commons.beanutils.PropertyUtils; |
20 | |
import org.apache.commons.lang.ClassUtils; |
21 | |
import org.apache.struts.action.ActionErrors; |
22 | |
import org.apache.struts.action.ActionMessage; |
23 | |
import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator; |
24 | |
import org.kuali.rice.kew.rule.RuleBaseValues; |
25 | |
import org.kuali.rice.kew.rule.RuleDelegation; |
26 | |
import org.kuali.rice.kew.rule.RuleExtension; |
27 | |
import org.kuali.rice.kew.rule.RuleExtensionValue; |
28 | |
import org.kuali.rice.kew.rule.RuleResponsibility; |
29 | |
import org.kuali.rice.kew.rule.service.RuleService; |
30 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
31 | |
import org.kuali.rice.kew.util.KEWConstants; |
32 | |
import org.kuali.rice.kim.api.entity.principal.Principal; |
33 | |
import org.kuali.rice.kim.api.group.Group; |
34 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
35 | |
|
36 | |
import org.kuali.rice.kns.util.KNSConstants; |
37 | |
|
38 | |
import java.lang.reflect.InvocationHandler; |
39 | |
import java.lang.reflect.Method; |
40 | |
import java.lang.reflect.Proxy; |
41 | |
import java.util.Iterator; |
42 | |
import java.util.List; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public class WebRuleResponsibility extends RuleResponsibility { |
53 | |
|
54 | |
private static final long serialVersionUID = -8422695726158274189L; |
55 | |
|
56 | |
private static final String DISPLAY_INLINE = "display:inline"; |
57 | |
|
58 | |
private static final String DISPLAY_NONE = "display:none"; |
59 | |
|
60 | |
private String reviewer; |
61 | |
|
62 | 0 | private String reviewerStyle = ""; |
63 | |
|
64 | 0 | private String personLookupStyle = ""; |
65 | |
|
66 | 0 | private String workgroupLookupStyle = ""; |
67 | |
|
68 | |
private String roleReviewer; |
69 | |
|
70 | 0 | private String roleAreaStyle = ""; |
71 | |
|
72 | 0 | private boolean delegationRulesMaterialized = false; |
73 | |
|
74 | 0 | private boolean showDelegations = false; |
75 | |
|
76 | |
private int numberOfDelegations; |
77 | |
|
78 | 0 | private int index = 0; |
79 | |
|
80 | 0 | private boolean hasDelegateRuleTemplate = false; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
private String reviewerId; |
87 | |
|
88 | |
public String getReviewerId() { |
89 | 0 | return reviewerId; |
90 | |
} |
91 | |
|
92 | |
public void setReviewerId(String reviewerId) { |
93 | 0 | this.reviewerId = reviewerId; |
94 | 0 | } |
95 | |
|
96 | 0 | public WebRuleResponsibility() { |
97 | 0 | setRuleResponsibilityType(KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID); |
98 | 0 | setApprovePolicy(KEWConstants.APPROVE_POLICY_FIRST_APPROVE); |
99 | 0 | } |
100 | |
|
101 | |
public void initialize() throws Exception { |
102 | 0 | if (getDelegationRules().size() <= Integer.parseInt(CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.RULE_DETAIL_TYPE, KEWConstants.RULE_DELEGATE_LIMIT))) { |
103 | 0 | showDelegations = true; |
104 | |
} |
105 | 0 | setNumberOfDelegations(getDelegationRules().size()); |
106 | 0 | if (delegationRulesMaterialized) { |
107 | 0 | for (Iterator iterator = getDelegationRules().iterator(); iterator.hasNext();) { |
108 | 0 | RuleDelegation ruleDelegation = (RuleDelegation) iterator.next(); |
109 | 0 | WebRuleBaseValues webRule = (WebRuleBaseValues) ruleDelegation.getDelegationRuleBaseValues(); |
110 | 0 | webRule.initialize(); |
111 | 0 | } |
112 | |
} |
113 | 0 | establishRequiredState(); |
114 | 0 | } |
115 | |
|
116 | |
private void loadWebValues() throws Exception { |
117 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(getRuleResponsibilityName())) { |
118 | 0 | if (KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID.equals(getRuleResponsibilityType())) { |
119 | |
|
120 | |
|
121 | 0 | Principal principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(getRuleResponsibilityName()); |
122 | 0 | setReviewer(principal.getPrincipalName()); |
123 | 0 | setReviewerId(principal.getPrincipalId()); |
124 | 0 | } else if (KEWConstants.RULE_RESPONSIBILITY_GROUP_ID.equals(getRuleResponsibilityType())) { |
125 | |
|
126 | |
|
127 | |
|
128 | 0 | Group group = KimApiServiceLocator.getIdentityManagementService(). |
129 | |
getGroup(getRuleResponsibilityName()); |
130 | 0 | setReviewer(group.getName()); |
131 | 0 | setReviewerId(group.getId()); |
132 | 0 | } else if (KEWConstants.RULE_RESPONSIBILITY_ROLE_ID.equals(getRuleResponsibilityType())) { |
133 | 0 | setRoleReviewer(getRuleResponsibilityName()); |
134 | 0 | setReviewer(getResolvedRoleName()); |
135 | |
} |
136 | |
} |
137 | 0 | } |
138 | |
|
139 | |
private void injectWebMembers() throws Exception { |
140 | 0 | DelegationRulesProxy delegationRulesProxy = new DelegationRulesProxy(getDelegationRules()); |
141 | 0 | Class delegationRulesClass = getDelegationRules().getClass(); |
142 | |
|
143 | 0 | Class[] delegationRulesInterfaces = new Class[0]; |
144 | 0 | List<Class> delegationRulesInterfaceList = (List<Class>) ClassUtils.getAllInterfaces(delegationRulesClass); |
145 | 0 | delegationRulesInterfaces = delegationRulesInterfaceList.toArray(delegationRulesInterfaces); |
146 | 0 | ClassLoader delegationRulesClassLoader = getDelegationRules().getClass().getClassLoader(); |
147 | 0 | Object o = Proxy.newProxyInstance(delegationRulesClassLoader, delegationRulesInterfaces, delegationRulesProxy); |
148 | |
|
149 | |
|
150 | 0 | if (Integer.parseInt(CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.RULE_DETAIL_TYPE, KEWConstants.RULE_DELEGATE_LIMIT)) > getDelegationRules().size() || showDelegations) { |
151 | 0 | for (Iterator iterator = getDelegationRules().iterator(); iterator.hasNext();) { |
152 | 0 | RuleDelegation ruleDelegation = (RuleDelegation) iterator.next(); |
153 | 0 | WebRuleBaseValues webRule = new WebRuleBaseValues(); |
154 | 0 | webRule.load(ruleDelegation.getDelegationRuleBaseValues()); |
155 | 0 | webRule.edit(ruleDelegation.getDelegationRuleBaseValues()); |
156 | 0 | ruleDelegation.setDelegationRuleBaseValues(webRule); |
157 | 0 | } |
158 | |
} |
159 | 0 | } |
160 | |
|
161 | |
public RuleDelegation addNewDelegation() { |
162 | 0 | RuleDelegation ruleDelegation = new RuleDelegation(); |
163 | 0 | ruleDelegation.setDelegationRuleBaseValues(new WebRuleBaseValues()); |
164 | 0 | ruleDelegation.setDelegationType(KEWConstants.DELEGATION_PRIMARY); |
165 | 0 | ruleDelegation.getDelegationRuleBaseValues().setDelegateRule(Boolean.TRUE); |
166 | 0 | ruleDelegation.getDelegationRuleBaseValues().setDocTypeName(getRuleBaseValues().getDocTypeName()); |
167 | 0 | getDelegationRules().add(ruleDelegation); |
168 | 0 | showDelegations = true; |
169 | 0 | return ruleDelegation; |
170 | |
} |
171 | |
|
172 | |
public String getReviewer() { |
173 | 0 | return reviewer; |
174 | |
} |
175 | |
|
176 | |
public void setReviewer(String reviewer) { |
177 | 0 | this.reviewer = reviewer; |
178 | 0 | } |
179 | |
|
180 | |
public void setWorkgroupId(String workgroupId) { |
181 | 0 | Group workgroup = KimApiServiceLocator.getIdentityManagementService().getGroup(workgroupId); |
182 | |
|
183 | 0 | if (workgroup != null) { |
184 | 0 | setReviewer(workgroup.getName()); |
185 | |
} else { |
186 | 0 | setReviewer(""); |
187 | |
} |
188 | 0 | } |
189 | |
|
190 | |
public String getPersonLookupStyle() { |
191 | 0 | return personLookupStyle; |
192 | |
} |
193 | |
|
194 | |
public void setPersonLookupStyle(String personLookupStyle) { |
195 | 0 | this.personLookupStyle = personLookupStyle; |
196 | 0 | } |
197 | |
|
198 | |
public String getReviewerStyle() { |
199 | 0 | return reviewerStyle; |
200 | |
} |
201 | |
|
202 | |
public void setReviewerStyle(String reviewerStyle) { |
203 | 0 | this.reviewerStyle = reviewerStyle; |
204 | 0 | } |
205 | |
|
206 | |
public String getRoleAreaStyle() { |
207 | 0 | return roleAreaStyle; |
208 | |
} |
209 | |
|
210 | |
public void setRoleAreaStyle(String roleAreaLookupStyle) { |
211 | 0 | this.roleAreaStyle = roleAreaLookupStyle; |
212 | 0 | } |
213 | |
|
214 | |
public String getWorkgroupLookupStyle() { |
215 | 0 | return workgroupLookupStyle; |
216 | |
} |
217 | |
|
218 | |
public void setWorkgroupLookupStyle(String workgroupLookupStyle) { |
219 | 0 | this.workgroupLookupStyle = workgroupLookupStyle; |
220 | 0 | } |
221 | |
|
222 | |
public RuleDelegation getDelegationRule(int index) { |
223 | 0 | while (getDelegationRules().size() <= index) { |
224 | 0 | addNewDelegation(); |
225 | |
} |
226 | 0 | return (RuleDelegation) getDelegationRules().get(index); |
227 | |
} |
228 | |
|
229 | |
public int getNumberOfDelegations() { |
230 | 0 | return numberOfDelegations; |
231 | |
} |
232 | |
|
233 | |
public void setNumberOfDelegations(int numberOfDelegations) { |
234 | 0 | this.numberOfDelegations = numberOfDelegations; |
235 | 0 | } |
236 | |
|
237 | |
public boolean isDelegationRulesMaterialized() { |
238 | 0 | return delegationRulesMaterialized; |
239 | |
} |
240 | |
|
241 | |
public void setDelegationRulesMaterialized(boolean isDelegationRulesMaterialized) { |
242 | 0 | this.delegationRulesMaterialized = isDelegationRulesMaterialized; |
243 | 0 | } |
244 | |
|
245 | |
public String getRoleReviewer() { |
246 | 0 | return roleReviewer; |
247 | |
} |
248 | |
|
249 | |
public void setRoleReviewer(String roleReviewer) { |
250 | 0 | this.roleReviewer = roleReviewer; |
251 | 0 | } |
252 | |
|
253 | |
public int getIndex() { |
254 | 0 | return index; |
255 | |
} |
256 | |
|
257 | |
public void setIndex(int index) { |
258 | 0 | this.index = index; |
259 | 0 | } |
260 | |
|
261 | |
public boolean isShowDelegations() { |
262 | 0 | return showDelegations; |
263 | |
} |
264 | |
|
265 | |
public void setShowDelegations(boolean showDelegations) { |
266 | 0 | this.showDelegations = showDelegations; |
267 | 0 | } |
268 | |
|
269 | |
public void establishRequiredState() throws Exception { |
270 | 0 | if (KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID.equals(getRuleResponsibilityType())) { |
271 | 0 | reviewerStyle = DISPLAY_INLINE; |
272 | 0 | personLookupStyle = DISPLAY_INLINE; |
273 | 0 | workgroupLookupStyle = DISPLAY_NONE; |
274 | 0 | roleAreaStyle = DISPLAY_NONE; |
275 | |
} |
276 | 0 | if (KEWConstants.RULE_RESPONSIBILITY_GROUP_ID.equals(getRuleResponsibilityType())) { |
277 | 0 | reviewerStyle = DISPLAY_INLINE; |
278 | 0 | personLookupStyle = DISPLAY_NONE; |
279 | 0 | workgroupLookupStyle = DISPLAY_INLINE; |
280 | 0 | roleAreaStyle = DISPLAY_NONE; |
281 | |
} |
282 | 0 | if (KEWConstants.RULE_RESPONSIBILITY_ROLE_ID.equals(getRuleResponsibilityType())) { |
283 | 0 | reviewerStyle = DISPLAY_NONE; |
284 | 0 | personLookupStyle = DISPLAY_NONE; |
285 | 0 | workgroupLookupStyle = DISPLAY_NONE; |
286 | 0 | roleAreaStyle = DISPLAY_INLINE; |
287 | |
} |
288 | 0 | loadWebValues(); |
289 | 0 | if (delegationRulesMaterialized) { |
290 | 0 | for (Iterator iterator = getDelegationRules().iterator(); iterator.hasNext();) { |
291 | 0 | RuleDelegation delegation = (RuleDelegation) iterator.next(); |
292 | 0 | ((WebRuleBaseValues) delegation.getDelegationRuleBaseValues()).establishRequiredState(); |
293 | 0 | } |
294 | |
} |
295 | 0 | } |
296 | |
|
297 | |
public void validateResponsibility(String keyPrefix, ActionErrors errors) { |
298 | 0 | if (KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID.equals(getRuleResponsibilityType())) { |
299 | 0 | boolean invalidUser = org.apache.commons.lang.StringUtils.isEmpty(getReviewer()); |
300 | 0 | if (!invalidUser) |
301 | |
{ |
302 | |
|
303 | 0 | Principal principal = KimApiServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(getReviewer()); |
304 | 0 | if( principal != null) |
305 | |
{ |
306 | 0 | setRuleResponsibilityName(principal.getPrincipalId()); |
307 | |
} |
308 | |
else |
309 | |
{ |
310 | 0 | invalidUser = true; |
311 | |
} |
312 | |
} |
313 | 0 | if (invalidUser) { |
314 | 0 | errors.add(keyPrefix + "reviewer", new ActionMessage("routetemplate.ruleservice.user.invalid")); |
315 | |
} |
316 | 0 | } else if (KEWConstants.RULE_RESPONSIBILITY_GROUP_ID.equals(getRuleResponsibilityType())) { |
317 | 0 | boolean invalidWorkgroup = org.apache.commons.lang.StringUtils.isEmpty(getReviewer()); |
318 | |
; |
319 | 0 | if (!invalidWorkgroup) { |
320 | 0 | Group workgroup = KimApiServiceLocator.getIdentityManagementService().getGroup(getReviewerId()); |
321 | 0 | if (workgroup == null) { |
322 | 0 | invalidWorkgroup = true; |
323 | |
} else { |
324 | 0 | setRuleResponsibilityName(workgroup.getId()); |
325 | |
} |
326 | 0 | } else { |
327 | 0 | errors.add(keyPrefix + "reviewer", new ActionMessage("routetemplate.ruleservice.workgroup.invalid")); |
328 | |
} |
329 | |
|
330 | 0 | } else if (KEWConstants.RULE_RESPONSIBILITY_ROLE_ID.equals(getRuleResponsibilityType())) { |
331 | 0 | setRuleResponsibilityName(getRoleReviewer()); |
332 | |
} |
333 | |
|
334 | 0 | int delIndex = 0; |
335 | 0 | for (Iterator respIterator = getDelegationRules().iterator(); respIterator.hasNext();) { |
336 | 0 | String delPrefix = keyPrefix + "delegationRule[" + delIndex + "].delegationRuleBaseValues."; |
337 | 0 | RuleDelegation ruleDelegation = (RuleDelegation) respIterator.next(); |
338 | 0 | ((WebRuleBaseValues) ruleDelegation.getDelegationRuleBaseValues()).validateRule(delPrefix, errors); |
339 | 0 | } |
340 | 0 | } |
341 | |
|
342 | |
public void edit(RuleResponsibility ruleResponsibility) throws Exception { |
343 | 0 | load(ruleResponsibility); |
344 | 0 | initialize(); |
345 | 0 | } |
346 | |
|
347 | |
public void load(RuleResponsibility ruleResponsibility) throws Exception { |
348 | 0 | PropertyUtils.copyProperties(this, ruleResponsibility); |
349 | 0 | injectWebMembers(); |
350 | 0 | } |
351 | |
|
352 | |
public void loadDelegations() throws Exception { |
353 | 0 | fetchDelegations(); |
354 | |
|
355 | 0 | for (Iterator iterator = getDelegationRules().iterator(); iterator.hasNext();) { |
356 | 0 | RuleDelegation ruleDelegation = (RuleDelegation) iterator.next(); |
357 | 0 | WebRuleBaseValues webRule = new WebRuleBaseValues(); |
358 | 0 | webRule.edit(ruleDelegation.getDelegationRuleBaseValues()); |
359 | 0 | ruleDelegation.setDelegationRuleBaseValues(webRule); |
360 | 0 | } |
361 | 0 | delegationRulesMaterialized = true; |
362 | 0 | populatePreviousVersionIds(); |
363 | 0 | } |
364 | |
|
365 | |
public void populatePreviousVersionIds() { |
366 | 0 | if (delegationRulesMaterialized) { |
367 | 0 | for (Iterator iterator = getDelegationRules().iterator(); iterator.hasNext();) { |
368 | 0 | RuleDelegation delegation = (RuleDelegation) iterator.next(); |
369 | 0 | ((WebRuleBaseValues) delegation.getDelegationRuleBaseValues()).populatePreviousVersionIds(); |
370 | 0 | } |
371 | |
} |
372 | 0 | } |
373 | |
|
374 | |
private void fetchDelegations() { |
375 | 0 | if (getRuleResponsibilityKey() != null) { |
376 | 0 | RuleResponsibility responsibility = getRuleService().findByRuleResponsibilityId(getRuleResponsibilityKey()); |
377 | 0 | if (responsibility == null) { |
378 | 0 | return; |
379 | |
} |
380 | 0 | getDelegationRules().addAll(responsibility.getDelegationRules()); |
381 | |
} |
382 | 0 | } |
383 | |
|
384 | |
public void prepareHiddenDelegationsForRoute() { |
385 | 0 | if (showDelegations) { |
386 | 0 | return; |
387 | |
} |
388 | |
|
389 | 0 | fetchDelegations(); |
390 | |
|
391 | 0 | for (Iterator iter = getDelegationRules().iterator(); iter.hasNext();) { |
392 | 0 | RuleDelegation delegation = (RuleDelegation) iter.next(); |
393 | 0 | delegation.setDelegateRuleId(null); |
394 | 0 | delegation.setVersionNumber(null); |
395 | 0 | delegation.setRuleDelegationId(null); |
396 | |
|
397 | 0 | delegation.setResponsibilityId(null); |
398 | |
|
399 | 0 | RuleBaseValues rule = delegation.getDelegationRuleBaseValues(); |
400 | 0 | rule.setVersionNumber(null); |
401 | 0 | rule.setPreviousVersionId(rule.getRuleBaseValuesId()); |
402 | 0 | rule.setDocumentId(null); |
403 | 0 | rule.setRuleBaseValuesId(null); |
404 | |
|
405 | 0 | for (Iterator iterator = rule.getResponsibilities().iterator(); iterator.hasNext();) { |
406 | 0 | RuleResponsibility responsibility = (RuleResponsibility) iterator.next(); |
407 | 0 | responsibility.setVersionNumber(null); |
408 | 0 | responsibility.setRuleBaseValuesId(null); |
409 | 0 | responsibility.setRuleBaseValues(rule); |
410 | 0 | responsibility.setRuleResponsibilityKey(null); |
411 | 0 | } |
412 | |
|
413 | 0 | for (Iterator iterator = rule.getRuleExtensions().iterator(); iterator.hasNext();) { |
414 | 0 | RuleExtension extension = (RuleExtension) iterator.next(); |
415 | 0 | extension.setLockVerNbr(null); |
416 | 0 | extension.setRuleBaseValues(rule); |
417 | 0 | extension.setRuleBaseValuesId(null); |
418 | 0 | extension.setRuleExtensionId(null); |
419 | |
|
420 | 0 | for (Iterator iter2 = extension.getExtensionValues().iterator(); iter2.hasNext();) { |
421 | 0 | RuleExtensionValue value = (RuleExtensionValue) iter2.next(); |
422 | 0 | value.setExtension(extension); |
423 | 0 | value.setLockVerNbr(null); |
424 | 0 | value.setRuleExtensionId(null); |
425 | 0 | value.setRuleExtensionValueId(null); |
426 | 0 | } |
427 | 0 | } |
428 | 0 | } |
429 | 0 | } |
430 | |
|
431 | |
public boolean isHasDelegateRuleTemplate() { |
432 | 0 | return hasDelegateRuleTemplate; |
433 | |
} |
434 | |
|
435 | |
public void setHasDelegateRuleTemplate(boolean hasDelegateRuleTemplate) { |
436 | 0 | this.hasDelegateRuleTemplate = hasDelegateRuleTemplate; |
437 | 0 | } |
438 | |
|
439 | |
private RuleService getRuleService() { |
440 | 0 | return (RuleService) KEWServiceLocator.getService(KEWServiceLocator.RULE_SERVICE); |
441 | |
} |
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
private class DelegationRulesProxy implements InvocationHandler, java.io.Serializable { |
451 | |
|
452 | |
private static final long serialVersionUID = 7046323200221509473L; |
453 | |
|
454 | |
private List delegationRules; |
455 | |
|
456 | 0 | public DelegationRulesProxy(List delegationRules) { |
457 | 0 | this.delegationRules = delegationRules; |
458 | 0 | } |
459 | |
|
460 | |
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { |
461 | 0 | if (!delegationRulesMaterialized && !m.getName().equals("isEmpty") && !m.getName().equals("size")) { |
462 | 0 | for (Iterator iterator = delegationRules.iterator(); iterator.hasNext();) { |
463 | 0 | RuleDelegation ruleDelegation = (RuleDelegation) iterator.next(); |
464 | 0 | WebRuleBaseValues webRule = new WebRuleBaseValues(); |
465 | 0 | webRule.load(ruleDelegation.getDelegationRuleBaseValues()); |
466 | 0 | webRule.establishRequiredState(); |
467 | 0 | ruleDelegation.setDelegationRuleBaseValues(webRule); |
468 | 0 | } |
469 | 0 | delegationRulesMaterialized = true; |
470 | |
|
471 | |
} |
472 | 0 | return m.invoke(delegationRules, args); |
473 | |
} |
474 | |
|
475 | |
} |
476 | |
|
477 | |
} |