View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.rule.bo;
18  
19  import org.hibernate.annotations.GenericGenerator;
20  import org.hibernate.annotations.Parameter;
21  import org.kuali.rice.core.api.reflect.ObjectDefinition;
22  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
23  import org.kuali.rice.kew.api.WorkflowRuntimeException;
24  import org.kuali.rice.kew.rule.RuleExtension;
25  import org.kuali.rice.kew.rule.RuleValidationAttribute;
26  import org.kuali.rice.kew.rule.WorkflowAttribute;
27  import org.kuali.rice.kew.rule.service.RuleAttributeService;
28  import org.kuali.rice.kew.service.KEWServiceLocator;
29  import org.kuali.rice.kew.util.KEWConstants;
30  import org.kuali.rice.krad.bo.MutableInactivatable;
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.List;
43  
44  
45  /**
46   * A model bean which services as the link between a {@link RuleTemplate} and
47   * a {@link RuleAttribute}.
48   *
49   * @author Kuali Rice Team (rice.collab@kuali.org)
50   */
51  @Entity
52  @Table(name="KREW_RULE_TMPL_ATTR_T")
53  //@Sequence(name="KREW_RTE_TMPL_S", property="ruleTemplateAttributeId")
54  public class RuleTemplateAttribute extends PersistableBusinessObjectBase implements Comparable<RuleTemplateAttribute>, MutableInactivatable {
55  
56      private static final long serialVersionUID = -3580049225424553828L;
57      @Id
58      @GeneratedValue(generator="KREW_RTE_TMPL_S")
59  	@GenericGenerator(name="KREW_RTE_TMPL_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
60  			@Parameter(name="sequence_name",value="KREW_RTE_TMPL_S"),
61  			@Parameter(name="value_column",value="id")
62  	})
63  	@Column(name="RULE_TMPL_ATTR_ID")
64  	private String ruleTemplateAttributeId;
65      @Column(name="RULE_TMPL_ID", insertable=false, updatable=false)
66  	private String ruleTemplateId;
67      @Column(name="RULE_ATTR_ID", insertable=false, updatable=false)
68  	private String ruleAttributeId;
69      @Column(name="REQ_IND")
70  	private Boolean required;
71      @Column(name="ACTV_IND")
72  	private Boolean active;
73      @Column(name="DSPL_ORD")
74  	private Integer displayOrder;
75      @Column(name="DFLT_VAL")
76  	private String defaultValue;
77  
78      @ManyToOne(fetch=FetchType.EAGER)
79  	@JoinColumn(name="RULE_TMPL_ID")
80  	private RuleTemplate ruleTemplate;
81      @ManyToOne(fetch=FetchType.EAGER)
82  	@JoinColumn(name="RULE_ATTR_ID")
83  	private RuleAttribute ruleAttribute;
84      @OneToMany(fetch=FetchType.LAZY,mappedBy="ruleTemplateAttribute")
85  	private List<RuleExtension> ruleExtensions;
86      
87      
88      public RuleTemplateAttribute() {
89  	this.required = Boolean.FALSE;
90  	this.active = Boolean.TRUE;
91      }
92     
93      public int compareTo(RuleTemplateAttribute ruleTemplateAttribute) {
94      	if ((this.getDisplayOrder() != null) && (ruleTemplateAttribute.getDisplayOrder() != null)) {
95  	    	return this.getDisplayOrder().compareTo(ruleTemplateAttribute.getDisplayOrder());
96  	    }
97      	return 0;
98      }
99  
100     public Object getAttribute() {
101 	try {
102 	    ObjectDefinition objectDefinition = new ObjectDefinition(getRuleAttribute().getClassName(), getRuleAttribute().getApplicationId());
103 	    Object attribute = GlobalResourceLoader.getObject(objectDefinition);
104 	    if (attribute == null) {
105 		throw new WorkflowRuntimeException("Could not find attribute " + objectDefinition);
106 	    }
107 	    if (attribute instanceof WorkflowAttribute) {
108 		((WorkflowAttribute) attribute).setRequired(required.booleanValue());
109 	    }
110 	    return attribute;
111 	} catch (Exception e) {
112 	    throw new RuntimeException("Caught error attempting to load attribute class: " + getRuleAttribute().getClassName(), e);
113 	}
114     }
115 
116     public boolean isWorkflowAttribute() {
117 	try {
118 	    Object attributeObject = getAttribute();//GlobalResourceLoader.getResourceLoader().getObject(new ObjectDefinition(getRuleAttribute().getClassName()));
119 	    if (attributeObject == null) {
120 		return false;
121 	    }
122 	    Class<?> attributeClass = attributeObject.getClass();
123 	    return WorkflowAttribute.class.isAssignableFrom(attributeClass);
124 	} catch (Exception e) {
125 	    throw new RuntimeException("Caught error attempting to load WorkflowAttribute class: " + getRuleAttribute().getClassName(), e);
126 	}
127     }
128 
129     public boolean isRuleValidationAttribute() {
130 	// just check the type here to avoid having to load the class from the class loader if it's not actually there
131 	return KEWConstants.RULE_VALIDATION_ATTRIBUTE_TYPE.equals(getRuleAttribute().getType());
132     }
133 
134     /**
135      * Instantiates and returns a new instance of the WorkflowAttribute class configured on this template.
136      * The calling code should be sure to call isWorkflowAttribute first to verify the type of this attribute
137      * is that of a WorkflowAttribute.  Otherwise a RuntimeException will be thrown.
138      */
139     public WorkflowAttribute getWorkflowAttribute() {
140 	try {
141 	    ObjectDefinition objectDefinition = new ObjectDefinition(getRuleAttribute().getClassName(), getRuleAttribute().getApplicationId());
142 	    WorkflowAttribute workflowAttribute = (WorkflowAttribute) GlobalResourceLoader.getResourceLoader().getObject(objectDefinition);
143 	    if (workflowAttribute == null) {
144 		throw new WorkflowRuntimeException("Could not find workflow attribute " + objectDefinition);
145 	    }
146 	    workflowAttribute.setRequired(required.booleanValue());
147 	    return workflowAttribute;
148 	} catch (Exception e) {
149 	    throw new RuntimeException("Caught exception instantiating new " + getRuleAttribute().getClassName(), e);
150 	}
151     }
152 
153     /**
154      * Instantiates and returns a new instance of the RuleValidationAttribute class configured on this template.
155      * The calling code should be sure to call isRuleValidationAttribute first to verify the type of this attribute
156      * is that of a RuleValidationAttribute.  Otherwise a RuntimeException will be thrown.
157      */
158     public RuleValidationAttribute getRuleValidationAttribute() {
159 	try {
160 	    return (RuleValidationAttribute) getAttribute();
161 	} catch (Exception e) {
162 	    throw new RuntimeException("Caught exception instantiating new " + getRuleAttribute().getClassName(), e);
163 	}
164     }
165 
166     public List<RuleExtension> getRuleExtensions() {
167 	return ruleExtensions;
168     }
169 
170     public void setRuleExtensions(List<RuleExtension> ruleExtensions) {
171 	this.ruleExtensions = ruleExtensions;
172     }
173 
174     public RuleAttribute getRuleAttribute() {
175 	if (ruleAttribute == null && ruleAttributeId != null) {
176 	    ruleAttribute = ((RuleAttributeService) KEWServiceLocator.getService(KEWServiceLocator.RULE_ATTRIBUTE_SERVICE)).findByRuleAttributeId(ruleAttributeId);
177 	}
178 	return ruleAttribute;
179     }
180 
181     public void setRuleAttribute(RuleAttribute ruleAttribute) {
182 	this.ruleAttribute = ruleAttribute;
183     }
184 
185     public RuleTemplate getRuleTemplate() {
186 	return ruleTemplate;
187     }
188 
189     public void setRuleTemplate(RuleTemplate ruleTemplate) {
190 	this.ruleTemplate = ruleTemplate;
191     }
192 
193     public String getDefaultValue() {
194 	return defaultValue;
195     }
196 
197     public void setDefaultValue(String defaultValue) {
198 	this.defaultValue = defaultValue;
199     }
200 
201     public Integer getDisplayOrder() {
202 	return displayOrder;
203     }
204 
205     public void setDisplayOrder(Integer displayOrder) {
206 	this.displayOrder = displayOrder;
207     }
208 
209     public boolean isRequired() {
210         return (getRequired() == null) || (getRequired().booleanValue());
211     }
212 
213     public Boolean getRequired() {
214 	return required;
215     }
216 
217     public void setRequired(Boolean required) {
218 	this.required = required;
219     }
220 
221     public boolean isActive() {
222         return (getActive() == null) || (getActive().booleanValue());
223     }
224 
225     public Boolean getActive() {
226         return active;
227     }
228 
229     public void setActive(Boolean active) {
230         this.active = active;
231     }
232     
233     public void setActive(boolean active) {
234     	this.active = active;
235     }
236 
237     public String getRuleAttributeId() {
238     	return ruleAttributeId;
239     }
240 
241     public void setRuleAttributeId(String ruleAttributeId) {
242     	this.ruleAttributeId = ruleAttributeId;
243     }
244 
245     public String getRuleTemplateAttributeId() {
246     	return ruleTemplateAttributeId;
247     }
248 
249     public void setRuleTemplateAttributeId(String ruleTemplateAttributeId) {
250     	this.ruleTemplateAttributeId = ruleTemplateAttributeId;
251     }
252 
253     public String getRuleTemplateId() {
254     	return ruleTemplateId;
255     }
256 
257     public void setRuleTemplateId(String ruleTemplateId) {
258     	this.ruleTemplateId = ruleTemplateId;
259     }
260 }