001: /*
002: * Copyright 2005-2007 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;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.apache.commons.lang.StringUtils;
023: import org.junit.Test;
024: import org.kuali.workflow.test.WorkflowTestCase;
025:
026: import edu.iu.uis.eden.EdenConstants;
027: import edu.iu.uis.eden.KEWServiceLocator;
028: import edu.iu.uis.eden.applicationconstants.ApplicationConstant;
029: import edu.iu.uis.eden.clientapp.WorkflowDocument;
030: import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
031: import edu.iu.uis.eden.routetemplate.web.Rule2Form;
032: import edu.iu.uis.eden.routetemplate.web.WebRuleBaseValues;
033: import edu.iu.uis.eden.routetemplate.web.WebRuleResponsibility;
034: import edu.iu.uis.eden.user.AuthenticationUserId;
035: import edu.iu.uis.eden.user.WorkflowUser;
036:
037: /**
038: * Tests adding a delegation rule
039: * @author Aaron Hamid (arh14 at cornell dot edu)
040: */
041: public class AddRuleDelegationTest extends WorkflowTestCase {
042:
043: protected void loadTestData() throws Exception {
044: loadXmlFile("AddRuleDelegationTestData.xml");
045: }
046:
047: /**
048: * Tests adding a delegation rule. The implementation is mostly a cut-and-paste copy of
049: * createDelegateRule and routeRule methods from DelegatRule2Action Struts action.
050: */
051: @Test
052: public void testAddRuleDelegation() throws Exception {
053: // set some application constants that aren't defined in test data, and will cause NPEs if not defined
054: KEWServiceLocator.getApplicationConstantsService().save(
055: new ApplicationConstant(
056: EdenConstants.RULE_DELEGATE_LIMIT_KEY, "1000"));
057: KEWServiceLocator
058: .getApplicationConstantsService()
059: .save(
060: new ApplicationConstant(
061: EdenConstants.RULE_CHANGE_AR_GENERATION_KEY,
062: EdenConstants.YES_RULE_CHANGE_AR_GENERATION_VALUE));
063: KEWServiceLocator
064: .getApplicationConstantsService()
065: .save(
066: new ApplicationConstant(
067: EdenConstants.DELEGATE_CHANGE_AR_GENERATION_KEY,
068: EdenConstants.YES_DELEGATE_CHANGE_AR_GENERATION_VALUE));
069:
070: final String A_WF_ADMIN_USERNAME = "ewestfal";
071: final String DELEGATE_USER = "user2";
072: final String DELEGATE_USER_WF_ID = "1002";
073:
074: final String DOCTYPE = "AddDelegationTest_DocType";
075: final String RULE_TEMPLATE = "AddDelegationTest_RuleTemplate";
076: final String DELEGATION_TEMPLATE = "AddDelegationTest_DelegationTemplate";
077:
078: List<RuleBaseValues> existingRules = KEWServiceLocator
079: .getRuleService()
080: .fetchAllCurrentRulesForTemplateDocCombination(
081: RULE_TEMPLATE, DOCTYPE);
082: assertNotNull(existingRules);
083: assertEquals(1, existingRules.size());
084:
085: RuleBaseValues originalRule = existingRules.get(0);
086:
087: List<RuleResponsibility> originalResps = originalRule
088: .getResponsibilities();
089: assertEquals(1, originalResps.size());
090:
091: RuleResponsibility originalResp = originalResps.get(0);
092: Long originalRuleResponsibilityKey = originalResp
093: .getRuleResponsibilityKey();
094:
095: RuleTemplate rt = KEWServiceLocator.getRuleTemplateService()
096: .findByRuleTemplateName(DELEGATION_TEMPLATE);
097: assertNotNull(rt);
098: assertNotNull(rt.getRuleTemplateId());
099: assertFalse(StringUtils.isEmpty(rt.getName()));
100:
101: Rule2Form ruleForm = new Rule2Form();
102: ruleForm.setParentRule(new WebRuleBaseValues(originalRule));
103: ruleForm.getRuleCreationValues().setCreating(true);
104: ruleForm.getRuleCreationValues().setRuleId(
105: originalRule.getRuleBaseValuesId());
106: ruleForm.getRuleCreationValues().setManualDelegationTemplate(
107: false);
108: ruleForm.getRuleCreationValues().setRuleTemplateId(
109: rt.getRuleTemplateId());
110: ruleForm.getRuleCreationValues().setRuleTemplateName(
111: rt.getName());
112: ruleForm.getRuleCreationValues().setRuleResponsibilityKey(
113: originalRuleResponsibilityKey);
114:
115: WebRuleBaseValues rule = new WebRuleBaseValues();
116:
117: {
118: RuleBaseValues defaultRule = KEWServiceLocator
119: .getRuleService().findDefaultRuleByRuleTemplateId(
120: originalRule.getRuleTemplate()
121: .getDelegationTemplateId());
122: if (defaultRule != null) {
123: List ruleDelegations = KEWServiceLocator
124: .getRuleDelegationService()
125: .findByDelegateRuleId(
126: defaultRule.getRuleBaseValuesId());
127: defaultRule.setActivationDate(null);
128: defaultRule.setCurrentInd(null);
129: defaultRule.setDeactivationDate(null);
130: defaultRule.setDocTypeName(null);
131: defaultRule.setLockVerNbr(null);
132: defaultRule.setRuleBaseValuesId(null);
133: defaultRule.setTemplateRuleInd(Boolean.FALSE);
134: defaultRule.setVersionNbr(null);
135: rule.load(defaultRule);
136:
137: if (ruleDelegations != null
138: && !ruleDelegations.isEmpty()) {
139: RuleDelegation defaultDelegation = (RuleDelegation) ruleDelegations
140: .get(0);
141: ruleForm.getRuleDelegation().setDelegationType(
142: defaultDelegation.getDelegationType());
143: }
144: }
145:
146: rule.setDocTypeName(ruleForm.getParentRule()
147: .getDocTypeName());
148: rule.setRuleTemplateId(ruleForm.getParentRule()
149: .getRuleTemplate().getDelegationTemplateId());
150: rule.setRuleTemplateName(ruleForm.getParentRule()
151: .getRuleTemplate().getDelegateTemplateName());
152: // have to set the ruletemplate object explicitly...the struts action must do this itself somewhere...
153: rule.setRuleTemplate(KEWServiceLocator
154: .getRuleTemplateService().findByRuleTemplateId(
155: rule.getRuleTemplateId()));
156: rule.setDelegateRule(Boolean.TRUE);
157: rule.loadFieldsWithDefaultValues();
158: rule.createNewRuleResponsibility();
159:
160: ruleForm.getRuleDelegation().setDelegationRuleBaseValues(
161: rule);
162: ruleForm.getMyRules().addRule(rule);
163:
164: //createFlexDoc(request, ruleForm, ruleForm.getMyRules().getRules());
165: if (ruleForm.getFlexDoc() == null) {
166: // rule2Form.setFlexDoc(new WorkflowDocument(EdenConstants.RULE_DOCUMENT_NAME, getUserSession(request).getWorkflowUser(), EdenConstants.EDEN_APP_CODE));
167: String ruleDocTypeName = KEWServiceLocator
168: .getRuleService().getRuleDocmentTypeName(
169: ruleForm.getMyRules().getRules());
170: ruleForm.setFlexDoc(new WorkflowDocument(
171: new NetworkIdVO(A_WF_ADMIN_USERNAME),
172: ruleDocTypeName));
173: ruleForm.setDocId(ruleForm.getFlexDoc()
174: .getRouteHeaderId());
175: ruleForm.establishVisibleActionRequestCds();
176: }
177:
178: ruleForm.getShowHide().append().append();
179: ruleForm.setEditingDelegate(true);
180: rule.establishRequiredState();
181: }
182: // ---
183:
184: // ok, "update the form"
185: ruleForm.getRuleCreationValues().setCreating(false);
186:
187: MyRules2 rules = ruleForm.getMyRules();
188:
189: WebRuleBaseValues delegateRule = rules.getRule(0);
190: delegateRule.setDescription("A delegate rule");
191: WebRuleResponsibility resp = (WebRuleResponsibility) delegateRule
192: .getResponsibility(0);
193: resp.setNumberOfDelegations(0);
194: resp.setShowDelegations(false);
195: resp.setDelegationRulesMaterialized(false);
196: resp.setHasDelegateRuleTemplate(false);
197: resp.setReviewer(DELEGATE_USER);
198: resp
199: .setRuleResponsibilityType(EdenConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
200: resp.setRuleResponsibilityName(DELEGATE_USER_WF_ID); // This is taken as a WorkflowId (not NetworkId, etc.)
201:
202: delegateRule.setCurrentInd(Boolean.FALSE);
203:
204: // get the parent rule of the delegation-rule being submitted
205: RuleBaseValues parentRule = KEWServiceLocator.getRuleService()
206: .findRuleBaseValuesById(
207: ruleForm.getParentRule().getRuleBaseValuesId());
208: // new delegation rule
209: // if (delegateRule.getPreviousVersionId() == null) {
210:
211: Long previousVersionId = parentRule.getRuleBaseValuesId();
212:
213: RuleResponsibility delegateResponsibility = parentRule
214: .getResponsibility(ruleForm.getRuleCreationValues()
215: .getRuleResponsibilityKey());
216:
217: // get the RuleDelegation being submitted and connect the
218: // delegate rule and the delegat responsibility
219: RuleDelegation ruleDelegation = ruleForm.getRuleDelegation();
220: ruleDelegation.setDelegationRuleBaseValues(delegateRule);
221: ruleDelegation.setRuleResponsibility(delegateResponsibility);
222:
223: // if the delegateRule itself has a previous version, then iterate
224: // through the rules associated with the delegate responsibility and
225: // make sure that the previous, delegate rule version is no longer
226: // associated with the responsibility
227: if (delegateRule.getPreviousVersionId() != null) {
228: for (Iterator iter = delegateResponsibility
229: .getDelegationRules().iterator(); iter.hasNext();) {
230: RuleDelegation delegation = (RuleDelegation) iter
231: .next();
232: if (delegation.getDelegateRuleId().longValue() == delegateRule
233: .getPreviousVersionId().longValue()) {
234: iter.remove();
235: break;
236: }
237: }
238: }
239:
240: // add the new rule delegation to the responsibility
241: delegateResponsibility.getDelegationRules().add(ruleDelegation);
242:
243: // make a copy of the parent rule, which will be the new version
244: // of the parent rule, that contains this new delegation
245: // XXX
246: parentRule = (RuleBaseValues) parentRule.copy(false);
247: parentRule.setPreviousVersionId(previousVersionId);
248: // }
249:
250: /*
251: * RuleBaseValues parentRule = getRuleService().findRuleBaseValuesById(ruleForm.getParentRule().getRuleBaseValuesId()); Long previousVersionId = parentRule.getRuleBaseValuesId(); RuleResponsibility delegateResponsibility = parentRule.getResponsibility(ruleForm.getRuleCreationValues().getRuleResponsibilityKey()); RuleDelegation ruleDelegation = ruleForm.getRuleDelegation(); ruleDelegation.setDelegationRuleBaseValues(delegateRule); ruleDelegation.setRuleResponsibility(delegateResponsibility); // replace the existing delegation in the parent rule boolean foundDelegation = false; int delIndex = 0; for (Iterator iterator = delegateResponsibility.getDelegationRules().iterator(); iterator.hasNext();) { RuleDelegation delegation = (RuleDelegation) iterator.next(); if (delegation.getDelegateRuleId().equals(delegateRule.getPreviousVersionId())) { iterator.remove(); foundDelegation = true; break; } delIndex++; } if (foundDelegation) {
252: * delegateResponsibility.getDelegationRules().add(delIndex, ruleDelegation); } else { delegateResponsibility.getDelegationRules().add(ruleDelegation); } parentRule = (RuleBaseValues) parentRule.copy(false); parentRule.setPreviousVersionId(previousVersionId);
253: */
254:
255: // null out the responsibility keys for the delegate rule
256: for (Iterator iterator = delegateRule.getResponsibilities()
257: .iterator(); iterator.hasNext();) {
258: RuleResponsibility responsibility = (RuleResponsibility) iterator
259: .next();
260: responsibility.setRuleResponsibilityKey(null);
261: }
262:
263: WorkflowUser user = KEWServiceLocator.getUserService()
264: .getWorkflowUser(
265: new AuthenticationUserId(A_WF_ADMIN_USERNAME));
266: KEWServiceLocator.getRuleService().routeRuleWithDelegate(
267: ruleForm.getDocId(), parentRule, delegateRule, user,
268: ruleForm.getAnnotation(), true);
269:
270: }
271: }
|