001    /**
002     * Copyright 2005-2012 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.api.rule;
017    
018    import java.io.Serializable;
019    import java.util.Collection;
020    import java.util.Map;
021    import javax.xml.bind.annotation.XmlAccessType;
022    import javax.xml.bind.annotation.XmlAccessorType;
023    import javax.xml.bind.annotation.XmlAnyElement;
024    import javax.xml.bind.annotation.XmlElement;
025    import javax.xml.bind.annotation.XmlRootElement;
026    import javax.xml.bind.annotation.XmlType;
027    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
028    
029    import org.apache.commons.lang.StringUtils;
030    import org.kuali.rice.core.api.CoreConstants;
031    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
032    import org.kuali.rice.core.api.mo.ModelBuilder;
033    import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
034    import org.kuali.rice.kew.api.extension.ExtensionDefinition;
035    import org.w3c.dom.Element;
036    
037    @XmlRootElement(name = RuleTemplateAttribute.Constants.ROOT_ELEMENT_NAME)
038    @XmlAccessorType(XmlAccessType.NONE)
039    @XmlType(name = RuleTemplateAttribute.Constants.TYPE_NAME, propOrder = {
040        RuleTemplateAttribute.Elements.DEFAULT_VALUE,
041        RuleTemplateAttribute.Elements.RULE_TEMPLATE_ID,
042        RuleTemplateAttribute.Elements.REQUIRED,
043        RuleTemplateAttribute.Elements.DISPLAY_ORDER,
044        RuleTemplateAttribute.Elements.RULE_ATTRIBUTE,
045        RuleTemplateAttribute.Elements.RULE_EXTENSIONS,
046        RuleTemplateAttribute.Elements.ID,
047        CoreConstants.CommonElements.VERSION_NUMBER,
048        CoreConstants.CommonElements.OBJECT_ID,
049        RuleTemplateAttribute.Elements.ACTIVE,
050        CoreConstants.CommonElements.FUTURE_ELEMENTS
051    })
052    public final class RuleTemplateAttribute
053        extends AbstractDataTransferObject
054        implements RuleTemplateAttributeContract,
055            Comparable<RuleTemplateAttribute>
056    {
057    
058        @XmlElement(name = Elements.DEFAULT_VALUE, required = false)
059        private final String defaultValue;
060        @XmlElement(name = Elements.RULE_TEMPLATE_ID, required = true)
061        private final String ruleTemplateId;
062        @XmlElement(name = Elements.REQUIRED, required = true)
063        private final boolean required;
064        @XmlElement(name = Elements.DISPLAY_ORDER, required = true)
065        private final Integer displayOrder;
066        @XmlElement(name = Elements.RULE_ATTRIBUTE, required = true)
067        private final ExtensionDefinition ruleAttribute;
068        @XmlElement(name = Elements.RULE_EXTENSIONS, required = false)
069        @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
070        private final Map<String, String> ruleExtensionMap;
071        @XmlElement(name = Elements.ID, required = false)
072        private final String id;
073        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
074        private final Long versionNumber;
075        @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
076        private final String objectId;
077        @XmlElement(name = Elements.ACTIVE, required = true)
078        private final boolean active;
079        @SuppressWarnings("unused")
080        @XmlAnyElement
081        private final Collection<Element> _futureElements = null;
082    
083        /**
084         * Private constructor used only by JAXB.
085         * 
086         */
087        private RuleTemplateAttribute() {
088            this.defaultValue = null;
089            this.ruleTemplateId = null;
090            this.required = false;
091            this.displayOrder = null;
092            this.ruleAttribute = null;
093            this.ruleExtensionMap = null;
094            this.id = null;
095            this.versionNumber = null;
096            this.objectId = null;
097            this.active = false;
098        }
099    
100        private RuleTemplateAttribute(Builder builder) {
101            this.defaultValue = builder.getDefaultValue();
102            this.ruleTemplateId = builder.getRuleTemplateId();
103            this.required = builder.isRequired();
104            this.displayOrder = builder.getDisplayOrder();
105            this.ruleAttribute = builder.getRuleAttribute() == null ? null : builder.getRuleAttribute().build();
106            this.ruleExtensionMap = builder.getRuleExtensionMap();
107            this.id = builder.getId();
108            this.versionNumber = builder.getVersionNumber();
109            this.objectId = builder.getObjectId();
110            this.active = builder.isActive();
111        }
112    
113        @Override
114        public String getDefaultValue() {
115            return this.defaultValue;
116        }
117    
118        @Override
119        public String getRuleTemplateId() {
120            return this.ruleTemplateId;
121        }
122    
123        @Override
124        public boolean isRequired() {
125            return this.required;
126        }
127    
128        @Override
129        public Integer getDisplayOrder() {
130            return this.displayOrder;
131        }
132    
133        @Override
134        public ExtensionDefinition getRuleAttribute() {
135            return this.ruleAttribute;
136        }
137    
138        @Override
139        public Map<String, String> getRuleExtensionMap() {
140            return this.ruleExtensionMap;
141        }
142    
143        @Override
144        public String getId() {
145            return this.id;
146        }
147    
148        @Override
149        public Long getVersionNumber() {
150            return this.versionNumber;
151        }
152    
153        @Override
154        public String getObjectId() {
155            return this.objectId;
156        }
157    
158        @Override
159        public boolean isActive() {
160            return this.active;
161        }
162    
163        public int compareTo(RuleTemplateAttribute ruleTemplateAttribute) {
164            if ((this.getDisplayOrder() != null) && (ruleTemplateAttribute.getDisplayOrder() != null)) {
165                return this.getDisplayOrder().compareTo(ruleTemplateAttribute.getDisplayOrder());
166            }
167            return 0;
168        }
169    
170    
171        /**
172         * A builder which can be used to construct {@link RuleTemplateAttribute} instances.  Enforces the constraints of the {@link RuleTemplateAttributeContract}.
173         * 
174         */
175        public final static class Builder
176            implements Serializable, ModelBuilder, RuleTemplateAttributeContract
177        {
178    
179            private String defaultValue;
180            private String ruleTemplateId;
181            private boolean required;
182            private Integer displayOrder;
183            private ExtensionDefinition.Builder ruleAttribute;
184            private Map<String, String> ruleExtensionMap;
185            private String id;
186            private Long versionNumber;
187            private String objectId;
188            private boolean active;
189    
190            private Builder(String ruleTemplateId,
191                    ExtensionDefinition.Builder ruleAttribute,
192                    boolean required,
193                    Integer displayOrder) {
194                setActive(true);
195                setRuleTemplateId(ruleTemplateId);
196                setRuleAttribute(ruleAttribute);
197                setRequired(required);
198                setDisplayOrder(displayOrder);
199            }
200    
201            public static Builder create(String ruleTemplateId,
202                    ExtensionDefinition.Builder ruleAttribute,
203                    boolean required,
204                    Integer displayOrder) {
205                return new Builder(ruleTemplateId, ruleAttribute, required, displayOrder);
206            }
207    
208            public static Builder create(RuleTemplateAttributeContract contract) {
209                if (contract == null) {
210                    throw new IllegalArgumentException("contract was null");
211                }
212                Builder builder = create(contract.getRuleTemplateId(),
213                    contract.getRuleAttribute()  == null ? null : ExtensionDefinition.Builder.create(contract.getRuleAttribute()),
214                    contract.isRequired(),
215                    contract.getDisplayOrder());
216                builder.setDefaultValue(contract.getDefaultValue());
217                builder.setRuleExtensionMap(contract.getRuleExtensionMap());
218                builder.setId(contract.getId());
219                builder.setVersionNumber(contract.getVersionNumber());
220                builder.setObjectId(contract.getObjectId());
221                builder.setActive(contract.isActive());
222                return builder;
223            }
224    
225            @Override
226            public RuleTemplateAttribute build() {
227                return new RuleTemplateAttribute(this);
228            }
229    
230            @Override
231            public String getDefaultValue() {
232                return this.defaultValue;
233            }
234    
235            @Override
236            public String getRuleTemplateId() {
237                return this.ruleTemplateId;
238            }
239    
240            @Override
241            public boolean isRequired() {
242                return this.required;
243            }
244    
245            @Override
246            public Integer getDisplayOrder() {
247                return this.displayOrder;
248            }
249    
250            @Override
251            public ExtensionDefinition.Builder getRuleAttribute() {
252                return this.ruleAttribute;
253            }
254    
255            @Override
256            public Map<String, String> getRuleExtensionMap() {
257                return this.ruleExtensionMap;
258            }
259    
260            @Override
261            public String getId() {
262                return this.id;
263            }
264    
265            @Override
266            public Long getVersionNumber() {
267                return this.versionNumber;
268            }
269    
270            @Override
271            public String getObjectId() {
272                return this.objectId;
273            }
274    
275            @Override
276            public boolean isActive() {
277                return this.active;
278            }
279    
280            public void setDefaultValue(String defaultValue) {
281                this.defaultValue = defaultValue;
282            }
283    
284            public void setRuleTemplateId(String ruleTemplateId) {
285                if (StringUtils.isBlank(ruleTemplateId)) {
286                    throw new IllegalArgumentException("ruleTemplateId was null or blank");
287                }
288                this.ruleTemplateId = ruleTemplateId;
289            }
290    
291            public void setRequired(boolean required) {
292                this.required = required;
293            }
294    
295            public void setDisplayOrder(Integer displayOrder) {
296                if (displayOrder == null) {
297                    throw new IllegalArgumentException("displayOrder was null");
298                }
299                this.displayOrder = displayOrder;
300            }
301    
302            public void setRuleAttribute(ExtensionDefinition.Builder ruleAttribute) {
303                if (ruleAttribute == null) {
304                    throw new IllegalArgumentException("ruleAttribute was null");
305                }
306                this.ruleAttribute = ruleAttribute;
307            }
308    
309            public void setRuleExtensionMap(Map<String, String> ruleExtensionMap) {
310                this.ruleExtensionMap = ruleExtensionMap;
311            }
312    
313            public void setId(String id) {
314                if (StringUtils.isWhitespace(id)) {
315                    throw new IllegalArgumentException("id was whitespace");
316                }
317                this.id = id;
318            }
319    
320            public void setVersionNumber(Long versionNumber) {
321                this.versionNumber = versionNumber;
322            }
323    
324            public void setObjectId(String objectId) {
325                this.objectId = objectId;
326            }
327    
328            public void setActive(boolean active) {
329                this.active = active;
330            }
331    
332        }
333    
334    
335        /**
336         * Defines some internal constants used on this class.
337         * 
338         */
339        static class Constants {
340    
341            final static String ROOT_ELEMENT_NAME = "ruleTemplateAttribute";
342            final static String TYPE_NAME = "RuleTemplateAttributeType";
343    
344        }
345    
346    
347        /**
348         * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
349         * 
350         */
351        static class Elements {
352    
353            final static String DEFAULT_VALUE = "defaultValue";
354            final static String RULE_TEMPLATE_ID = "ruleTemplateId";
355            final static String RULE_ATTRIBUTE_ID = "ruleAttributeId";
356            final static String REQUIRED = "required";
357            final static String DISPLAY_ORDER = "displayOrder";
358            final static String RULE_ATTRIBUTE = "ruleAttribute";
359            final static String RULE_EXTENSIONS = "ruleExtensionMap";
360            final static String ID = "id";
361            final static String ACTIVE = "active";
362    
363        }
364    
365    }