View Javadoc

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