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