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 org.apache.commons.lang.ObjectUtils;
19  import org.hibernate.annotations.Fetch;
20  import org.hibernate.annotations.FetchMode;
21  import org.hibernate.annotations.GenericGenerator;
22  import org.hibernate.annotations.Parameter;
23  import org.kuali.rice.core.api.util.collect.CollectionUtils;
24  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
25  import org.kuali.rice.kew.api.rule.RuleExtension;
26  import org.kuali.rice.kew.api.rule.RuleExtensionContract;
27  import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
28  import org.kuali.rice.kew.service.KEWServiceLocator;
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.ManyToOne;
38  import javax.persistence.OneToMany;
39  import javax.persistence.Table;
40  import javax.persistence.Version;
41  import java.io.Serializable;
42  import java.util.ArrayList;
43  import java.util.HashMap;
44  import java.util.List;
45  import java.util.Map;
46  
47  /**
48   * An extension of a {@link RuleBaseValues}.  Provides attribute-specific data
49   * extensions to the rule for a particular {@link org.kuali.rice.kew.rule.bo.RuleAttribute}.  Contains
50   * a List of {@link RuleExtensionValue}s.
51   * 
52   * @see RuleBaseValues
53   * @see org.kuali.rice.kew.rule.bo.RuleAttribute
54   * @see RuleExtensionValue
55   *
56   * @author Kuali Rice Team (rice.collab@kuali.org)
57   */
58  @Entity
59  @Table(name="KREW_RULE_EXT_T")
60  //@Sequence(name="KREW_RTE_TMPL_S", property="ruleExtensionId")
61  public class RuleExtensionBo implements RuleExtensionContract, Serializable {
62  
63  	private static final long serialVersionUID = 8178135296413950516L;
64  
65  	@Id
66  	@GeneratedValue(generator="KREW_RTE_TMPL_S")
67  	@GenericGenerator(name="KREW_RTE_TMPL_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
68  			@Parameter(name="sequence_name",value="KREW_RTE_TMPL_S"),
69  			@Parameter(name="value_column",value="id")
70  	})
71  	@Column(name="RULE_EXT_ID")
72  	private String ruleExtensionId;
73  
74  	@Column(name="RULE_TMPL_ATTR_ID", insertable=false, updatable=false)
75  	private String ruleTemplateAttributeId;
76  
77  	@Column(name="RULE_ID", insertable=false, updatable=false)
78  	private String ruleBaseValuesId;
79  
80  	@Version
81  	@Column(name="VER_NBR")
82  	private Long versionNumber;
83  	
84  	@ManyToOne(fetch=FetchType.EAGER)
85  	@JoinColumn(name="RULE_ID")
86  	private RuleBaseValues ruleBaseValues;
87  
88  	@ManyToOne(fetch=FetchType.EAGER)
89  	@JoinColumn(name="RULE_TMPL_ATTR_ID")
90  	private RuleTemplateAttributeBo ruleTemplateAttribute;
91  
92  	@OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE}, mappedBy="extension")
93  	@Fetch(value = FetchMode.SELECT)
94  	private List<RuleExtensionValue> extensionValues;
95  
96  	public RuleExtensionBo() {
97  		extensionValues = new ArrayList<RuleExtensionValue>();
98  	}
99  
100 	//@PrePersist
101     public void beforeInsert(){
102         OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());
103     }
104 	
105 	public List<RuleExtensionValue> getExtensionValues() {
106 		return extensionValues;
107 	}
108 
109 	public void setExtensionValues(List<RuleExtensionValue> extensionValues) {
110 		this.extensionValues = extensionValues;
111 	}
112 
113     @Override
114 	public RuleTemplateAttributeBo getRuleTemplateAttribute() {
115 		return ruleTemplateAttribute;
116 	}
117 
118     @Override
119     public Map<String, String> getExtensionValuesMap() {
120         Map<String, String> extensions = new HashMap<String, String>();
121         for (RuleExtensionValue value : getExtensionValues()) {
122             extensions.put(value.getKey(), value.getValue());
123         }
124         return extensions;
125     }
126 
127     public void setRuleTemplateAttribute(RuleTemplateAttributeBo ruleTemplateAttribute) {
128 		this.ruleTemplateAttribute = ruleTemplateAttribute;
129 	}
130 
131 	public RuleExtensionValue getRuleExtensionValue(int index) {
132 		while (getExtensionValues().size() <= index) {
133 			getExtensionValues().add(new RuleExtensionValue());
134 		}
135 		return (RuleExtensionValue) getExtensionValues().get(index);
136 	}
137 
138 	public RuleBaseValues getRuleBaseValues() {
139 		return ruleBaseValues;
140 	}
141 
142 	public void setRuleBaseValues(RuleBaseValues ruleBaseValues) {
143 		this.ruleBaseValues = ruleBaseValues;
144 	}
145 
146 	public Long getVersionNumber() {
147 		return versionNumber;
148 	}
149 
150 	public void setVersionNumber(Long versionNumber) {
151 		this.versionNumber = versionNumber;
152 	}
153 
154 	public String getRuleBaseValuesId() {
155 		return ruleBaseValuesId;
156 	}
157 
158 	public void setRuleBaseValuesId(String ruleBaseValuesId) {
159 		this.ruleBaseValuesId = ruleBaseValuesId;
160 	}
161 
162 	public String getRuleExtensionId() {
163 		return ruleExtensionId;
164 	}
165 
166 	public void setRuleExtensionId(String ruleExtensionId) {
167 		this.ruleExtensionId = ruleExtensionId;
168 	}
169 
170 	public String getRuleTemplateAttributeId() {
171 		return ruleTemplateAttributeId;
172 	}
173 
174 	public void setRuleTemplateAttributeId(String ruleTemplateAttributeId) {
175 		this.ruleTemplateAttributeId = ruleTemplateAttributeId;
176 	}
177 
178     public boolean equals(Object o) {
179         if (o == null) return false;
180         if (!(o instanceof RuleExtensionBo)) return false;
181         RuleExtensionBo pred = (RuleExtensionBo) o;
182         return ObjectUtils.equals(ruleBaseValues.getRuleTemplate(), pred.getRuleBaseValues().getRuleTemplate()) &&
183                ObjectUtils.equals(ruleTemplateAttribute, pred.getRuleTemplateAttribute()) &&
184                CollectionUtils.collectionsEquivalent(extensionValues, pred.getExtensionValues());
185     }
186 
187     public String toString() {
188         return "[RuleExtension:"
189                +  " ruleExtensionId=" + ruleExtensionId
190                + ", ruleTemplateAttributeId=" + ruleTemplateAttributeId
191                + ", ruleBaseValuesId=" + ruleBaseValuesId
192                + ", ruleBaseValues=" + ruleBaseValues
193                + ", ruleTemplateAttribute=" + ruleTemplateAttribute
194                + ", extensionValues=" + extensionValues
195                + ", versionNumber=" + versionNumber
196                + "]";
197     }
198 
199     /**
200      * Converts a mutable bo to its immutable counterpart
201      * @param bo the mutable business object
202      * @return the immutable object
203      */
204     public static RuleExtension to(RuleExtensionBo bo) {
205         if (bo == null) {
206             return null;
207         }
208         return RuleExtension.Builder.create(bo).build();
209     }
210 
211 }