View Javadoc

1   /**
2    * Copyright 2005-2013 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.apache.commons.lang.StringUtils;
19  import org.hibernate.annotations.GenericGenerator;
20  import org.hibernate.annotations.Parameter;
21  import org.kuali.rice.core.api.delegation.DelegationType;
22  import org.kuali.rice.kew.api.rule.RuleDelegationContract;
23  import org.kuali.rice.kew.doctype.bo.DocumentType;
24  import org.kuali.rice.kew.service.KEWServiceLocator;
25  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
26  import org.kuali.rice.kim.impl.group.GroupBo;
27  import org.kuali.rice.kim.impl.identity.PersonImpl;
28  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
29  
30  import javax.persistence.CascadeType;
31  import javax.persistence.Column;
32  import javax.persistence.Entity;
33  import javax.persistence.FetchType;
34  import javax.persistence.GeneratedValue;
35  import javax.persistence.Id;
36  import javax.persistence.JoinColumn;
37  import javax.persistence.OneToOne;
38  import javax.persistence.Table;
39  import javax.persistence.Transient;
40  
41  
42  /**
43   * A model bean representing the delegation of a rule from a responsibility to
44   * another rule.  Specifies the delegation type which can be either
45   * {@link {@link DelegationType#PRIMARY} or {@link DelegationType#SECONDARY}.
46   *
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  @Entity
50  @Table(name="KREW_DLGN_RSP_T")
51  //@Sequence(name="KREW_RTE_TMPL_S", property="ruleDelegationId")
52  public class RuleDelegationBo extends PersistableBusinessObjectBase implements RuleDelegationContract {
53  
54  	private static final long serialVersionUID = 7989203310473741293L;
55  	@Id
56  	@GeneratedValue(generator="KREW_RTE_TMPL_S")
57  	@GenericGenerator(name="KREW_RTE_TMPL_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
58  			@Parameter(name="sequence_name",value="KREW_RTE_TMPL_S"),
59  			@Parameter(name="value_column",value="id")
60  	})
61  	@Column(name="DLGN_RULE_ID")
62  	private String ruleDelegationId;
63      @Column(name="RSP_ID")
64  	private String responsibilityId;
65      @Column(name="DLGN_RULE_BASE_VAL_ID", insertable=false, updatable=false)
66  	private String delegateRuleId;
67      @Column(name="DLGN_TYP")
68      private String delegationTypeCode = DelegationType.PRIMARY.getCode();
69  
70      @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
71  	@JoinColumn(name="DLGN_RULE_BASE_VAL_ID")
72  	private RuleBaseValues delegationRule;
73  //    @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
74  //	@JoinColumn(name="RULE_RSP_ID")
75  //	private RuleResponsibility ruleResponsibility;
76  
77      @Transient
78      private String groupReviewerName;
79      @Transient
80      private String groupReviewerNamespace;
81      @Transient
82      private String personReviewer;
83      @Transient
84      private String personReviewerType;
85  
86      public RuleDelegationBo() {
87      }
88  
89      public Object copy(boolean preserveKeys) {
90          RuleDelegationBo clone = new RuleDelegationBo();
91          if (ruleDelegationId != null && preserveKeys) {
92              clone.setRuleDelegationId(ruleDelegationId);
93          }
94          clone.setDelegationRule(delegationRule);
95          clone.setDelegateRuleId(delegationRule.getId());
96          if (delegationTypeCode != null) {
97              clone.setDelegationType(DelegationType.fromCode(delegationTypeCode));
98          }
99          return clone;
100     }
101 
102     public String getDelegateRuleId() {
103         return delegateRuleId;
104     }
105     public void setDelegateRuleId(String delegateRuleId) {
106         this.delegateRuleId = delegateRuleId;
107     }
108 
109     @Override
110     public RuleBaseValues getDelegationRule() {
111         return delegationRule;
112     }
113 
114     public RuleBaseValues getDelegationRuleBaseValues() {
115         return delegationRule;
116     }
117 
118     public void setDelegationRuleBaseValues(RuleBaseValues delegationRuleBaseValues) {
119         this.delegationRule = delegationRuleBaseValues;
120     }
121 
122     public void setDelegationRule(RuleBaseValues delegationRule) {
123         this.delegationRule = delegationRule;
124     }
125 
126     /**
127      * Setter for type code preserved for DD
128      * @param delegationTypeCode the DelegationType code
129      */
130     public void setDelegationTypeCode(String delegationTypeCode) {
131         DelegationType.fromCode(delegationTypeCode);
132         this.delegationTypeCode = delegationTypeCode;
133     }
134 
135     /**
136      * Getter for type code preserved for DD
137      * @return the DelegationType code
138      */
139     public String getDelegationTypeCode() {
140         return delegationTypeCode;
141     }
142 
143     @Override
144     public DelegationType getDelegationType() {
145         return DelegationType.fromCode(delegationTypeCode);
146     }
147     public void setDelegationType(DelegationType delegationType) {
148         this.delegationTypeCode = delegationType.getCode();
149     }
150     public String getRuleDelegationId() {
151         return ruleDelegationId;
152     }
153     public void setRuleDelegationId(String ruleDelegationId) {
154         this.ruleDelegationId = ruleDelegationId;
155     }
156 
157     /**
158      * Returns the most recent RuleResponsibility for the responsibility
159      * id on this RuleDelegation.
160      */
161     public RuleResponsibilityBo getRuleResponsibility() {
162     	if ( getResponsibilityId() == null ) {
163     		return null;
164     	}
165     	return KEWServiceLocator.getRuleService().findRuleResponsibility(getResponsibilityId());
166     }
167 
168     public DocumentType getDocumentType() {
169         return this.getDelegationRule().getDocumentType();
170     }
171 
172     public String getResponsibilityId() {
173         return responsibilityId;
174     }
175     public void setResponsibilityId(String ruleResponsibilityId) {
176         this.responsibilityId = ruleResponsibilityId;
177     }
178 
179     public String getGroupReviewerName() {
180         return this.groupReviewerName;
181     }
182 
183     public String getGroupReviewerNamespace() {
184         return this.groupReviewerNamespace;
185     }
186 
187     public String getPersonReviewer() {
188         return this.personReviewer;
189     }
190 
191     public void setGroupReviewerName(String groupReviewerName) {
192         this.groupReviewerName = groupReviewerName;
193     }
194 
195     public void setGroupReviewerNamespace(String groupReviewerNamespace) {
196         this.groupReviewerNamespace = groupReviewerNamespace;
197     }
198 
199     public void setPersonReviewer(String personReviewer) {
200         this.personReviewer = personReviewer;
201     }
202 
203     public String getPersonReviewerType() {
204         return this.personReviewerType;
205     }
206 
207     public void setPersonReviewerType(String personReviewerType) {
208         this.personReviewerType = personReviewerType;
209     }
210 
211     public GroupBo getGroupBo() {
212         GroupBo groupBo = null;
213         if (StringUtils.isNotBlank(getGroupReviewerName())) {
214             if ( groupBo == null ) {
215                 groupBo = GroupBo.from(KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(
216                         getGroupReviewerNamespace(), getGroupReviewerName()));
217             }
218         }
219         return groupBo;
220     }
221 
222     public PersonImpl getPersonImpl() {
223         return new PersonImpl();
224     }
225 
226         /**
227        * An override of the refresh() method that properly preserves the RuleBaseValues instance. If the delegationRuleBaseValues property
228        * becomes null as a result of the refresh() method on the PersistableBusinessObjectBase superclass, an attempt is made to retrieve
229        * it by calling refreshReferenceObject() for the property. If that also fails, then the RuleBaseValues instance that was in-place
230        * prior to the refresh() superclass call will be used as the delegationRuleBaseValues property's value. This override is necessary
231        * in order to prevent certain exceptions during the cancellation of a rule delegation maintenance document.
232        *
233        * @see org.kuali.rice.krad.bo.PersistableBusinessObjectBase#refresh()
234        * @see org.kuali.rice.krad.bo.PersistableBusinessObjectBase#refreshReferenceObject(java.lang.String)
235        */
236 	@Override
237 	public void refresh() {
238 		RuleBaseValues oldRuleBaseValues = this.getDelegationRule();
239 		super.refresh();
240 		if (this.getDelegationRule() == null) {
241 			this.refreshReferenceObject("delegationRuleBaseValues");
242 			if (this.getDelegationRule() == null) {
243 				this.setDelegationRule(oldRuleBaseValues);
244 			}
245 		}
246 	}
247 
248     public static org.kuali.rice.kew.api.rule.RuleDelegation to(RuleDelegationBo bo) {
249         if (bo == null) {
250             return null;
251         }
252         return org.kuali.rice.kew.api.rule.RuleDelegation.Builder.create(bo).build();
253         /*org.kuali.rice.kew.api.rule.RuleDelegation.Builder builder = org.kuali.rice.kew.api.rule.RuleDelegation.Builder.create();
254         builder.setDelegationType(bo.getDelegationType());
255         builder.setDelegationRule(org.kuali.rice.kew.api.rule.Rule.Builder.create(RuleBaseValues.to(
256                 bo.getDelegationRule())));
257         return builder.build();*/
258     }
259 }
260