View Javadoc

1   /**
2    * Copyright 2005-2011 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.XmlRootElement;
28  import javax.xml.bind.annotation.XmlType;
29  
30  import org.apache.commons.collections.CollectionUtils;
31  import org.apache.commons.lang.StringUtils;
32  import org.kuali.rice.core.api.CoreConstants;
33  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
34  import org.kuali.rice.core.api.mo.ModelBuilder;
35  import org.kuali.rice.kew.api.KewApiConstants;
36  import org.w3c.dom.Element;
37  
38  @XmlRootElement(name = RuleTemplate.Constants.ROOT_ELEMENT_NAME)
39  @XmlAccessorType(XmlAccessType.NONE)
40  @XmlType(name = RuleTemplate.Constants.TYPE_NAME, propOrder = {
41      RuleTemplate.Elements.NAME,
42      RuleTemplate.Elements.DESCRIPTION,
43      RuleTemplate.Elements.DELEGATION_TEMPLATE,
44      RuleTemplate.Elements.RULE_TEMPLATE_ATTRIBUTES,
45      RuleTemplate.Elements.RULE_TEMPLATE_OPTIONS,
46      RuleTemplate.Elements.ID,
47      CoreConstants.CommonElements.VERSION_NUMBER,
48      CoreConstants.CommonElements.OBJECT_ID,
49      CoreConstants.CommonElements.FUTURE_ELEMENTS
50  })
51  public final class RuleTemplate
52      extends AbstractDataTransferObject
53      implements RuleTemplateContract
54  {
55  
56      @XmlElement(name = Elements.NAME, required = false)
57      private final String name;
58      @XmlElement(name = Elements.DESCRIPTION, required = false)
59      private final String description;
60      @XmlElement(name = Elements.DELEGATION_TEMPLATE, required = false)
61      private final RuleTemplate delegationTemplate;
62      @XmlElement(name = Elements.RULE_TEMPLATE_ATTRIBUTES, required = false)
63      private final List<RuleTemplateAttribute> ruleTemplateAttributes;
64      @XmlElement(name = Elements.RULE_TEMPLATE_OPTIONS, required = false)
65      private final List<RuleTemplateOption> ruleTemplateOptions;
66      @XmlElement(name = Elements.ID, required = false)
67      private final String id;
68      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
69      private final Long versionNumber;
70      @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
71      private final String objectId;
72      @SuppressWarnings("unused")
73      @XmlAnyElement
74      private final Collection<Element> _futureElements = null;
75  
76      /**
77       * Private constructor used only by JAXB.
78       * 
79       */
80      private RuleTemplate() {
81          this.name = null;
82          this.description = null;
83          this.delegationTemplate = null;
84          this.ruleTemplateAttributes = null;
85          this.ruleTemplateOptions = null;
86          this.id = null;
87          this.versionNumber = null;
88          this.objectId = null;
89      }
90  
91      private RuleTemplate(Builder builder) {
92          this.name = builder.getName();
93          this.description = builder.getDescription();
94          this.delegationTemplate = builder.getDelegationTemplate() == null ? null : builder.getDelegationTemplate().build();
95          if (CollectionUtils.isNotEmpty(builder.getRuleTemplateAttributes())) {
96              List<RuleTemplateAttribute> rta = new ArrayList<RuleTemplateAttribute>();
97              for (RuleTemplateAttribute.Builder attribute : builder.getRuleTemplateAttributes()) {
98                  rta.add(attribute.build());
99              }
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 }