001/**
002 * Copyright 2005-2015 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;
017
018import org.apache.commons.lang.ObjectUtils;
019import org.kuali.rice.core.api.util.collect.CollectionUtils;
020import org.kuali.rice.kew.api.rule.RuleExtension;
021import org.kuali.rice.kew.api.rule.RuleExtensionContract;
022import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
023
024import javax.persistence.CascadeType;
025import javax.persistence.Column;
026import javax.persistence.Entity;
027import javax.persistence.FetchType;
028import javax.persistence.GeneratedValue;
029import javax.persistence.Id;
030import javax.persistence.JoinColumn;
031import javax.persistence.ManyToOne;
032import javax.persistence.OneToMany;
033import javax.persistence.Table;
034import javax.persistence.Version;
035import java.io.Serializable;
036import java.util.ArrayList;
037import java.util.HashMap;
038import java.util.List;
039import java.util.Map;
040
041/**
042 * An extension of a {@link RuleBaseValues}.  Provides attribute-specific data
043 * extensions to the rule for a particular {@link org.kuali.rice.kew.rule.bo.RuleAttribute}.  Contains
044 * a List of {@link RuleExtensionValue}s.
045 * 
046 * @see RuleBaseValues
047 * @see org.kuali.rice.kew.rule.bo.RuleAttribute
048 * @see RuleExtensionValue
049 *
050 * @author Kuali Rice Team (rice.collab@kuali.org)
051 */
052@Entity
053@Table(name="KREW_RULE_EXT_T")
054public class RuleExtensionBo implements RuleExtensionContract, Serializable {
055
056        private static final long serialVersionUID = 8178135296413950516L;
057
058        @Id
059        @GeneratedValue(generator="KREW_RTE_TMPL_S")
060        @Column(name="RULE_EXT_ID")
061        private String ruleExtensionId;
062
063        @Column(name="RULE_TMPL_ATTR_ID", insertable=false, updatable=false)
064        private String ruleTemplateAttributeId;
065
066    @Column(name="RULE_ID", insertable = false, updatable = false)
067    private String ruleBaseValuesId;
068
069
070        @Version
071        @Column(name="VER_NBR")
072        private Long versionNumber;
073        
074        @ManyToOne(fetch=FetchType.EAGER)
075        @JoinColumn(name="RULE_ID", nullable = false)
076        private RuleBaseValues ruleBaseValues;
077
078        @ManyToOne(fetch=FetchType.EAGER)
079        @JoinColumn(name="RULE_TMPL_ATTR_ID")
080        private RuleTemplateAttributeBo ruleTemplateAttribute;
081
082        @OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.ALL}, mappedBy="extension")
083        private List<RuleExtensionValue> extensionValues;
084
085        public RuleExtensionBo() {
086                extensionValues = new ArrayList<RuleExtensionValue>();
087        }
088
089        public List<RuleExtensionValue> getExtensionValues() {
090                return extensionValues;
091        }
092
093        public void setExtensionValues(List<RuleExtensionValue> extensionValues) {
094                this.extensionValues = extensionValues;
095        }
096
097    @Override
098        public RuleTemplateAttributeBo getRuleTemplateAttribute() {
099                return ruleTemplateAttribute;
100        }
101
102    @Override
103    public Map<String, String> getExtensionValuesMap() {
104        Map<String, String> extensions = new HashMap<String, String>();
105        for (RuleExtensionValue value : getExtensionValues()) {
106            extensions.put(value.getKey(), value.getValue());
107        }
108        return extensions;
109    }
110
111    public void setRuleTemplateAttribute(RuleTemplateAttributeBo ruleTemplateAttribute) {
112                this.ruleTemplateAttribute = ruleTemplateAttribute;
113        }
114
115        public RuleExtensionValue getRuleExtensionValue(int index) {
116                while (getExtensionValues().size() <= index) {
117                        getExtensionValues().add(new RuleExtensionValue());
118                }
119                return (RuleExtensionValue) getExtensionValues().get(index);
120        }
121
122        public RuleBaseValues getRuleBaseValues() {
123                return ruleBaseValues;
124        }
125
126        public void setRuleBaseValues(RuleBaseValues ruleBaseValues) {
127                this.ruleBaseValues = ruleBaseValues;
128        }
129
130        public Long getVersionNumber() {
131                return versionNumber;
132        }
133
134        public void setVersionNumber(Long versionNumber) {
135                this.versionNumber = versionNumber;
136        }
137
138        public String getRuleBaseValuesId() {
139                return ruleBaseValuesId;
140        }
141
142        public void setRuleBaseValuesId(String ruleBaseValuesId) {
143                this.ruleBaseValuesId = ruleBaseValuesId;
144        }
145
146        public String getRuleExtensionId() {
147                return ruleExtensionId;
148        }
149
150        public void setRuleExtensionId(String ruleExtensionId) {
151                this.ruleExtensionId = ruleExtensionId;
152        }
153
154        public String getRuleTemplateAttributeId() {
155                return ruleTemplateAttributeId;
156        }
157
158        public void setRuleTemplateAttributeId(String ruleTemplateAttributeId) {
159                this.ruleTemplateAttributeId = ruleTemplateAttributeId;
160        }
161
162    public boolean equals(Object o) {
163        if (o == null) return false;
164        if (!(o instanceof RuleExtensionBo)) return false;
165        RuleExtensionBo pred = (RuleExtensionBo) o;
166        return ObjectUtils.equals(ruleBaseValues.getRuleTemplate(), pred.getRuleBaseValues().getRuleTemplate()) &&
167               ObjectUtils.equals(ruleTemplateAttribute, pred.getRuleTemplateAttribute()) &&
168               CollectionUtils.collectionsEquivalent(extensionValues, pred.getExtensionValues());
169    }
170
171    public String toString() {
172        return "[RuleExtension:"
173               +  " ruleExtensionId=" + ruleExtensionId
174               + ", ruleTemplateAttributeId=" + ruleTemplateAttributeId
175               + ", ruleBaseValuesId=" + ruleBaseValuesId
176               + ", ruleBaseValues=" + ruleBaseValues
177               + ", ruleTemplateAttribute=" + ruleTemplateAttributeId
178               + ", versionNumber=" + versionNumber
179               + "]";
180    }
181
182    /**
183     * Converts a mutable bo to its immutable counterpart
184     * @param bo the mutable business object
185     * @return the immutable object
186     */
187    public static RuleExtension to(RuleExtensionBo bo) {
188        if (bo == null) {
189            return null;
190        }
191        return RuleExtension.Builder.create(bo).build();
192    }
193
194}