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