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.bo;
17  
18  import org.hibernate.annotations.GenericGenerator;
19  import org.hibernate.annotations.Parameter;
20  import org.kuali.rice.core.api.mo.common.active.MutableInactivatable;
21  import org.kuali.rice.kew.api.WorkflowRuntimeException;
22  import org.kuali.rice.kew.api.extension.ExtensionUtils;
23  import org.kuali.rice.kew.api.rule.RuleTemplateAttributeContract;
24  import org.kuali.rice.kew.rule.RuleExtensionBo;
25  import org.kuali.rice.kew.rule.RuleExtensionValue;
26  import org.kuali.rice.kew.rule.RuleValidationAttribute;
27  import org.kuali.rice.kew.rule.WorkflowRuleAttribute;
28  import org.kuali.rice.kew.rule.service.RuleAttributeService;
29  import org.kuali.rice.kew.service.KEWServiceLocator;
30  import org.kuali.rice.kew.api.KewApiConstants;
31  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
32  
33  import javax.persistence.Column;
34  import javax.persistence.Entity;
35  import javax.persistence.FetchType;
36  import javax.persistence.GeneratedValue;
37  import javax.persistence.Id;
38  import javax.persistence.JoinColumn;
39  import javax.persistence.ManyToOne;
40  import javax.persistence.OneToMany;
41  import javax.persistence.Table;
42  import java.util.HashMap;
43  import java.util.List;
44  import java.util.Map;
45  
46  /**
47   * A model bean which services as the link between a {@link RuleTemplateBo} and
48   * a {@link RuleAttribute}.
49   *
50   * @author Kuali Rice Team (rice.collab@kuali.org)
51   */
52  @Entity
53  @Table(name="KREW_RULE_TMPL_ATTR_T")
54  //@Sequence(name="KREW_RTE_TMPL_S", property="id")
55  public class RuleTemplateAttributeBo extends PersistableBusinessObjectBase
56          implements Comparable<RuleTemplateAttributeBo>, MutableInactivatable, RuleTemplateAttributeContract {
57  
58      private static final long serialVersionUID = -3580049225424553828L;
59      @Id
60      @GeneratedValue(generator="KREW_RTE_TMPL_S")
61  	@GenericGenerator(name="KREW_RTE_TMPL_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
62  			@Parameter(name="sequence_name",value="KREW_RTE_TMPL_S"),
63  			@Parameter(name="value_column",value="id")
64  	})
65  	@Column(name="RULE_TMPL_ATTR_ID")
66  	private String id;
67      @Column(name="RULE_TMPL_ID", insertable=false, updatable=false)
68  	private String ruleTemplateId;
69      @Column(name="RULE_ATTR_ID", insertable=false, updatable=false)
70  	private String ruleAttributeId;
71      @Column(name="REQ_IND")
72  	private Boolean required;
73      @Column(name="ACTV_IND")
74  	private Boolean active;
75      @Column(name="DSPL_ORD")
76  	private Integer displayOrder;
77      @Column(name="DFLT_VAL")
78  	private String defaultValue;
79  
80      @ManyToOne(fetch=FetchType.EAGER)
81  	@JoinColumn(name="RULE_TMPL_ID")
82  	private RuleTemplateBo ruleTemplate;
83      @ManyToOne(fetch=FetchType.EAGER)
84  	@JoinColumn(name="RULE_ATTR_ID")
85  	private RuleAttribute ruleAttribute;
86      @OneToMany(fetch=FetchType.LAZY,mappedBy="ruleTemplateAttribute")
87  	private List<RuleExtensionBo> ruleExtensions;
88      
89      
90      public RuleTemplateAttributeBo() {
91          this.required = Boolean.FALSE;
92          this.active = Boolean.TRUE;
93      }
94     
95      public int compareTo(RuleTemplateAttributeBo ruleTemplateAttribute) {
96          if ((this.getDisplayOrder() != null) && (ruleTemplateAttribute.getDisplayOrder() != null)) {
97              return this.getDisplayOrder().compareTo(ruleTemplateAttribute.getDisplayOrder());
98          }
99          return 0;
100     }
101 
102     public Object getAttribute() {
103         try {
104             //ObjectDefinition objectDefinition = new ObjectDefinition(getRuleAttribute().getResourceDescriptor(), getRuleAttribute().getApplicationId());
105             Object attribute = ExtensionUtils.loadExtension(RuleAttribute.to(getRuleAttribute()), getRuleAttribute().getApplicationId());
106             if (attribute == null) {
107                 throw new WorkflowRuntimeException("Could not find attribute " + getRuleAttribute().getName());
108             }
109             if (attribute instanceof WorkflowRuleAttribute) {
110                 ((WorkflowRuleAttribute) attribute).setRequired(required.booleanValue());
111             }
112             return attribute;
113         } catch (Exception e) {
114             throw new RuntimeException("Caught error attempting to load attribute class: " + getRuleAttribute().getResourceDescriptor(), e);
115         }
116     }
117 
118     public boolean isWorkflowAttribute() {
119         try {
120             Object attributeObject = getAttribute();//GlobalResourceLoader.getResourceLoader().getObject(new ObjectDefinition(getRuleAttribute().getClassName()));
121             if (attributeObject == null) {
122                 return false;
123             }
124             Class<?> attributeClass = attributeObject.getClass();
125             return WorkflowRuleAttribute.class.isAssignableFrom(attributeClass);
126         } catch (Exception e) {
127             throw new RuntimeException("Caught error attempting to load WorkflowAttribute class: " + getRuleAttribute().getResourceDescriptor(), e);
128         }
129     }
130 
131     public boolean isRuleValidationAttribute() {
132         // just check the type here to avoid having to load the class from the class loader if it's not actually there
133         return KewApiConstants.RULE_VALIDATION_ATTRIBUTE_TYPE.equals(getRuleAttribute().getType());
134     }
135 
136     /**
137      * Instantiates and returns a new instance of the WorkflowAttribute class configured on this template.
138      * The calling code should be sure to call isWorkflowAttribute first to verify the type of this attribute
139      * is that of a WorkflowAttribute.  Otherwise a RuntimeException will be thrown.
140      */
141     public WorkflowRuleAttribute getWorkflowAttribute() {
142         try {
143             Object tempAttr = ExtensionUtils.loadExtension(RuleAttribute.to(getRuleAttribute()), getRuleAttribute().getApplicationId());
144 
145             if (tempAttr == null
146                     || !WorkflowRuleAttribute.class.isAssignableFrom(tempAttr.getClass())) {
147                 throw new WorkflowRuntimeException("Could not find workflow attribute " + getRuleAttribute().getName());
148             }
149             WorkflowRuleAttribute workflowAttribute = (WorkflowRuleAttribute)tempAttr;
150             workflowAttribute.setRequired(required.booleanValue());
151             return workflowAttribute;
152         } catch (Exception e) {
153             throw new RuntimeException("Caught exception instantiating new " + getRuleAttribute().getResourceDescriptor(), e);
154         }
155     }
156 
157     /**
158      * Instantiates and returns a new instance of the RuleValidationAttribute class configured on this template.
159      * The calling code should be sure to call isRuleValidationAttribute first to verify the type of this attribute
160      * is that of a RuleValidationAttribute.  Otherwise a RuntimeException will be thrown.
161      */
162     public RuleValidationAttribute getRuleValidationAttribute() {
163         try {
164             RuleAttribute attrib = getRuleAttribute();
165             return KEWServiceLocator.getRuleValidationAttributeResolver().resolveRuleValidationAttribute(attrib.getName(), attrib.getApplicationId());
166         } catch (Exception e) {
167             throw new RuntimeException("Caught exception instantiating new " + getRuleAttribute().getResourceDescriptor(), e);
168         }
169     }
170 
171     public List<RuleExtensionBo> getRuleExtensions() {
172         return ruleExtensions;
173     }
174 
175     public Map<String, String> getRuleExtensionMap() {
176         Map<String, String> extensions = new HashMap<String, String>();
177         if (this.getRuleExtensions() != null) {
178             for (RuleExtensionBo ext : this.getRuleExtensions()) {
179                 for (RuleExtensionValue value : ext.getExtensionValues()) {
180                     extensions.put(value.getKey(), value.getValue());
181                 }
182             }
183         }
184         return extensions;
185     }
186 
187     public void setRuleExtensions(List<RuleExtensionBo> ruleExtensions) {
188         this.ruleExtensions = ruleExtensions;
189     }
190 
191     public RuleAttribute getRuleAttribute() {
192         if (ruleAttribute == null && ruleAttributeId != null) {
193             ruleAttribute = ((RuleAttributeService) KEWServiceLocator.getService(KEWServiceLocator.RULE_ATTRIBUTE_SERVICE)).findByRuleAttributeId(ruleAttributeId);
194         }
195         return ruleAttribute;
196     }
197 
198     public void setRuleAttribute(org.kuali.rice.kew.rule.bo.RuleAttribute ruleAttribute) {
199         this.ruleAttribute = ruleAttribute;
200     }
201 
202     public RuleTemplateBo getRuleTemplate() {
203         return ruleTemplate;
204     }
205 
206     public void setRuleTemplate(RuleTemplateBo ruleTemplate) {
207         this.ruleTemplate = ruleTemplate;
208     }
209 
210     public String getDefaultValue() {
211         return defaultValue;
212     }
213 
214     public void setDefaultValue(String defaultValue) {
215         this.defaultValue = defaultValue;
216     }
217 
218     public Integer getDisplayOrder() {
219         return displayOrder;
220     }
221 
222     public void setDisplayOrder(Integer displayOrder) {
223         this.displayOrder = displayOrder;
224     }
225 
226     public boolean isRequired() {
227         return (getRequired() == null) || (getRequired().booleanValue());
228     }
229 
230     public Boolean getRequired() {
231         return required;
232     }
233 
234     public void setRequired(Boolean required) {
235         this.required = required;
236     }
237 
238     public boolean isActive() {
239         return (getActive() == null) || (getActive().booleanValue());
240     }
241 
242     public Boolean getActive() {
243         return active;
244     }
245 
246     public void setActive(Boolean active) {
247         this.active = active;
248     }
249     
250     public void setActive(boolean active) {
251     	this.active = active;
252     }
253 
254     public String getRuleAttributeId() {
255     	return ruleAttributeId;
256     }
257 
258     public void setRuleAttributeId(String ruleAttributeId) {
259     	this.ruleAttributeId = ruleAttributeId;
260     }
261 
262     public String getId() {
263     	return id;
264     }
265 
266     public void setId(String id) {
267     	this.id = id;
268     }
269 
270     public String getRuleTemplateId() {
271     	return ruleTemplateId;
272     }
273 
274     public void setRuleTemplateId(String ruleTemplateId) {
275     	this.ruleTemplateId = ruleTemplateId;
276     }
277 }