1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36
37
38 public final class RuleTestUtils {
39
40 private RuleTestUtils() {
41 throw new UnsupportedOperationException("do not call");
42 }
43
44
45
46
47
48
49
50
51
52 public static RuleDelegationBo createDelegationToUser(String docType, String ruleTemplate, String delegateUser) {
53
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
63
64 return createRuleDelegationToUser(originalRule, originalResp, delegatePrincipal);
65 }
66
67
68
69
70
71
72
73
74
75 public static RuleDelegationBo createDelegationToGroup(String docType, String ruleTemplate, String delegateGroupId) {
76
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
85
86 return createRuleDelegationToGroup(originalRule, originalResp, delegateGroup);
87 }
88
89
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
101
102
103
104
105
106
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
114
115
116
117
118
119
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
127
128
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 }