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.ArrayList;
020    import java.util.Collection;
021    import java.util.Collections;
022    import java.util.List;
023    import javax.xml.bind.annotation.XmlAccessType;
024    import javax.xml.bind.annotation.XmlAccessorType;
025    import javax.xml.bind.annotation.XmlAnyElement;
026    import javax.xml.bind.annotation.XmlElement;
027    import javax.xml.bind.annotation.XmlElementWrapper;
028    import javax.xml.bind.annotation.XmlRootElement;
029    import javax.xml.bind.annotation.XmlType;
030    
031    import org.apache.commons.collections.CollectionUtils;
032    import org.apache.commons.lang.StringUtils;
033    import org.kuali.rice.core.api.CoreConstants;
034    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
035    import org.kuali.rice.core.api.mo.ModelBuilder;
036    import org.kuali.rice.kew.api.KewApiConstants;
037    import org.w3c.dom.Element;
038    
039    @XmlRootElement(name = RuleTemplate.Constants.ROOT_ELEMENT_NAME)
040    @XmlAccessorType(XmlAccessType.NONE)
041    @XmlType(name = RuleTemplate.Constants.TYPE_NAME, propOrder = {
042        RuleTemplate.Elements.NAME,
043        RuleTemplate.Elements.DESCRIPTION,
044        RuleTemplate.Elements.DELEGATION_TEMPLATE,
045        RuleTemplate.Elements.RULE_TEMPLATE_ATTRIBUTES,
046        RuleTemplate.Elements.RULE_TEMPLATE_OPTIONS,
047        RuleTemplate.Elements.ID,
048        CoreConstants.CommonElements.VERSION_NUMBER,
049        CoreConstants.CommonElements.OBJECT_ID,
050        CoreConstants.CommonElements.FUTURE_ELEMENTS
051    })
052    public final class RuleTemplate
053        extends AbstractDataTransferObject
054        implements RuleTemplateContract
055    {
056    
057        @XmlElement(name = Elements.NAME, required = false)
058        private final String name;
059    
060        @XmlElement(name = Elements.DESCRIPTION, required = false)
061        private final String description;
062    
063        @XmlElement(name = Elements.DELEGATION_TEMPLATE, required = false)
064        private final RuleTemplate delegationTemplate;
065    
066        @XmlElementWrapper(name = Elements.RULE_TEMPLATE_ATTRIBUTES, required = false)
067        @XmlElement(name = Elements.RULE_TEMPLATE_ATTRIBUTE, required = false)
068        private final List<RuleTemplateAttribute> ruleTemplateAttributes;
069    
070        @XmlElementWrapper(name = Elements.RULE_TEMPLATE_OPTIONS, required = false)
071        @XmlElement(name = Elements.RULE_TEMPLATE_OPTION, required = false)
072        private final List<RuleTemplateOption> ruleTemplateOptions;
073    
074        @XmlElement(name = Elements.ID, required = false)
075        private final String id;
076    
077        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
078        private final Long versionNumber;
079    
080        @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
081        private final String objectId;
082    
083        @SuppressWarnings("unused")
084        @XmlAnyElement
085        private final Collection<Element> _futureElements = null;
086    
087        /**
088         * Private constructor used only by JAXB.
089         * 
090         */
091        private RuleTemplate() {
092            this.name = null;
093            this.description = null;
094            this.delegationTemplate = null;
095            this.ruleTemplateAttributes = null;
096            this.ruleTemplateOptions = null;
097            this.id = null;
098            this.versionNumber = null;
099            this.objectId = null;
100        }
101    
102        private RuleTemplate(Builder builder) {
103            this.name = builder.getName();
104            this.description = builder.getDescription();
105            this.delegationTemplate = builder.getDelegationTemplate() == null ? null : builder.getDelegationTemplate().build();
106            if (CollectionUtils.isNotEmpty(builder.getRuleTemplateAttributes())) {
107                List<RuleTemplateAttribute> rta = new ArrayList<RuleTemplateAttribute>();
108                for (RuleTemplateAttribute.Builder attribute : builder.getRuleTemplateAttributes()) {
109                    rta.add(attribute.build());
110                }
111                this.ruleTemplateAttributes = Collections.unmodifiableList(rta);
112            } else {
113                this.ruleTemplateAttributes = Collections.emptyList();
114            }
115            if (CollectionUtils.isNotEmpty(builder.getRuleTemplateOptions())) {
116                List<RuleTemplateOption> rto = new ArrayList<RuleTemplateOption>();
117                for (RuleTemplateOption.Builder option : builder.getRuleTemplateOptions()) {
118                    rto.add(option.build());
119                }
120                this.ruleTemplateOptions = Collections.unmodifiableList(rto);
121            } else {
122                this.ruleTemplateOptions = Collections.emptyList();
123            }
124            this.id = builder.getId();
125            this.versionNumber = builder.getVersionNumber();
126            this.objectId = builder.getObjectId();
127        }
128    
129        @Override
130        public String getName() {
131            return this.name;
132        }
133    
134        @Override
135        public String getDescription() {
136            return this.description;
137        }
138    
139        @Override
140        public RuleTemplate getDelegationTemplate() {
141            return this.delegationTemplate;
142        }
143    
144        @Override
145        public List<RuleTemplateAttribute> getRuleTemplateAttributes() {
146            return this.ruleTemplateAttributes;
147        }
148    
149        public List<RuleTemplateAttribute> getActiveRuleTemplateAttributes() {
150            List<RuleTemplateAttribute> activeAttributes = new ArrayList<RuleTemplateAttribute>();
151            for (RuleTemplateAttribute templateAttribute : getRuleTemplateAttributes())
152            {
153                if (templateAttribute.isActive())
154                {
155                    activeAttributes.add(templateAttribute);
156                }
157            }
158            Collections.sort(activeAttributes);
159            return activeAttributes;
160        }
161    
162        @Override
163        public List<RuleTemplateOption> getRuleTemplateOptions() {
164            return this.ruleTemplateOptions;
165        }
166    
167        @Override
168        public String getId() {
169            return this.id;
170        }
171    
172        @Override
173        public Long getVersionNumber() {
174            return this.versionNumber;
175        }
176    
177        @Override
178        public String getObjectId() {
179            return this.objectId;
180        }
181    
182    
183        /**
184         * A builder which can be used to construct {@link RuleTemplate} instances.  Enforces the constraints of the {@link RuleTemplateContract}.
185         * 
186         */
187        public final static class Builder
188            implements Serializable, ModelBuilder, RuleTemplateContract
189        {
190    
191            private String name;
192            private String description;
193            private RuleTemplate.Builder delegationTemplate;
194            private List<RuleTemplateAttribute.Builder> ruleTemplateAttributes;
195            private List<RuleTemplateOption.Builder> ruleTemplateOptions;
196            private String id;
197            private Long versionNumber;
198            private String objectId;
199    
200            private Builder() {
201                // TODO modify this constructor as needed to pass any required values and invoke the appropriate 'setter' methods
202            }
203    
204            public static Builder create() {
205                // TODO modify as needed to pass any required values and add them to the signature of the 'create' method
206                return new Builder();
207            }
208    
209            public static Builder create(RuleTemplateContract contract) {
210                if (contract == null) {
211                    throw new IllegalArgumentException("contract was null");
212                }
213                // TODO if create() is modified to accept required parameters, this will need to be modified
214                Builder builder = create();
215                builder.setName(contract.getName());
216                builder.setDescription(contract.getDescription());
217                builder.setDelegationTemplate(
218                        contract.getDelegationTemplate() == null ?
219                                null : RuleTemplate.Builder.create(contract.getDelegationTemplate()));
220                if (CollectionUtils.isNotEmpty(contract.getRuleTemplateAttributes())) {
221                    List<RuleTemplateAttribute.Builder> attrs = new ArrayList<RuleTemplateAttribute.Builder>();
222                    for (RuleTemplateAttributeContract attr : contract.getRuleTemplateAttributes()) {
223                        attrs.add(RuleTemplateAttribute.Builder.create(attr));
224                    }
225                    builder.setRuleTemplateAttributes(attrs);
226                } else {
227                    builder.setRuleTemplateAttributes(Collections.<RuleTemplateAttribute.Builder>emptyList());
228                }
229                if (CollectionUtils.isNotEmpty(contract.getRuleTemplateOptions())) {
230                    List<RuleTemplateOption.Builder> options = new ArrayList<RuleTemplateOption.Builder>();
231                    for (RuleTemplateOptionContract option : contract.getRuleTemplateOptions()) {
232                        options.add(RuleTemplateOption.Builder.create(option));
233                    }
234                    builder.setRuleTemplateOptions(options);
235                } else {
236                    builder.setRuleTemplateOptions(Collections.<RuleTemplateOption.Builder>emptyList());
237                }
238                builder.setId(contract.getId());
239                builder.setVersionNumber(contract.getVersionNumber());
240                builder.setObjectId(contract.getObjectId());
241                return builder;
242            }
243    
244            public RuleTemplate build() {
245                return new RuleTemplate(this);
246            }
247    
248            @Override
249            public String getName() {
250                return this.name;
251            }
252    
253            @Override
254            public String getDescription() {
255                return this.description;
256            }
257    
258            @Override
259            public RuleTemplate.Builder getDelegationTemplate() {
260                return this.delegationTemplate;
261            }
262    
263            @Override
264            public List<RuleTemplateAttribute.Builder> getRuleTemplateAttributes() {
265                return this.ruleTemplateAttributes;
266            }
267    
268            @Override
269            public List<RuleTemplateOption.Builder> getRuleTemplateOptions() {
270                return this.ruleTemplateOptions;
271            }
272    
273            @Override
274            public String getId() {
275                return this.id;
276            }
277    
278            @Override
279            public Long getVersionNumber() {
280                return this.versionNumber;
281            }
282    
283            @Override
284            public String getObjectId() {
285                return this.objectId;
286            }
287    
288            public void setName(String name) {
289                if (StringUtils.isBlank(name)) {
290                    throw new IllegalArgumentException("name is null or blank");
291                }
292                this.name = name;
293            }
294    
295            public void setDescription(String description) {
296                this.description = description;
297            }
298    
299            public void setDelegationTemplate(RuleTemplate.Builder delegationTemplate) {
300                this.delegationTemplate = delegationTemplate;
301            }
302    
303            public void setRuleTemplateAttributes(List<RuleTemplateAttribute.Builder> ruleTemplateAttributes) {
304                this.ruleTemplateAttributes = Collections.unmodifiableList(ruleTemplateAttributes);
305            }
306    
307            public void setRuleTemplateOptions(List<RuleTemplateOption.Builder> ruleTemplateOptions) {
308                this.ruleTemplateOptions = Collections.unmodifiableList(ruleTemplateOptions);
309            }
310    
311            public void setId(String id) {
312                if (StringUtils.isWhitespace(id)) {
313                    throw new IllegalArgumentException("id was whitespace");
314                }
315                this.id = id;
316            }
317    
318            public void setVersionNumber(Long versionNumber) {
319                this.versionNumber = versionNumber;
320            }
321    
322            public void setObjectId(String objectId) {
323                this.objectId = objectId;
324            }
325    
326        }
327    
328    
329        /**
330         * Defines some internal constants used on this class.
331         * 
332         */
333        static class Constants {
334    
335            final static String ROOT_ELEMENT_NAME = "ruleTemplate";
336            final static String TYPE_NAME = "RuleTemplateType";
337    
338        }
339    
340    
341        /**
342         * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
343         * 
344         */
345        static class Elements {
346    
347            final static String NAME = "name";
348            final static String DESCRIPTION = "description";
349            final static String DELEGATION_TEMPLATE = "delegationTemplate";
350            final static String RULE_TEMPLATE_ATTRIBUTES = "ruleTemplateAttributes";
351            final static String RULE_TEMPLATE_OPTIONS = "ruleTemplateOptions";
352            final static String RULE_TEMPLATE_ATTRIBUTE = "ruleTemplateAttribute";
353            final static String RULE_TEMPLATE_OPTION = "ruleTemplateOption";
354            final static String ID = "id";
355    
356        }
357    
358        public static class Cache {
359            public static final String NAME = KewApiConstants.Namespaces.KEW_NAMESPACE_2_0 + "/" + RuleTemplate.Constants.TYPE_NAME;
360        }
361    }