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