View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.rule;
18  
19  import java.util.LinkedHashMap;
20  
21  import javax.persistence.CascadeType;
22  import javax.persistence.Column;
23  import javax.persistence.Entity;
24  import javax.persistence.FetchType;
25  import javax.persistence.Id;
26  import javax.persistence.JoinColumn;
27  import javax.persistence.ManyToOne;
28  import javax.persistence.OneToOne;
29  import javax.persistence.Table;
30  import javax.persistence.Transient;
31  
32  import org.kuali.rice.core.jpa.annotations.Sequence;
33  import org.kuali.rice.kew.bo.KewPersistableBusinessObjectBase;
34  import org.kuali.rice.kew.doctype.bo.DocumentType;
35  import org.kuali.rice.kew.service.KEWServiceLocator;
36  import org.kuali.rice.kew.util.KEWConstants;
37  
38  
39  /**
40   * A model bean representing the delegation of a rule from a responsibility to
41   * another rule.  Specifies the delegation type which can be either
42   * {@link KEWConstants#DELEGATION_PRIMARY} or {@link KEWConstants#DELEGATION_SECONDARY}.
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  @Entity
47  @Table(name="KREW_DLGN_RSP_T")
48  @Sequence(name="KREW_RTE_TMPL_S", property="ruleDelegationId")
49  public class RuleDelegation extends KewPersistableBusinessObjectBase {
50  
51  	private static final long serialVersionUID = 7989203310473741293L;
52  	@Id
53  	@Column(name="DLGN_RULE_ID")
54  	private Long ruleDelegationId;
55      @Column(name="RSP_ID", insertable=false, updatable=false)
56  	private Long responsibilityId;
57      @Column(name="DLGN_RULE_BASE_VAL_ID", insertable=false, updatable=false)
58  	private Long delegateRuleId;
59      @Column(name="DLGN_TYP")
60      private String delegationType = KEWConstants.DELEGATION_PRIMARY;
61  
62      @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
63  	@JoinColumn(name="DLGN_RULE_BASE_VAL_ID")
64  	private RuleBaseValues delegationRuleBaseValues;
65  //    @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
66  //	@JoinColumn(name="RULE_RSP_ID")
67  //	private RuleResponsibility ruleResponsibility;
68  
69      public RuleDelegation() {
70      }
71  
72      public Object copy(boolean preserveKeys) {
73          RuleDelegation clone = new RuleDelegation();
74          if (ruleDelegationId != null && preserveKeys) {
75              clone.setRuleDelegationId(new Long(ruleDelegationId.longValue()));
76          }
77          clone.setDelegationRuleBaseValues(delegationRuleBaseValues);
78          clone.setDelegateRuleId(delegationRuleBaseValues.getRuleBaseValuesId());
79          if (delegationType != null) {
80              clone.setDelegationType(new String(delegationType));
81          }
82          return clone;
83      }
84  
85      public Long getDelegateRuleId() {
86          return delegateRuleId;
87      }
88      public void setDelegateRuleId(Long delegateRuleId) {
89          this.delegateRuleId = delegateRuleId;
90      }
91      public RuleBaseValues getDelegationRuleBaseValues() {
92          return delegationRuleBaseValues;
93      }
94      public void setDelegationRuleBaseValues(RuleBaseValues delegationRuleBaseValues) {
95          this.delegationRuleBaseValues = delegationRuleBaseValues;
96      }
97      public String getDelegationType() {
98          return delegationType;
99      }
100     public void setDelegationType(String delegationType) {
101         this.delegationType = delegationType;
102     }
103     public Long getRuleDelegationId() {
104         return ruleDelegationId;
105     }
106     public void setRuleDelegationId(Long ruleDelegationId) {
107         this.ruleDelegationId = ruleDelegationId;
108     }
109 
110     /**
111      * Returns the most recent RuleResponsibility for the responsibility
112      * id on this RuleDelegation.
113      */
114     public RuleResponsibility getRuleResponsibility() {
115     	if ( getResponsibilityId() == null ) {
116     		return null;
117     	}
118     	return KEWServiceLocator.getRuleService().findRuleResponsibility(getResponsibilityId());
119     }
120 
121     public DocumentType getDocumentType() {
122         return this.getDelegationRuleBaseValues().getDocumentType();
123     }
124 
125     public Long getResponsibilityId() {
126         return responsibilityId;
127     }
128     public void setResponsibilityId(Long ruleResponsibilityId) {
129         this.responsibilityId = ruleResponsibilityId;
130     }
131 
132 	@Override
133 	protected LinkedHashMap toStringMapper() {
134 		LinkedHashMap map = new LinkedHashMap();
135 		map.put("ruleDelegationId", ruleDelegationId);
136 		map.put("responsibilityId", responsibilityId);
137 		map.put("delegateRuleId", delegateRuleId);
138 		map.put("delegationType", delegationType);
139 		return map;
140 	}
141 
142 	/**
143 	 * An override of the refresh() method that properly preserves the RuleBaseValues instance. If the delegationRuleBaseValues property
144 	 * becomes null as a result of the refresh() method on the PersistableBusinessObjectBase superclass, an attempt is made to retrieve
145 	 * it by calling refreshReferenceObject() for the property. If that also fails, then the RuleBaseValues instance that was in-place
146 	 * prior to the refresh() superclass call will be used as the delegationRuleBaseValues property's value. This override is necessary
147 	 * in order to prevent certain exceptions during the cancellation of a rule delegation maintenance document.
148 	 * 
149 	 * @see org.kuali.rice.kns.bo.PersistableBusinessObjectBase#refresh()
150 	 * @see org.kuali.rice.kns.bo.PersistableBusinessObjectBase#refreshReferenceObject(java.lang.String)
151 	 */
152 	@Override
153 	public void refresh() {
154 		RuleBaseValues oldRuleBaseValues = this.getDelegationRuleBaseValues();
155 		super.refresh();
156 		if (this.getDelegationRuleBaseValues() == null) {
157 			this.refreshReferenceObject("delegationRuleBaseValues");
158 			if (this.getDelegationRuleBaseValues() == null) {
159 				this.setDelegationRuleBaseValues(oldRuleBaseValues);
160 			}
161 		}
162 	}
163 }
164