View Javadoc

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