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