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  package org.kuali.rice.coreservice.api.namespace;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  
21  import javax.xml.bind.annotation.XmlAccessType;
22  import javax.xml.bind.annotation.XmlAccessorType;
23  import javax.xml.bind.annotation.XmlAnyElement;
24  import javax.xml.bind.annotation.XmlElement;
25  import javax.xml.bind.annotation.XmlRootElement;
26  import javax.xml.bind.annotation.XmlType;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.kuali.rice.core.api.CoreConstants;
30  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
31  import org.kuali.rice.core.api.mo.ModelBuilder;
32  import org.w3c.dom.Element;
33  
34  /**
35   * An immutable representation of a {@link NamespaceContract}.
36   *
37   * <p>To construct an instance of a Namespace, use the {@link Namespace.Builder} class.
38   *
39   * @see NamespaceContract
40   */
41  @XmlRootElement(name = Namespace.Constants.ROOT_ELEMENT_NAME)
42  @XmlAccessorType(XmlAccessType.NONE)
43  @XmlType(name = Namespace.Constants.TYPE_NAME, propOrder = {
44          Namespace.Elements.CODE,
45          Namespace.Elements.APPLICATION_ID,
46          Namespace.Elements.NAME,
47          Namespace.Elements.ACTIVE,
48          CoreConstants.CommonElements.VERSION_NUMBER,
49          CoreConstants.CommonElements.OBJECT_ID,
50          CoreConstants.CommonElements.FUTURE_ELEMENTS
51  })
52  public final class Namespace extends AbstractDataTransferObject implements NamespaceContract {
53  
54      private static final long serialVersionUID = -5206398776503106883L;
55  
56      @XmlElement(name = Elements.CODE, required = true)
57      private final String code;
58  
59      @XmlElement(name = Elements.APPLICATION_ID, required = false)
60      private final String applicationId;
61  
62      @XmlElement(name = Elements.NAME, required = false)
63      private final String name;
64  
65      @XmlElement(name = Elements.ACTIVE, required = true)
66      private final boolean active;
67  
68      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
69      private final Long versionNumber;
70  
71      @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
72      private final String objectId;
73  
74      @SuppressWarnings("unused") @XmlAnyElement
75      private final Collection<Element> _futureElements = null;
76  
77      /**
78       * This constructor should never be called.  It is only present for use during JAXB unmarshalling.
79       */
80      private Namespace() {
81          this.code = null;
82          this.applicationId = null;
83          this.name = null;
84          this.active = true;
85          this.versionNumber = null;
86          this.objectId = null;
87      }
88  
89      /**
90       * Constructs a Namespace from the given builder.  This constructor is private and should only
91       * ever be invoked from the builder.
92       *
93       * @param builder the Builder from which to construct the namespace
94       */
95      private Namespace(Builder builder) {
96          code = builder.getCode();
97          applicationId = builder.getApplicationId();
98          name = builder.getName();
99          active = builder.isActive();
100         versionNumber = builder.getVersionNumber();
101         objectId = builder.getObjectId();
102     }
103 
104     @Override
105     public String getCode() {
106         return code;
107     }
108 
109     @Override
110     public String getApplicationId() {
111         return applicationId;
112     }
113 
114     @Override
115     public String getName() {
116         return name;
117     }
118 
119     @Override
120     public boolean isActive() {
121         return active;
122     }
123 
124     @Override
125     public Long getVersionNumber() {
126         return versionNumber;
127     }
128 
129     @Override
130     public String getObjectId() {
131         return objectId;
132     }
133 
134     /**
135      * This builder is used to construct instances of Namespace.  It enforces the constraints of the {@link
136      * NamespaceContract}.
137      */
138     public static final class Builder implements NamespaceContract, ModelBuilder, Serializable {
139 
140         private static final long serialVersionUID = -70194982373806749L;
141 
142         private String code;
143         private String applicationId;
144         private String name;
145         private boolean active;
146         private Long versionNumber;
147         private String objectId;
148 
149         /**
150          * Constructs a Namespace Builder with the given namespace code and application id.  Defaults the active
151          * indicator to true.
152          *
153          * @param code the namespace code to use when constructing this builder
154          * @throws IllegalArgumentException if the code or applicationId are null or blank
155          */
156         private Builder(String code) {
157             setCode(code);
158             setActive(true);
159         }
160 
161         /**
162          * Creates a builder from the given namespace code and application id.
163          *
164          * @param code the namespace code to use when constructing this builder
165          * @return an instance of the builder with the given data already populated
166          * @throws IllegalArgumentException if the code or applicationId are null or blank
167          */
168         public static Builder create(String code) {
169             return new Builder(code);
170         }
171 
172         /**
173          * Creates a builder from the given namespace code and application id.
174          *
175          * @param code the namespace code to use when constructing this builder
176          * @param applicationId the application id to use when constructing this builder
177          * @return an instance of the builder with the given data already populated
178          * @throws IllegalArgumentException if the code or applicationId are null or blank
179          */
180         public static Builder create(String code, String applicationId) {
181             Builder builder = new Builder(code);
182             builder.setApplicationId(applicationId);
183             return builder;
184         }
185 
186         /**
187          * Creates a builder by populating it with data from the given {@link NamespaceContract}.
188          *
189          * @param contract the contract from which to populate this builder
190          * @return an instance of the builder populated with data from the contract
191          */
192         public static Builder create(NamespaceContract contract) {
193             if (contract == null) {
194                 throw new IllegalArgumentException("contract was null");
195             }
196             Builder builder = new Builder(contract.getCode());
197             builder.setApplicationId(contract.getApplicationId());
198             builder.setName(contract.getName());
199             builder.setActive(contract.isActive());
200             builder.setVersionNumber(contract.getVersionNumber());
201             builder.setObjectId(contract.getObjectId());
202             return builder;
203         }
204 
205         /**
206          * Sets the value of the code on this builder to the given value.
207          *
208          * @param code the code value to set, must not be null or blank
209          * @throws IllegalArgumentException if the code is null or blank
210          */
211         public void setCode(String code) {
212             if (StringUtils.isBlank(code)) {
213                 throw new IllegalArgumentException("code is blank");
214             }
215             this.code = code;
216         }
217 
218         /**
219          * Sets the value of the applicationId on this builder to the given value.
220          *
221          * @param applicationId the application id value to set, must not be null or blank
222          * @throws IllegalArgumentException if the application id is null or blank
223          */
224         public void setApplicationId(String applicationId) {
225             this.applicationId = applicationId;
226         }
227 
228         public void setVersionNumber(Long versionNumber) {
229             this.versionNumber = versionNumber;
230         }
231 
232         public void setObjectId(String objectId) {
233             this.objectId = objectId;
234         }
235 
236         @Override
237         public String getName() {
238             return name;
239         }
240 
241         public void setName(String name) {
242             this.name = name;
243         }
244 
245         @Override
246         public boolean isActive() {
247             return active;
248         }
249 
250         public void setActive(boolean active) {
251             this.active = active;
252         }
253 
254         @Override
255         public String getCode() {
256             return code;
257         }
258 
259         @Override
260         public String getApplicationId() {
261             return applicationId;
262         }
263 
264         @Override
265         public Long getVersionNumber() {
266             return versionNumber;
267         }
268 
269         @Override
270         public String getObjectId() {
271             return objectId;
272         }
273 
274         /**
275          * Builds an instance of a Namespace based on the current state of the builder.
276          *
277          * @return the fully-constructed Namespace
278          */
279         @Override
280         public Namespace build() {
281             return new Namespace(this);
282         }
283 
284     }
285 
286     /**
287      * Defines some internal constants used on this class.
288      */
289     static class Constants {
290         final static String ROOT_ELEMENT_NAME = "namespace";
291         final static String TYPE_NAME = "NamespaceType";
292     }
293 
294     /**
295      * A private class which exposes constants which define the XML element names to use
296      * when this object is marshalled to XML.
297      */
298     static class Elements {
299         final static String CODE = "code";
300         final static String APPLICATION_ID = "applicationId";
301         final static String NAME = "name";
302         final static String ACTIVE = "active";
303     }
304 
305     public static class Cache {
306         public static final String NAME =
307                 CoreConstants.Namespaces.CORE_NAMESPACE_2_0 + "/" + Namespace.Constants.TYPE_NAME;
308     }
309 
310 }