001 /** 002 * Copyright 2005-2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.kew.rule; 017 018 import org.kuali.rice.core.api.delegation.DelegationType; 019 import org.kuali.rice.kew.rule.bo.RuleTemplateBo; 020 import org.kuali.rice.kew.service.KEWServiceLocator; 021 import org.kuali.rice.kew.api.KewApiConstants; 022 import org.kuali.rice.kew.workgroup.GroupNameId; 023 import org.kuali.rice.kim.api.group.Group; 024 import org.kuali.rice.kim.api.identity.principal.Principal; 025 import org.kuali.rice.kim.api.identity.principal.PrincipalContract; 026 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 027 028 import java.util.List; 029 030 import static org.junit.Assert.assertTrue; 031 032 /** 033 * This is a description of what this class does - gilesp don't forget to fill this in. 034 * 035 * @author Kuali Rice Team (rice.collab@kuali.org) 036 * 037 */ 038 public final class RuleTestUtils { 039 040 private RuleTestUtils() { 041 throw new UnsupportedOperationException("do not call"); 042 } 043 044 /** 045 * <p>This method will create a delegate rule for the rule (assumed to be cardinality of 1) specified by the given 046 * docType and ruleTemplate. 047 * 048 * <p>As a side effect, active documents of this type will be requeued for workflow processing. 049 * 050 * @param delegateUser the user who will be the delegate 051 */ 052 public static RuleDelegationBo createDelegationToUser(String docType, String ruleTemplate, String delegateUser) { 053 // create and save a rule delegation 054 RuleBaseValues originalRule = getRule(docType, ruleTemplate); 055 List<RuleResponsibilityBo> responsibilities = originalRule.getRuleResponsibilities(); 056 assertTrue("assuming there is 1 responsibility", responsibilities != null && responsibilities.size() == 1); 057 058 RuleResponsibilityBo originalResp = responsibilities.get(0); 059 060 Principal delegatePrincipal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(delegateUser); 061 062 // save the new rule delegation 063 // this *SHOULD* requeue 064 return createRuleDelegationToUser(originalRule, originalResp, delegatePrincipal); 065 } 066 067 /** 068 * <p>This method will create a delegate rule for the rule (assumed to be cardinality of 1) specified by the given 069 * docType and ruleTemplate. 070 * 071 * <p>As a side effect, active documents of this type will be requeued for workflow processing. 072 * 073 * @param delegateUser the user who will be the delegate 074 */ 075 public static RuleDelegationBo createDelegationToGroup(String docType, String ruleTemplate, String delegateGroupId) { 076 // create and save a rule delegation 077 RuleBaseValues originalRule = getRule(docType, ruleTemplate); 078 List<RuleResponsibilityBo> responsibilities = originalRule.getRuleResponsibilities(); 079 assertTrue("assuming there is 1 responsibility", responsibilities != null && responsibilities.size() == 1); 080 081 RuleResponsibilityBo originalResp = responsibilities.get(0); 082 Group delegateGroup = KEWServiceLocator.getIdentityHelperService().getGroup(new GroupNameId(delegateGroupId)); 083 084 // save the new rule delegation 085 // this *SHOULD* requeue 086 return createRuleDelegationToGroup(originalRule, originalResp, delegateGroup); 087 } 088 /** 089 * This method gets a rule from a docType / ruleTemplate combo 090 */ 091 public static RuleBaseValues getRule(String docType, String ruleTemplate) { 092 List rules = KEWServiceLocator.getRuleService().fetchAllCurrentRulesForTemplateDocCombination(ruleTemplate, docType); 093 assertTrue("assuming there is 1 rule", rules != null && rules.size() == 1); 094 095 RuleBaseValues originalRule = (RuleBaseValues)rules.get(0); 096 return originalRule; 097 } 098 099 /** 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 }