View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.api.rule;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.List;
23  import javax.xml.bind.annotation.XmlAccessType;
24  import javax.xml.bind.annotation.XmlAccessorType;
25  import javax.xml.bind.annotation.XmlAnyElement;
26  import javax.xml.bind.annotation.XmlElement;
27  import javax.xml.bind.annotation.XmlElementWrapper;
28  import javax.xml.bind.annotation.XmlRootElement;
29  import javax.xml.bind.annotation.XmlType;
30  
31  import org.apache.commons.collections.CollectionUtils;
32  import org.apache.commons.lang.StringUtils;
33  import org.kuali.rice.core.api.CoreConstants;
34  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
35  import org.kuali.rice.core.api.mo.ModelBuilder;
36  import org.kuali.rice.kew.api.KewApiConstants;
37  import org.w3c.dom.Element;
38  
39  @XmlRootElement(name = RuleTemplate.Constants.ROOT_ELEMENT_NAME)
40  @XmlAccessorType(XmlAccessType.NONE)
41  @XmlType(name = RuleTemplate.Constants.TYPE_NAME, propOrder = {
42      RuleTemplate.Elements.NAME,
43      RuleTemplate.Elements.DESCRIPTION,
44      RuleTemplate.Elements.DELEGATION_TEMPLATE,
45      RuleTemplate.Elements.RULE_TEMPLATE_ATTRIBUTES,
46      RuleTemplate.Elements.RULE_TEMPLATE_OPTIONS,
47      RuleTemplate.Elements.ID,
48      CoreConstants.CommonElements.VERSION_NUMBER,
49      CoreConstants.CommonElements.OBJECT_ID,
50      CoreConstants.CommonElements.FUTURE_ELEMENTS
51  })
52  public final class RuleTemplate
53      extends AbstractDataTransferObject
54      implements RuleTemplateContract
55  {
56  
57      @XmlElement(name = Elements.NAME, required = false)
58      private final String name;
59  
60      @XmlElement(name = Elements.DESCRIPTION, required = false)
61      private final String description;
62  
63      @XmlElement(name = Elements.DELEGATION_TEMPLATE, required = false)
64      private final RuleTemplate delegationTemplate;
65  
66      @XmlElementWrapper(name = Elements.RULE_TEMPLATE_ATTRIBUTES, required = false)
67      @XmlElement(name = Elements.RULE_TEMPLATE_ATTRIBUTE, required = false)
68      private final List<RuleTemplateAttribute> ruleTemplateAttributes;
69  
70      @XmlElementWrapper(name = Elements.RULE_TEMPLATE_OPTIONS, required = false)
71      @XmlElement(name = Elements.RULE_TEMPLATE_OPTION, required = false)
72      private final List<RuleTemplateOption> ruleTemplateOptions;
73  
74      @XmlElement(name = Elements.ID, required = false)
75      private final String id;
76  
77      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
78      private final Long versionNumber;
79  
80      @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
81      private final String objectId;
82  
83      @SuppressWarnings("unused")
84      @XmlAnyElement
85      private final Collection<Element> _futureElements = null;
86  
87      /**
88       * Private constructor used only by JAXB.
89       * 
90       */
91      private RuleTemplate() {
92          this.name = null;
93          this.description = null;
94          this.delegationTemplate = null;
95          this.ruleTemplateAttributes = null;
96          this.ruleTemplateOptions = null;
97          this.id = null;
98          this.versionNumber = null;
99          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 }