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