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