View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.rule;
17  
18  import org.kuali.rice.core.api.delegation.DelegationType;
19  import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
20  import org.kuali.rice.kew.service.KEWServiceLocator;
21  import org.kuali.rice.kew.api.KewApiConstants;
22  import org.kuali.rice.kew.workgroup.GroupNameId;
23  import org.kuali.rice.kim.api.group.Group;
24  import org.kuali.rice.kim.api.identity.principal.Principal;
25  import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
26  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
27  
28  import java.util.List;
29  
30  import static org.junit.Assert.assertTrue;
31  
32  /**
33   * This is a description of what this class does - gilesp don't forget to fill this in. 
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   */
38  public final class RuleTestUtils {
39  	
40  	private RuleTestUtils() {
41  		throw new UnsupportedOperationException("do not call");
42  	}
43  	
44  	/**
45  	 * <p>This method will create a delegate rule for the rule (assumed to be cardinality of 1) specified by the given
46  	 * docType and ruleTemplate. 
47  	 * 
48  	 * <p>As a side effect, active documents of this type will be requeued for workflow processing.
49  	 * 
50  	 * @param delegateUser the user who will be the delegate
51  	 */
52  	public static RuleDelegationBo createDelegationToUser(String docType, String ruleTemplate, String delegateUser) {
53  		// create and save a rule delegation 
54      	RuleBaseValues originalRule = getRule(docType, ruleTemplate);
55      	List<RuleResponsibilityBo> responsibilities = originalRule.getRuleResponsibilities();
56      	assertTrue("assuming there is 1 responsibility", responsibilities != null && responsibilities.size() == 1);
57      	
58      	RuleResponsibilityBo originalResp = responsibilities.get(0);
59  
60      	Principal delegatePrincipal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(delegateUser);
61  
62  		// save the new rule delegation
63  		// this *SHOULD* requeue
64  		return createRuleDelegationToUser(originalRule, originalResp, delegatePrincipal);
65  	}
66  
67  	/**
68  	 * <p>This method will create a delegate rule for the rule (assumed to be cardinality of 1) specified by the given
69  	 * docType and ruleTemplate. 
70  	 * 
71  	 * <p>As a side effect, active documents of this type will be requeued for workflow processing.
72  	 * 
73  	 * @param delegateUser the user who will be the delegate
74  	 */
75  	public static RuleDelegationBo createDelegationToGroup(String docType, String ruleTemplate, String delegateGroupId) {
76  		// create and save a rule delegation 
77      	RuleBaseValues originalRule = getRule(docType, ruleTemplate);
78      	List<RuleResponsibilityBo> responsibilities = originalRule.getRuleResponsibilities();
79      	assertTrue("assuming there is 1 responsibility", responsibilities != null && responsibilities.size() == 1);
80      	
81      	RuleResponsibilityBo originalResp = responsibilities.get(0);
82      	Group delegateGroup = KEWServiceLocator.getIdentityHelperService().getGroup(new GroupNameId(delegateGroupId));
83      	
84  		// save the new rule delegation
85  		// this *SHOULD* requeue
86  		return createRuleDelegationToGroup(originalRule, originalResp, delegateGroup);
87  	}
88  	/**
89  	 * This method gets a rule from a docType / ruleTemplate combo
90  	 */
91  	public static RuleBaseValues getRule(String docType, String ruleTemplate) {
92  		List rules = KEWServiceLocator.getRuleService().fetchAllCurrentRulesForTemplateDocCombination(ruleTemplate, docType);
93      	assertTrue("assuming there is 1 rule", rules != null && rules.size() == 1);
94      	
95      	RuleBaseValues originalRule = (RuleBaseValues)rules.get(0);
96  		return originalRule;
97  	}
98  
99  	/**
100 	 * <p>This method creates and saves a rule delegation
101 	 * 
102 	 * <p>As a side effect, active documents of this type will be requeued for workflow processing.
103 	 * 
104 	 * @param parentRule
105 	 * @param parentResponsibility
106 	 * @param delegatePrincipal
107 	 */
108 	public static RuleDelegationBo createRuleDelegationToUser(RuleBaseValues parentRule, RuleResponsibilityBo parentResponsibility, PrincipalContract delegatePrincipal) {
109 		return createRuleDelegation(parentRule, parentResponsibility, delegatePrincipal.getPrincipalId(), KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
110 	}
111 	
112 	/**
113 	 * <p>This method creates and saves a rule delegation
114 	 * 
115 	 * <p>As a side effect, active documents of this type will be requeued for workflow processing.
116 	 * 
117 	 * @param parentRule
118 	 * @param parentResponsibility
119 	 * @param delegateGroup
120 	 */
121 	public static RuleDelegationBo createRuleDelegationToGroup(RuleBaseValues parentRule, RuleResponsibilityBo parentResponsibility, Group delegateGroup) {
122 		return createRuleDelegation(parentRule, parentResponsibility, delegateGroup.getId(), KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
123 	}
124 	
125 	/**
126 	 * <p>This method creates and saves a rule delegation
127 	 * 
128 	 * <p>As a side effect, active documents of this type will be requeued for workflow processing.
129 	 */
130 	private static RuleDelegationBo createRuleDelegation(RuleBaseValues parentRule, RuleResponsibilityBo parentResponsibility, String delegateId, String groupTypeCode) {
131     	RuleTemplateBo delegationTemplate = parentRule.getRuleTemplate();
132 		RuleDelegationBo ruleDelegation = new RuleDelegationBo();
133 		ruleDelegation.setResponsibilityId(parentResponsibility.getResponsibilityId());
134 		ruleDelegation.setDelegationType(DelegationType.PRIMARY);
135 		RuleBaseValues rule = new RuleBaseValues();
136 		ruleDelegation.setDelegationRule(rule);
137 		rule.setDelegateRule(true);
138 		rule.setActive(true);
139 		rule.setCurrentInd(true);
140 		rule.setDocTypeName(parentRule.getDocTypeName());
141 		rule.setRuleTemplateId(delegationTemplate.getDelegationTemplateId());
142 		rule.setRuleTemplate(delegationTemplate);
143 		rule.setDescription("Description of this delegate rule");
144 		rule.setForceAction(true);
145 		RuleResponsibilityBo delegationResponsibility = new RuleResponsibilityBo();
146 		rule.getRuleResponsibilities().add(delegationResponsibility);
147 		delegationResponsibility.setRuleBaseValues(rule);
148 		delegationResponsibility.setRuleResponsibilityName(delegateId);
149 		delegationResponsibility.setRuleResponsibilityType(groupTypeCode);
150 		KEWServiceLocator.getRuleService().saveRuleDelegation(ruleDelegation, true);
151 		return ruleDelegation;
152 	}
153 }