001/**
002 * Copyright 2005-2017 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 */
016package org.kuali.rice.kew.rule.bo;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kew.api.KewApiConstants;
020import org.kuali.rice.kew.api.extension.ExtensionDefinition;
021import org.kuali.rice.kew.api.extension.ExtensionDefinitionContract;
022import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
023import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
024
025import javax.persistence.Basic;
026import javax.persistence.CascadeType;
027import javax.persistence.Column;
028import javax.persistence.Entity;
029import javax.persistence.FetchType;
030import javax.persistence.GeneratedValue;
031import javax.persistence.Id;
032import javax.persistence.Lob;
033import javax.persistence.OneToMany;
034import javax.persistence.Table;
035import javax.persistence.Transient;
036import java.util.ArrayList;
037import java.util.HashMap;
038import java.util.List;
039import java.util.Map;
040
041
042/**
043 * Model bean defining a rule attribute.  Includes the classname of the attribute
044 * class, as well as it's name and other information.
045 *
046 * @author Kuali Rice Team (rice.collab@kuali.org)
047 */
048@Entity
049@Table(name="KREW_RULE_ATTR_T")
050public class RuleAttribute extends PersistableBusinessObjectBase implements ExtensionDefinitionContract {
051
052        private static final long serialVersionUID = 1027673603158346349L;
053
054        @Id
055    @PortableSequenceGenerator(name="KREW_RTE_TMPL_S")
056        @GeneratedValue(generator="KREW_RTE_TMPL_S")
057        @Column(name="RULE_ATTR_ID")
058        private String id;
059    @Column(name="NM")
060        private String name;
061    @Column(name="LBL")
062        private String label;
063    @Column(name="RULE_ATTR_TYP_CD")
064        private String type;
065    @Column(name="CLS_NM")
066        private String resourceDescriptor;
067    @Column(name="DESC_TXT")
068        private String description;
069    @Lob
070        @Basic(fetch=FetchType.LAZY)
071        @Column(name="XML")
072        private String xmlConfigData;
073
074    @Column(name="APPL_ID")
075        private String applicationId;
076    
077    @OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE},
078           targetEntity=RuleTemplateAttributeBo.class, mappedBy="ruleAttribute")
079        private List ruleTemplateAttributes;
080    @Transient
081    private List validValues;
082    
083    // required to be lookupable
084    @Transient
085    private String returnUrl;
086
087    public RuleAttribute() {
088        ruleTemplateAttributes = new ArrayList();
089        validValues = new ArrayList();
090    }
091
092    public List getValidValues() {
093        return validValues;
094    }
095    public void setValidValues(List ruleAttributeValidValues) {
096        this.validValues = ruleAttributeValidValues;
097    }
098    public List getRuleTemplateAttributes() {
099        return ruleTemplateAttributes;
100    }
101    public void setRuleTemplateAttributes(List ruleTemplateAttributes) {
102        this.ruleTemplateAttributes = ruleTemplateAttributes;
103    }
104    public String getDescription() {
105        return description;
106    }
107    public void setDescription(String description) {
108        this.description = description;
109    }
110    public String getLabel() {
111        return label;
112    }
113    public void setLabel(String label) {
114        this.label = label;
115    }
116
117    public String getName() {
118        return name;
119    }
120    public void setName(String name) {
121        this.name = name;
122    }
123    public String getId() {
124        return id;
125    }
126    public void setId(String id) {
127        this.id = id;
128    }
129    public String getType() {
130        return type;
131    }
132    public void setType(String type) {
133        this.type = type;
134    }
135
136    /**
137     * @return Returns the resourceDescriptor.
138     */
139    public String getResourceDescriptor() {
140      return resourceDescriptor;
141    }
142    /**
143     * @param resourceDescriptor The className to set.
144     */
145    public void setResourceDescriptor(String resourceDescriptor) {
146      this.resourceDescriptor = resourceDescriptor;
147    }
148    
149    public String getRuleAttributeActionsUrl() {
150        return "<a href=\"RuleAttributeReport.do?id="+ id +"\" >report</a>";
151    }
152    
153    public String getReturnUrl() {
154        return returnUrl;
155    }
156    public void setReturnUrl(String returnUrl) {
157        this.returnUrl = returnUrl;
158    }
159
160        public String getXmlConfigData() {
161                return xmlConfigData;
162        }
163
164    @Override
165    public Map<String, String> getConfiguration() {
166        Map<String, String> config = new HashMap<String, String>();
167        if (StringUtils.isNotBlank(getXmlConfigData())) {
168            config.put(KewApiConstants.ATTRIBUTE_XML_CONFIG_DATA, getXmlConfigData());
169        }
170        return config;
171    }
172
173        public void setXmlConfigData(String xmlConfigData) {
174                this.xmlConfigData = xmlConfigData;
175        }
176
177    @Override
178        public String getApplicationId() {
179                return applicationId;
180        }
181
182        public void setApplicationId(String applicationId) {
183                this.applicationId = applicationId;
184        }
185
186    public boolean isWorkflowAttribute() {
187        return isWorkflowAttribute(getType());
188    }
189
190    public static boolean isWorkflowAttribute(String type) {
191        return KewApiConstants.RULE_ATTRIBUTE_TYPE.equals(type) ||
192            KewApiConstants.RULE_XML_ATTRIBUTE_TYPE.equals(type);
193    }
194
195    public static ExtensionDefinition to(RuleAttribute ruleAttribute) {
196        if (ruleAttribute == null) {
197            return null;
198        }
199        return ExtensionDefinition.Builder.create(ruleAttribute).build();
200    }
201
202    public static RuleAttribute from(ExtensionDefinition im) {
203        if (im == null) {
204            return null;
205        }
206        RuleAttribute bo = new RuleAttribute();
207        bo.setApplicationId(im.getApplicationId());
208        bo.setDescription(im.getDescription());
209        bo.setResourceDescriptor(im.getResourceDescriptor());
210        bo.setId(im.getId());
211        bo.setLabel(im.getLabel());
212        bo.setType(im.getType());
213        bo.setVersionNumber(im.getVersionNumber());
214        bo.setXmlConfigData(im.getConfiguration().get(KewApiConstants.ATTRIBUTE_XML_CONFIG_DATA));
215
216        return bo;
217    }
218}