View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package org.kuali.rice.krms.api.repository;
6   
7   import java.io.Serializable;
8   import java.util.ArrayList;
9   import java.util.Collection;
10  import java.util.Collections;
11  import java.util.List;
12  import javax.xml.bind.annotation.XmlAccessType;
13  import javax.xml.bind.annotation.XmlAccessorType;
14  import javax.xml.bind.annotation.XmlAnyElement;
15  import javax.xml.bind.annotation.XmlElement;
16  import javax.xml.bind.annotation.XmlRootElement;
17  import javax.xml.bind.annotation.XmlType;
18  import org.kuali.rice.core.api.CoreConstants;
19  import org.kuali.rice.core.api.mo.ModelBuilder;
20  import org.kuali.rice.krms.api.KrmsConstants;
21  
22  /**
23   * Concrete model object implementation of a natural language tree immutable.
24   * Instances of natural language tree can be (un)marshalled to and from XML.
25   *
26   * @see NaturalLanguageTreeContract
27   */
28  @XmlRootElement(name = NaturalLanguageTree.Constants.ROOT_ELEMENT_NAME)
29  @XmlAccessorType(XmlAccessType.NONE)
30  @XmlType(name = NaturalLanguageTree.Constants.TYPE_NAME, propOrder = {
31      NaturalLanguageTree.Elements.NATURAL_LANGUAGE,
32      NaturalLanguageTree.Elements.CHILDREN,
33      CoreConstants.CommonElements.FUTURE_ELEMENTS
34  })
35  public class NaturalLanguageTree implements NaturalLanguageTreeContract {
36  
37      private static final long serialVersionUID = 2783959459503209577L;
38      @XmlElement(name = NaturalLanguageTree.Elements.NATURAL_LANGUAGE, required = false)
39      private String naturalLanguage;
40      @XmlElement(name = NaturalLanguageTree.Elements.CHILDREN, required = false)
41      private List<NaturalLanguageTree> children;
42      @SuppressWarnings("unused")
43      @XmlAnyElement
44      private final Collection<org.w3c.dom.Element> _futureElements = null;
45  
46      /**
47       * This constructor should never be called. It is only present for use
48       * during JAXB unmarshalling.
49       */
50      public NaturalLanguageTree() {
51          this.naturalLanguage = null;
52          this.children = null;
53      }
54  
55      @Override
56      public String getNaturalLanguage() {
57          return naturalLanguage;
58      }
59  
60      @Override
61      public List<NaturalLanguageTree> getChildren() {
62          return children;
63      }
64  
65      /**
66       * Constructs a KRMS Repository Agenda object from the given builder. This
67       * constructor is private and should only ever be invoked from the builder.
68       *
69       * @param builder the Builder from which to construct the Agenda
70       */
71      private NaturalLanguageTree(Builder builder) {
72          this.naturalLanguage = builder.getNaturalLanguage();
73          List<NaturalLanguageTree> list = null;
74          if (builder.getChildren() != null) {
75              list = new ArrayList<NaturalLanguageTree>(builder.getChildren().size());
76              for (NaturalLanguageTreeContract nltree : builder.getChildren()) {
77                  list.add(Builder.create(nltree).build());
78              }
79              this.children = Collections.unmodifiableList(list);
80          }
81      }
82  
83      /**
84       * This builder is used to construct instances of KRMS Repository Agenda. It
85       * enforces the constraints of the {@link NaturalLanguageTreeContract}.
86       */
87      public static class Builder implements NaturalLanguageTreeContract, ModelBuilder, Serializable {
88  
89          private static final long serialVersionUID = -8862851720709537839L;
90          private String naturalLanguage;
91          private List<? extends NaturalLanguageTreeContract> children;
92   
93          /**
94           * Private constructor for creating a builder with all of it's required
95           * attributes.
96           */
97          private Builder() {
98          }        
99          
100         /**
101          * Private constructor for creating a builder with all of it's required
102          * attributes.
103          */
104         private Builder(String naturalLanguage, List<? extends NaturalLanguageTreeContract> children) {
105             setNaturalLanguage(naturalLanguage);
106             setChildren(children);
107         }
108 
109         @Override
110         public String getNaturalLanguage() {
111             return naturalLanguage;
112         }
113 
114         public void setNaturalLanguage(String naturalLanguage) {
115             this.naturalLanguage = naturalLanguage;
116         }
117 
118         @Override
119         public List<? extends NaturalLanguageTreeContract> getChildren() {
120             return children;
121         }
122 
123         public void setChildren(List<? extends NaturalLanguageTreeContract> children) {
124             this.children = children;
125         }
126 
127         
128         /**
129          * Creates a builder by populating it with data from the given
130          * {@link NaturalLanguageTreeContract}.
131          *
132          * @param contract the contract from which to populate this builder
133          * @return an instance of the builder populated with data from the
134          * contract
135          * @throws IllegalArgumentException if the contract is null
136          */
137         public static NaturalLanguageTree.Builder create() {
138             NaturalLanguageTree.Builder builder = new NaturalLanguageTree.Builder();
139             return builder;
140         }
141         /**
142          * Creates a builder by populating it with data from the given
143          * {@link NaturalLanguageTreeContract}.
144          *
145          * @param contract the contract from which to populate this builder
146          * @return an instance of the builder populated with data from the
147          * contract
148          * @throws IllegalArgumentException if the contract is null
149          */
150         public static NaturalLanguageTree.Builder create(NaturalLanguageTreeContract contract) {
151             if (contract == null) {
152                 throw new IllegalArgumentException("contract is null");
153             }
154             NaturalLanguageTree.Builder builder = new NaturalLanguageTree.Builder(contract.getNaturalLanguage(), contract.getChildren());
155 
156             return builder;
157         }
158 
159         /**
160          * Builds an instance of a Natural Language Tree based on the current state of the
161          * builder.
162          *
163          * @return the fully-constructed Agenda
164          */
165         @Override
166         public NaturalLanguageTree build() {
167             return new NaturalLanguageTree(this);
168         }
169     }
170 
171     /**
172      * Defines some constants used on this class.
173      */
174     public static class Constants {
175 
176         final static String ROOT_ELEMENT_NAME = "naturalLanguageTree";
177         final static String TYPE_NAME = "NaturalLanguageTreeType";
178         final static String[] HASH_CODE_EQUALS_EXCLUDE = {"_futureElements"};
179         public final static String EVENT = "Event";   // key for event attribute
180     }
181 
182     /**
183      * A private class which exposes constants which define the XML element
184      * names to use when this object is marshalled to XML.
185      */
186     public static class Elements {
187 
188         final static String NATURAL_LANGUAGE = "naturalLanguage";
189         final static String CHILDREN = "children";
190     }
191 
192     public static class Cache {
193 
194         public static final String NAME = KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0 + "/" + NaturalLanguageTree.Constants.TYPE_NAME;
195     }
196 }