View Javadoc

1   /**
2    * Copyright 2005-2014 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.KewApiConstants;
22  import org.kuali.rice.kew.api.KewApiServiceLocator;
23  import org.kuali.rice.kew.api.WorkflowRuntimeException;
24  import org.kuali.rice.kew.api.extension.ExtensionUtils;
25  import org.kuali.rice.kew.api.rule.RuleTemplateAttributeContract;
26  import org.kuali.rice.kew.rule.RuleExtensionBo;
27  import org.kuali.rice.kew.rule.RuleExtensionValue;
28  import org.kuali.rice.kew.rule.RuleValidationAttribute;
29  import org.kuali.rice.kew.rule.WorkflowRuleAttribute;
30  import org.kuali.rice.kew.service.KEWServiceLocator;
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         return getRuleAttribute().isWorkflowAttribute();
120     }
121 
122     public boolean isRuleValidationAttribute() {
123         // just check the type here to avoid having to load the class from the class loader if it's not actually there
124         return KewApiConstants.RULE_VALIDATION_ATTRIBUTE_TYPE.equals(getRuleAttribute().getType());
125     }
126 
127     /**
128      * Instantiates and returns a new instance of the WorkflowAttribute class configured on this template.
129      * The calling code should be sure to call isWorkflowAttribute first to verify the type of this attribute
130      * is that of a WorkflowAttribute.  Otherwise a RuntimeException will be thrown.
131      */
132     public WorkflowRuleAttribute getWorkflowAttribute() {
133         try {
134             Object tempAttr = ExtensionUtils.loadExtension(RuleAttribute.to(getRuleAttribute()), getRuleAttribute().getApplicationId());
135 
136             if (tempAttr == null
137                     || !WorkflowRuleAttribute.class.isAssignableFrom(tempAttr.getClass())) {
138                 throw new WorkflowRuntimeException("Could not find workflow attribute " + getRuleAttribute().getName());
139             }
140             WorkflowRuleAttribute workflowAttribute = (WorkflowRuleAttribute)tempAttr;
141             workflowAttribute.setRequired(required.booleanValue());
142             return workflowAttribute;
143         } catch (Exception e) {
144             throw new RuntimeException("Caught exception instantiating new " + getRuleAttribute().getResourceDescriptor(), e);
145         }
146     }
147 
148     /**
149      * Instantiates and returns a new instance of the RuleValidationAttribute class configured on this template.
150      * The calling code should be sure to call isRuleValidationAttribute first to verify the type of this attribute
151      * is that of a RuleValidationAttribute.  Otherwise a RuntimeException will be thrown.
152      */
153     public RuleValidationAttribute getRuleValidationAttribute() {
154         try {
155             RuleAttribute attrib = getRuleAttribute();
156             return KEWServiceLocator.getRuleValidationAttributeResolver().resolveRuleValidationAttribute(attrib.getName(), attrib.getApplicationId());
157         } catch (Exception e) {
158             throw new RuntimeException("Caught exception instantiating new " + getRuleAttribute().getResourceDescriptor(), e);
159         }
160     }
161 
162     public List<RuleExtensionBo> getRuleExtensions() {
163         return ruleExtensions;
164     }
165 
166     public Map<String, String> getRuleExtensionMap() {
167         Map<String, String> extensions = new HashMap<String, String>();
168         if (this.getRuleExtensions() != null) {
169             for (RuleExtensionBo ext : this.getRuleExtensions()) {
170                 for (RuleExtensionValue value : ext.getExtensionValues()) {
171                     extensions.put(value.getKey(), value.getValue());
172                 }
173             }
174         }
175         return extensions;
176     }
177 
178     public void setRuleExtensions(List<RuleExtensionBo> ruleExtensions) {
179         this.ruleExtensions = ruleExtensions;
180     }
181 
182     public RuleAttribute getRuleAttribute() {
183         if (ruleAttribute == null && ruleAttributeId != null) {
184             ruleAttribute = RuleAttribute.from(KewApiServiceLocator.getExtensionRepositoryService().getExtensionById(ruleAttributeId));
185         }
186         return ruleAttribute;
187     }
188 
189     public void setRuleAttribute(org.kuali.rice.kew.rule.bo.RuleAttribute ruleAttribute) {
190         this.ruleAttribute = ruleAttribute;
191     }
192 
193     public RuleTemplateBo getRuleTemplate() {
194         return ruleTemplate;
195     }
196 
197     public void setRuleTemplate(RuleTemplateBo ruleTemplate) {
198         this.ruleTemplate = ruleTemplate;
199     }
200 
201     public String getDefaultValue() {
202         return defaultValue;
203     }
204 
205     public void setDefaultValue(String defaultValue) {
206         this.defaultValue = defaultValue;
207     }
208 
209     public Integer getDisplayOrder() {
210         return displayOrder;
211     }
212 
213     public void setDisplayOrder(Integer displayOrder) {
214         this.displayOrder = displayOrder;
215     }
216 
217     public boolean isRequired() {
218         return (getRequired() == null) || (getRequired().booleanValue());
219     }
220 
221     public Boolean getRequired() {
222         return required;
223     }
224 
225     public void setRequired(Boolean required) {
226         this.required = required;
227     }
228 
229     public boolean isActive() {
230         return (getActive() == null) || (getActive().booleanValue());
231     }
232 
233     public Boolean getActive() {
234         return active;
235     }
236 
237     public void setActive(Boolean active) {
238         this.active = active;
239     }
240     
241     public void setActive(boolean active) {
242     	this.active = active;
243     }
244 
245     public String getRuleAttributeId() {
246     	return ruleAttributeId;
247     }
248 
249     public void setRuleAttributeId(String ruleAttributeId) {
250     	this.ruleAttributeId = ruleAttributeId;
251     }
252 
253     public String getId() {
254     	return id;
255     }
256 
257     public void setId(String id) {
258     	this.id = id;
259     }
260 
261     public String getRuleTemplateId() {
262     	return ruleTemplateId;
263     }
264 
265     public void setRuleTemplateId(String ruleTemplateId) {
266     	this.ruleTemplateId = ruleTemplateId;
267     }
268 
269 }