001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.routetemplate.web;
018:
019: import java.lang.reflect.InvocationHandler;
020: import java.lang.reflect.Method;
021: import java.lang.reflect.Proxy;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.commons.beanutils.PropertyUtils;
026: import org.apache.commons.lang.ClassUtils;
027: import org.apache.struts.action.ActionErrors;
028: import org.apache.struts.action.ActionMessage;
029:
030: import edu.iu.uis.eden.EdenConstants;
031: import edu.iu.uis.eden.KEWServiceLocator;
032: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
033: import edu.iu.uis.eden.routetemplate.RuleBaseValues;
034: import edu.iu.uis.eden.routetemplate.RuleDelegation;
035: import edu.iu.uis.eden.routetemplate.RuleExtension;
036: import edu.iu.uis.eden.routetemplate.RuleExtensionValue;
037: import edu.iu.uis.eden.routetemplate.RuleResponsibility;
038: import edu.iu.uis.eden.routetemplate.RuleService;
039: import edu.iu.uis.eden.user.AuthenticationUserId;
040: import edu.iu.uis.eden.user.UserService;
041: import edu.iu.uis.eden.user.WorkflowUser;
042: import edu.iu.uis.eden.user.WorkflowUserId;
043: import edu.iu.uis.eden.util.Utilities;
044: import edu.iu.uis.eden.workgroup.GroupNameId;
045: import edu.iu.uis.eden.workgroup.WorkflowGroupId;
046: import edu.iu.uis.eden.workgroup.Workgroup;
047: import edu.iu.uis.eden.workgroup.WorkgroupService;
048:
049: /**
050: * A decorator around a {@link RuleResponsibility} object which provides some
051: * convienance functions for interacting with the bean from the web-tier.
052: * This helps to alleviate some of the weaknesses of JSTL.
053: *
054: * @author ewestfal
055: * @author jhopf
056: */
057: public class WebRuleResponsibility extends RuleResponsibility {
058:
059: private static final long serialVersionUID = -8422695726158274189L;
060:
061: private static final String DISPLAY_INLINE = "display:inline";
062:
063: private static final String DISPLAY_NONE = "display:none";
064:
065: private String reviewer;
066:
067: private String reviewerStyle = "";
068:
069: private String personLookupStyle = "";
070:
071: private String workgroupLookupStyle = "";
072:
073: private String roleReviewer;
074:
075: private String roleAreaStyle = "";
076:
077: private boolean delegationRulesMaterialized = false;
078:
079: private boolean showDelegations = false;
080:
081: private int numberOfDelegations;
082:
083: private int index = 0;
084:
085: private boolean hasDelegateRuleTemplate = false;
086:
087: /**
088: * "reviewerId added to support links to workgroup report or user report
089: */
090:
091: private String reviewerId;
092:
093: public String getReviewerId() {
094: return reviewerId;
095: }
096:
097: public void setReviewerId(String reviewerId) {
098: this .reviewerId = reviewerId;
099: }
100:
101: public WebRuleResponsibility() {
102: setRuleResponsibilityType(EdenConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
103: setApprovePolicy(EdenConstants.APPROVE_POLICY_FIRST_APPROVE);
104: }
105:
106: public void initialize() throws Exception {
107: if (getDelegationRules().size() <= Integer
108: .parseInt(Utilities
109: .getApplicationConstant("Config.Application.DelegateLimit"))) {
110: showDelegations = true;
111: }
112: setNumberOfDelegations(getDelegationRules().size());
113: if (delegationRulesMaterialized) {
114: for (Iterator iterator = getDelegationRules().iterator(); iterator
115: .hasNext();) {
116: RuleDelegation ruleDelegation = (RuleDelegation) iterator
117: .next();
118: WebRuleBaseValues webRule = (WebRuleBaseValues) ruleDelegation
119: .getDelegationRuleBaseValues();
120: webRule.initialize();
121: }
122: }
123: establishRequiredState();
124: }
125:
126: private void loadWebValues() throws Exception {
127: if (!Utilities.isEmpty(getRuleResponsibilityName())) {
128: if (EdenConstants.RULE_RESPONSIBILITY_WORKFLOW_ID
129: .equals(getRuleResponsibilityType())) {
130: // setReviewer(getUserService().getWorkflowUser(new
131: // WorkflowUserId(getRuleResponsibilityName())).getAuthenticationUserId().getAuthenticationId());
132: WorkflowUser user = getUserService()
133: .getWorkflowUser(
134: new WorkflowUserId(
135: getRuleResponsibilityName()));
136: setReviewer(user.getAuthenticationUserId()
137: .getAuthenticationId());
138: setReviewerId(user.getWorkflowId());
139: } else if (EdenConstants.RULE_RESPONSIBILITY_WORKGROUP_ID
140: .equals(getRuleResponsibilityType())) {
141: // setReviewer(getWorkgroupService().getWorkgroup(new
142: // WorkflowGroupId(new
143: // Long(getRuleResponsibilityName()))).getGroupNameId().getNameId());
144: Workgroup group = getWorkgroupService().getWorkgroup(
145: new WorkflowGroupId(new Long(
146: getRuleResponsibilityName())));
147: setReviewer(group.getGroupNameId().getNameId());
148: setReviewerId(String.valueOf(group.getWorkflowGroupId()
149: .getGroupId()));
150: } else if (EdenConstants.RULE_RESPONSIBILITY_ROLE_ID
151: .equals(getRuleResponsibilityType())) {
152: setRoleReviewer(getRuleResponsibilityName());
153: setReviewer(getResolvedRoleName());
154: }
155: }
156: }
157:
158: private void injectWebMembers() throws Exception {
159: DelegationRulesProxy delegationRulesProxy = new DelegationRulesProxy(
160: getDelegationRules());
161: Class delegationRulesClass = getDelegationRules().getClass();
162: //System.err.println("delegation rules class: "+ delegationRulesClass);
163: Class[] delegationRulesInterfaces = new Class[0]; // = delegationRulesClass.getInterfaces();
164: List<Class> delegationRulesInterfaceList = (List<Class>) ClassUtils
165: .getAllInterfaces(delegationRulesClass);
166: delegationRulesInterfaces = delegationRulesInterfaceList
167: .toArray(delegationRulesInterfaces);
168: //System.err.println("# of interfaces: " + delegationRulesInterfaces.length);
169: //for (Class c: delegationRulesInterfaces) {
170: // System.err.println("a delegationrules list interface: " + c);
171: //}
172: ClassLoader delegationRulesClassLoader = getDelegationRules()
173: .getClass().getClassLoader();
174: Object o = Proxy.newProxyInstance(delegationRulesClassLoader,
175: delegationRulesInterfaces, delegationRulesProxy);
176: //System.err.println("DELEGATIONRULES PROXY OBJECT: " + o);
177: //System.err.println("DELEGATIONRULES PROXY OBJECT CLASS: " + o.getClass());
178: //System.err.println("DELEGATIONRULES PROXY OBJECT instanceof List?: " + (o instanceof List));
179: setDelegationRules((List) o);
180:
181: if (Integer
182: .parseInt(Utilities
183: .getApplicationConstant("Config.Application.DelegateLimit")) > getDelegationRules()
184: .size()
185: || showDelegations) {
186: for (Iterator iterator = getDelegationRules().iterator(); iterator
187: .hasNext();) {
188: RuleDelegation ruleDelegation = (RuleDelegation) iterator
189: .next();
190: WebRuleBaseValues webRule = new WebRuleBaseValues();
191: webRule.load(ruleDelegation
192: .getDelegationRuleBaseValues());
193: webRule.edit(ruleDelegation
194: .getDelegationRuleBaseValues());
195: ruleDelegation.setDelegationRuleBaseValues(webRule);
196: }
197: }
198: }
199:
200: public RuleDelegation addNewDelegation() {
201: RuleDelegation ruleDelegation = new RuleDelegation();
202: ruleDelegation
203: .setDelegationRuleBaseValues(new WebRuleBaseValues());
204: ruleDelegation
205: .setDelegationType(EdenConstants.DELEGATION_PRIMARY);
206: ruleDelegation.getDelegationRuleBaseValues().setDelegateRule(
207: Boolean.TRUE);
208: ruleDelegation.getDelegationRuleBaseValues().setDocTypeName(
209: getRuleBaseValues().getDocTypeName());
210: getDelegationRules().add(ruleDelegation);
211: showDelegations = true;
212: return ruleDelegation;
213: }
214:
215: public String getReviewer() {
216: return reviewer;
217: }
218:
219: public void setReviewer(String reviewer) {
220: this .reviewer = reviewer;
221: }
222:
223: public void setWorkgroupId(Long workgroupId) {
224: Workgroup workgroup = getWorkgroupService().getWorkgroup(
225: new WorkflowGroupId(workgroupId));
226: if (workgroup != null) {
227: setReviewer(workgroup.getGroupNameId().getNameId());
228: } else {
229: setReviewer("");
230: }
231: }
232:
233: public String getPersonLookupStyle() {
234: return personLookupStyle;
235: }
236:
237: public void setPersonLookupStyle(String personLookupStyle) {
238: this .personLookupStyle = personLookupStyle;
239: }
240:
241: public String getReviewerStyle() {
242: return reviewerStyle;
243: }
244:
245: public void setReviewerStyle(String reviewerStyle) {
246: this .reviewerStyle = reviewerStyle;
247: }
248:
249: public String getRoleAreaStyle() {
250: return roleAreaStyle;
251: }
252:
253: public void setRoleAreaStyle(String roleAreaLookupStyle) {
254: this .roleAreaStyle = roleAreaLookupStyle;
255: }
256:
257: public String getWorkgroupLookupStyle() {
258: return workgroupLookupStyle;
259: }
260:
261: public void setWorkgroupLookupStyle(String workgroupLookupStyle) {
262: this .workgroupLookupStyle = workgroupLookupStyle;
263: }
264:
265: public RuleDelegation getDelegationRule(int index) {
266: while (getDelegationRules().size() <= index) {
267: addNewDelegation();
268: }
269: return (RuleDelegation) getDelegationRules().get(index);
270: }
271:
272: public int getNumberOfDelegations() {
273: return numberOfDelegations;
274: }
275:
276: public void setNumberOfDelegations(int numberOfDelegations) {
277: this .numberOfDelegations = numberOfDelegations;
278: }
279:
280: public boolean isDelegationRulesMaterialized() {
281: return delegationRulesMaterialized;
282: }
283:
284: public void setDelegationRulesMaterialized(
285: boolean isDelegationRulesMaterialized) {
286: this .delegationRulesMaterialized = isDelegationRulesMaterialized;
287: }
288:
289: public String getRoleReviewer() {
290: return roleReviewer;
291: }
292:
293: public void setRoleReviewer(String roleReviewer) {
294: this .roleReviewer = roleReviewer;
295: }
296:
297: public int getIndex() {
298: return index;
299: }
300:
301: public void setIndex(int index) {
302: this .index = index;
303: }
304:
305: public boolean isShowDelegations() {
306: return showDelegations;
307: }
308:
309: public void setShowDelegations(boolean showDelegations) {
310: this .showDelegations = showDelegations;
311: }
312:
313: public void establishRequiredState() throws Exception {
314: if (EdenConstants.RULE_RESPONSIBILITY_WORKFLOW_ID
315: .equals(getRuleResponsibilityType())) {
316: reviewerStyle = DISPLAY_INLINE;
317: personLookupStyle = DISPLAY_INLINE;
318: workgroupLookupStyle = DISPLAY_NONE;
319: roleAreaStyle = DISPLAY_NONE;
320: }
321: if (EdenConstants.RULE_RESPONSIBILITY_WORKGROUP_ID
322: .equals(getRuleResponsibilityType())) {
323: reviewerStyle = DISPLAY_INLINE;
324: personLookupStyle = DISPLAY_NONE;
325: workgroupLookupStyle = DISPLAY_INLINE;
326: roleAreaStyle = DISPLAY_NONE;
327: }
328: if (EdenConstants.RULE_RESPONSIBILITY_ROLE_ID
329: .equals(getRuleResponsibilityType())) {
330: reviewerStyle = DISPLAY_NONE;
331: personLookupStyle = DISPLAY_NONE;
332: workgroupLookupStyle = DISPLAY_NONE;
333: roleAreaStyle = DISPLAY_INLINE;
334: }
335: loadWebValues();
336: if (delegationRulesMaterialized) {
337: for (Iterator iterator = getDelegationRules().iterator(); iterator
338: .hasNext();) {
339: RuleDelegation delegation = (RuleDelegation) iterator
340: .next();
341: ((WebRuleBaseValues) delegation
342: .getDelegationRuleBaseValues())
343: .establishRequiredState();
344: }
345: }
346: }
347:
348: public void validateResponsibility(String keyPrefix,
349: ActionErrors errors) {
350: if (EdenConstants.RULE_RESPONSIBILITY_WORKFLOW_ID
351: .equals(getRuleResponsibilityType())) {
352: boolean invalidUser = Utilities.isEmpty(getReviewer());
353: if (!invalidUser) {
354: try {
355: WorkflowUser user = getUserService()
356: .getWorkflowUser(
357: new AuthenticationUserId(
358: getReviewer()));
359: setRuleResponsibilityName(user.getWorkflowUserId()
360: .getWorkflowId());
361: } catch (EdenUserNotFoundException e) {
362: invalidUser = true;
363: }
364: }
365: if (invalidUser) {
366: errors.add(keyPrefix + "reviewer", new ActionMessage(
367: "routetemplate.ruleservice.user.invalid"));
368: }
369: } else if (EdenConstants.RULE_RESPONSIBILITY_WORKGROUP_ID
370: .equals(getRuleResponsibilityType())) {
371: boolean invalidWorkgroup = Utilities.isEmpty(getReviewer());
372: ;
373: if (!invalidWorkgroup) {
374: Workgroup workgroup = getWorkgroupService()
375: .getWorkgroup(new GroupNameId(getReviewer()));
376: if (workgroup == null) {
377: invalidWorkgroup = true;
378: } else {
379: setRuleResponsibilityName(workgroup
380: .getWorkflowGroupId().getGroupId()
381: .toString());
382: }
383: }
384: if (invalidWorkgroup) {
385: errors.add(keyPrefix + "reviewer", new ActionMessage(
386: "routetemplate.ruleservice.workgroup.invalid"));
387: }
388:
389: } else if (EdenConstants.RULE_RESPONSIBILITY_ROLE_ID
390: .equals(getRuleResponsibilityType())) {
391: setRuleResponsibilityName(getRoleReviewer());
392: }
393:
394: int delIndex = 0;
395: for (Iterator respIterator = getDelegationRules().iterator(); respIterator
396: .hasNext();) {
397: String delPrefix = keyPrefix + "delegationRule[" + delIndex
398: + "].delegationRuleBaseValues.";
399: RuleDelegation ruleDelegation = (RuleDelegation) respIterator
400: .next();
401: ((WebRuleBaseValues) ruleDelegation
402: .getDelegationRuleBaseValues()).validateRule(
403: delPrefix, errors);
404: }
405: }
406:
407: public void edit(RuleResponsibility ruleResponsibility)
408: throws Exception {
409: load(ruleResponsibility);
410: initialize();
411: }
412:
413: public void load(RuleResponsibility ruleResponsibility)
414: throws Exception {
415: PropertyUtils.copyProperties(this , ruleResponsibility);
416: injectWebMembers();
417: }
418:
419: public void loadDelegations() throws Exception {
420: fetchDelegations();
421:
422: for (Iterator iterator = getDelegationRules().iterator(); iterator
423: .hasNext();) {
424: RuleDelegation ruleDelegation = (RuleDelegation) iterator
425: .next();
426: WebRuleBaseValues webRule = new WebRuleBaseValues();
427: webRule.edit(ruleDelegation.getDelegationRuleBaseValues());
428: ruleDelegation.setDelegationRuleBaseValues(webRule);
429: }
430: delegationRulesMaterialized = true;
431: populatePreviousVersionIds();
432: }
433:
434: public void populatePreviousVersionIds() {
435: if (delegationRulesMaterialized) {
436: for (Iterator iterator = getDelegationRules().iterator(); iterator
437: .hasNext();) {
438: RuleDelegation delegation = (RuleDelegation) iterator
439: .next();
440: ((WebRuleBaseValues) delegation
441: .getDelegationRuleBaseValues())
442: .populatePreviousVersionIds();
443: }
444: }
445: }
446:
447: private void fetchDelegations() {
448: if (getRuleResponsibilityKey() != null) {
449: RuleResponsibility responsibility = getRuleService()
450: .findByRuleResponsibilityId(
451: getRuleResponsibilityKey());
452: if (responsibility == null) {
453: return;
454: }
455: getDelegationRules().addAll(
456: responsibility.getDelegationRules());
457: }
458: }
459:
460: public void prepareHiddenDelegationsForRoute() {
461: if (showDelegations) {
462: return;
463: }
464:
465: fetchDelegations();
466:
467: for (Iterator iter = getDelegationRules().iterator(); iter
468: .hasNext();) {
469: RuleDelegation delegation = (RuleDelegation) iter.next();
470: delegation.setDelegateRuleId(null);
471: delegation.setLockVerNbr(null);
472: delegation.setRuleDelegationId(null);
473: delegation.setRuleResponsibility(this );
474: delegation.setRuleResponsibilityId(null);
475:
476: RuleBaseValues rule = delegation
477: .getDelegationRuleBaseValues();
478: rule.setLockVerNbr(null);
479: rule.setPreviousVersionId(rule.getRuleBaseValuesId());
480: rule.setRouteHeaderId(null);
481: rule.setRuleBaseValuesId(null);
482:
483: for (Iterator iterator = rule.getResponsibilities()
484: .iterator(); iterator.hasNext();) {
485: RuleResponsibility responsibility = (RuleResponsibility) iterator
486: .next();
487: responsibility.setLockVerNbr(null);
488: responsibility.setRuleBaseValuesId(null);
489: responsibility.setRuleBaseValues(rule);
490: responsibility.setRuleResponsibilityKey(null);
491: }
492:
493: for (Iterator iterator = rule.getRuleExtensions()
494: .iterator(); iterator.hasNext();) {
495: RuleExtension extension = (RuleExtension) iterator
496: .next();
497: extension.setLockVerNbr(null);
498: extension.setRuleBaseValues(rule);
499: extension.setRuleBaseValuesId(null);
500: extension.setRuleExtensionId(null);
501:
502: for (Iterator iter2 = extension.getExtensionValues()
503: .iterator(); iter2.hasNext();) {
504: RuleExtensionValue value = (RuleExtensionValue) iter2
505: .next();
506: value.setExtension(extension);
507: value.setLockVerNbr(null);
508: value.setRuleExtensionId(null);
509: value.setRuleExtensionValueId(null);
510: }
511: }
512: }
513: }
514:
515: public boolean isHasDelegateRuleTemplate() {
516: return hasDelegateRuleTemplate;
517: }
518:
519: public void setHasDelegateRuleTemplate(
520: boolean hasDelegateRuleTemplate) {
521: this .hasDelegateRuleTemplate = hasDelegateRuleTemplate;
522: }
523:
524: private WorkgroupService getWorkgroupService() {
525: return (WorkgroupService) KEWServiceLocator
526: .getService(KEWServiceLocator.WORKGROUP_SRV);
527: }
528:
529: private UserService getUserService() {
530: return (UserService) KEWServiceLocator
531: .getService(KEWServiceLocator.USER_SERVICE);
532: }
533:
534: private RuleService getRuleService() {
535: return (RuleService) KEWServiceLocator
536: .getService(KEWServiceLocator.RULE_SERVICE);
537: }
538:
539: /**
540: * Just a little dynamic proxy to keep us from establishing required state
541: * on the delegation rules if they haven't been materialized from the
542: * database yet (they are currenty proxied by OJB)
543: *
544: * @author ewestfal
545: */
546: private class DelegationRulesProxy implements InvocationHandler,
547: java.io.Serializable {
548:
549: private static final long serialVersionUID = 7046323200221509473L;
550:
551: private List delegationRules;
552:
553: public DelegationRulesProxy(List delegationRules) {
554: this .delegationRules = delegationRules;
555: }
556:
557: public Object invoke(Object proxy, Method m, Object[] args)
558: throws Throwable {
559: if (!delegationRulesMaterialized
560: && !m.getName().equals("isEmpty")
561: && !m.getName().equals("size")) {
562: for (Iterator iterator = delegationRules.iterator(); iterator
563: .hasNext();) {
564: RuleDelegation ruleDelegation = (RuleDelegation) iterator
565: .next();
566: WebRuleBaseValues webRule = new WebRuleBaseValues();
567: webRule.load(ruleDelegation
568: .getDelegationRuleBaseValues());
569: webRule.establishRequiredState();
570: ruleDelegation.setDelegationRuleBaseValues(webRule);
571: }
572: delegationRulesMaterialized = true;
573:
574: }
575: return m.invoke(delegationRules, args);
576: }
577:
578: }
579:
580: }
|