001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.rule.bo;
017    
018    import org.hibernate.annotations.GenericGenerator;
019    import org.hibernate.annotations.Parameter;
020    import org.kuali.rice.core.api.mo.common.active.MutableInactivatable;
021    import org.kuali.rice.kew.api.KewApiConstants;
022    import org.kuali.rice.kew.api.KewApiServiceLocator;
023    import org.kuali.rice.kew.api.WorkflowRuntimeException;
024    import org.kuali.rice.kew.api.extension.ExtensionUtils;
025    import org.kuali.rice.kew.api.rule.RuleTemplateAttributeContract;
026    import org.kuali.rice.kew.rule.RuleExtensionBo;
027    import org.kuali.rice.kew.rule.RuleExtensionValue;
028    import org.kuali.rice.kew.rule.RuleValidationAttribute;
029    import org.kuali.rice.kew.rule.WorkflowRuleAttribute;
030    import org.kuali.rice.kew.service.KEWServiceLocator;
031    import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
032    
033    import javax.persistence.Column;
034    import javax.persistence.Entity;
035    import javax.persistence.FetchType;
036    import javax.persistence.GeneratedValue;
037    import javax.persistence.Id;
038    import javax.persistence.JoinColumn;
039    import javax.persistence.ManyToOne;
040    import javax.persistence.OneToMany;
041    import javax.persistence.Table;
042    import java.util.HashMap;
043    import java.util.List;
044    import java.util.Map;
045    
046    /**
047     * A model bean which services as the link between a {@link RuleTemplateBo} and
048     * a {@link RuleAttribute}.
049     *
050     * @author Kuali Rice Team (rice.collab@kuali.org)
051     */
052    @Entity
053    @Table(name="KREW_RULE_TMPL_ATTR_T")
054    //@Sequence(name="KREW_RTE_TMPL_S", property="id")
055    public class RuleTemplateAttributeBo extends PersistableBusinessObjectBase
056            implements Comparable<RuleTemplateAttributeBo>, MutableInactivatable, RuleTemplateAttributeContract {
057    
058        private static final long serialVersionUID = -3580049225424553828L;
059        @Id
060        @GeneratedValue(generator="KREW_RTE_TMPL_S")
061            @GenericGenerator(name="KREW_RTE_TMPL_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
062                            @Parameter(name="sequence_name",value="KREW_RTE_TMPL_S"),
063                            @Parameter(name="value_column",value="id")
064            })
065            @Column(name="RULE_TMPL_ATTR_ID")
066            private String id;
067        @Column(name="RULE_TMPL_ID", insertable=false, updatable=false)
068            private String ruleTemplateId;
069        @Column(name="RULE_ATTR_ID", insertable=false, updatable=false)
070            private String ruleAttributeId;
071        @Column(name="REQ_IND")
072            private Boolean required;
073        @Column(name="ACTV_IND")
074            private Boolean active;
075        @Column(name="DSPL_ORD")
076            private Integer displayOrder;
077        @Column(name="DFLT_VAL")
078            private String defaultValue;
079    
080        @ManyToOne(fetch=FetchType.EAGER)
081            @JoinColumn(name="RULE_TMPL_ID")
082            private RuleTemplateBo ruleTemplate;
083        @ManyToOne(fetch=FetchType.EAGER)
084            @JoinColumn(name="RULE_ATTR_ID")
085            private RuleAttribute ruleAttribute;
086        @OneToMany(fetch=FetchType.LAZY,mappedBy="ruleTemplateAttribute")
087            private List<RuleExtensionBo> ruleExtensions;
088        
089        
090        public RuleTemplateAttributeBo() {
091            this.required = Boolean.FALSE;
092            this.active = Boolean.TRUE;
093        }
094       
095        public int compareTo(RuleTemplateAttributeBo ruleTemplateAttribute) {
096            if ((this.getDisplayOrder() != null) && (ruleTemplateAttribute.getDisplayOrder() != null)) {
097                return this.getDisplayOrder().compareTo(ruleTemplateAttribute.getDisplayOrder());
098            }
099            return 0;
100        }
101    
102        public Object getAttribute() {
103            try {
104                //ObjectDefinition objectDefinition = new ObjectDefinition(getRuleAttribute().getResourceDescriptor(), getRuleAttribute().getApplicationId());
105                Object attribute = ExtensionUtils.loadExtension(RuleAttribute.to(getRuleAttribute()), getRuleAttribute().getApplicationId());
106                if (attribute == null) {
107                    throw new WorkflowRuntimeException("Could not find attribute " + getRuleAttribute().getName());
108                }
109                if (attribute instanceof WorkflowRuleAttribute) {
110                    ((WorkflowRuleAttribute) attribute).setRequired(required.booleanValue());
111                }
112                return attribute;
113            } catch (Exception e) {
114                throw new RuntimeException("Caught error attempting to load attribute class: " + getRuleAttribute().getResourceDescriptor(), e);
115            }
116        }
117    
118        public boolean isWorkflowAttribute() {
119            return getRuleAttribute().isWorkflowAttribute();
120        }
121    
122        public boolean isRuleValidationAttribute() {
123            // just check the type here to avoid having to load the class from the class loader if it's not actually there
124            return KewApiConstants.RULE_VALIDATION_ATTRIBUTE_TYPE.equals(getRuleAttribute().getType());
125        }
126    
127        /**
128         * Instantiates and returns a new instance of the WorkflowAttribute class configured on this template.
129         * The calling code should be sure to call isWorkflowAttribute first to verify the type of this attribute
130         * is that of a WorkflowAttribute.  Otherwise a RuntimeException will be thrown.
131         */
132        public WorkflowRuleAttribute getWorkflowAttribute() {
133            try {
134                Object tempAttr = ExtensionUtils.loadExtension(RuleAttribute.to(getRuleAttribute()), getRuleAttribute().getApplicationId());
135    
136                if (tempAttr == null
137                        || !WorkflowRuleAttribute.class.isAssignableFrom(tempAttr.getClass())) {
138                    throw new WorkflowRuntimeException("Could not find workflow attribute " + getRuleAttribute().getName());
139                }
140                WorkflowRuleAttribute workflowAttribute = (WorkflowRuleAttribute)tempAttr;
141                workflowAttribute.setRequired(required.booleanValue());
142                return workflowAttribute;
143            } catch (Exception e) {
144                throw new RuntimeException("Caught exception instantiating new " + getRuleAttribute().getResourceDescriptor(), e);
145            }
146        }
147    
148        /**
149         * Instantiates and returns a new instance of the RuleValidationAttribute class configured on this template.
150         * The calling code should be sure to call isRuleValidationAttribute first to verify the type of this attribute
151         * is that of a RuleValidationAttribute.  Otherwise a RuntimeException will be thrown.
152         */
153        public RuleValidationAttribute getRuleValidationAttribute() {
154            try {
155                RuleAttribute attrib = getRuleAttribute();
156                return KEWServiceLocator.getRuleValidationAttributeResolver().resolveRuleValidationAttribute(attrib.getName(), attrib.getApplicationId());
157            } catch (Exception e) {
158                throw new RuntimeException("Caught exception instantiating new " + getRuleAttribute().getResourceDescriptor(), e);
159            }
160        }
161    
162        public List<RuleExtensionBo> getRuleExtensions() {
163            return ruleExtensions;
164        }
165    
166        public Map<String, String> getRuleExtensionMap() {
167            Map<String, String> extensions = new HashMap<String, String>();
168            if (this.getRuleExtensions() != null) {
169                for (RuleExtensionBo ext : this.getRuleExtensions()) {
170                    for (RuleExtensionValue value : ext.getExtensionValues()) {
171                        extensions.put(value.getKey(), value.getValue());
172                    }
173                }
174            }
175            return extensions;
176        }
177    
178        public void setRuleExtensions(List<RuleExtensionBo> ruleExtensions) {
179            this.ruleExtensions = ruleExtensions;
180        }
181    
182        public RuleAttribute getRuleAttribute() {
183            if (ruleAttribute == null && ruleAttributeId != null) {
184                ruleAttribute = RuleAttribute.from(KewApiServiceLocator.getExtensionRepositoryService().getExtensionById(ruleAttributeId));
185            }
186            return ruleAttribute;
187        }
188    
189        public void setRuleAttribute(org.kuali.rice.kew.rule.bo.RuleAttribute ruleAttribute) {
190            this.ruleAttribute = ruleAttribute;
191        }
192    
193        public RuleTemplateBo getRuleTemplate() {
194            return ruleTemplate;
195        }
196    
197        public void setRuleTemplate(RuleTemplateBo ruleTemplate) {
198            this.ruleTemplate = ruleTemplate;
199        }
200    
201        public String getDefaultValue() {
202            return defaultValue;
203        }
204    
205        public void setDefaultValue(String defaultValue) {
206            this.defaultValue = defaultValue;
207        }
208    
209        public Integer getDisplayOrder() {
210            return displayOrder;
211        }
212    
213        public void setDisplayOrder(Integer displayOrder) {
214            this.displayOrder = displayOrder;
215        }
216    
217        public boolean isRequired() {
218            return (getRequired() == null) || (getRequired().booleanValue());
219        }
220    
221        public Boolean getRequired() {
222            return required;
223        }
224    
225        public void setRequired(Boolean required) {
226            this.required = required;
227        }
228    
229        public boolean isActive() {
230            return (getActive() == null) || (getActive().booleanValue());
231        }
232    
233        public Boolean getActive() {
234            return active;
235        }
236    
237        public void setActive(Boolean active) {
238            this.active = active;
239        }
240        
241        public void setActive(boolean active) {
242            this.active = active;
243        }
244    
245        public String getRuleAttributeId() {
246            return ruleAttributeId;
247        }
248    
249        public void setRuleAttributeId(String ruleAttributeId) {
250            this.ruleAttributeId = ruleAttributeId;
251        }
252    
253        public String getId() {
254            return id;
255        }
256    
257        public void setId(String id) {
258            this.id = id;
259        }
260    
261        public String getRuleTemplateId() {
262            return ruleTemplateId;
263        }
264    
265        public void setRuleTemplateId(String ruleTemplateId) {
266            this.ruleTemplateId = ruleTemplateId;
267        }
268    
269    }