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