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.core.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=true)
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=false)
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")
75      @XmlAnyElement
76      private final Collection<Element> _futureElements = null;
77  
78      /** 
79       * This constructor should never be called.  It is only present for use during JAXB unmarshalling. 
80       */
81      private Namespace() {
82      	this.code = null;
83      	this.applicationId = null;
84      	this.name = null;
85      	this.active = true;
86          this.versionNumber = null;
87          this.objectId = null;
88      }
89  
90  	/**
91  	* Constructs a Namespace from the given builder.  This constructor is private and should only
92  	* ever be invoked from the builder.
93  	*
94  	* @param builder the Builder from which to construct the namespace
95  	*/
96      private Namespace(Builder builder) {
97          code = builder.getCode();
98          applicationId = builder.getApplicationId();
99          name = builder.getName();
100         active = builder.isActive();
101         versionNumber = builder.getVersionNumber();
102         objectId = builder.getObjectId();
103     }
104 
105     @Override
106     public String getCode() {
107 		return code;
108 	}
109 
110     @Override
111 	public String getApplicationId() {
112 		return applicationId;
113 	}
114 
115     @Override
116 	public String getName() {
117 		return name;
118 	}
119 
120     @Override
121 	public boolean isActive() {
122 		return active;
123 	}
124 
125     @Override
126     public Long getVersionNumber() {
127         return versionNumber;
128     }
129     
130     @Override
131 	public String getObjectId() {
132 		return objectId;
133 	}
134 
135 	/**
136      * This builder is used to construct instances of Namespace.  It enforces the constraints of the {@link 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 indicator to true.
151 		 *
152 		 * @param code the namespace code to use when constructing this builder
153 		 * @param applicationId the application id to use when constructing this builder
154 		 * @throws IllegalArgumentException if the code or applicationId are null or blank
155 		 */
156         private Builder(String code, String applicationId) {
157             setCode(code);
158             setApplicationId(applicationId);
159 			setActive(true);
160         }
161 
162         /**
163          * Creates a builder from the given namespace code and application id.
164          * 
165 		 * @param code the namespace code to use when constructing this builder
166 		 * @param applicationId the application id to use when constructing this builder
167 		 * @return an instance of the builder with the given data already populated
168 		 * @throws IllegalArgumentException if the code or applicationId are null or blank
169          */
170         public static Builder create(String code, String applicationId) {
171             return new Builder(code, applicationId);
172         }
173 
174 		/**
175 		 * Creates a builder by populating it with data from the given {@link NamespaceContract}.
176          * 
177          * @param contract the contract from which to populate this builder
178          * @return an instance of the builder populated with data from the contract
179          */
180         public static Builder create(NamespaceContract contract) {
181             Builder builder  = new Builder(contract.getCode(), contract.getApplicationId());
182             builder.setName(contract.getName());
183             builder.setActive(contract.isActive());
184             builder.setVersionNumber(contract.getVersionNumber());
185             builder.setObjectId(contract.getObjectId());
186             return builder;
187         }
188 
189 		/**
190 		 * Sets the value of the code on this builder to the given value.
191 		 *
192 		 * @param code the code value to set, must not be null or blank
193 		 * @throws IllegalArgumentException if the code is null or blank
194 		 */
195         public void setCode(String code) {
196             if (StringUtils.isBlank(code)) {
197                 throw new IllegalArgumentException("code is blank");
198             }
199             this.code = code;
200         }
201 
202 		/**
203 		 * Sets the value of the applicationId on this builder to the given value.
204 		 *
205 		 * @param applicationId the application id value to set, must not be null or blank
206 		 * @throws IllegalArgumentException if the application id is null or blank
207 		 */
208         public void setApplicationId(String applicationId) {
209             if (StringUtils.isBlank(applicationId)) {
210                 throw new IllegalArgumentException("applicationId is blank");
211             }
212             this.applicationId = applicationId;
213         }
214         
215 		public void setVersionNumber(Long versionNumber) {
216 			this.versionNumber = versionNumber;
217 		}
218 		
219 		public void setObjectId(String objectId) {
220         	this.objectId = objectId;
221         }
222 
223         @Override
224 		public String getName() {
225 			return name;
226 		}
227 
228 		public void setName(String name) {
229 			this.name = name;
230 		}
231 
232         @Override
233 		public boolean isActive() {
234 			return active;
235 		}
236 
237 		public void setActive(boolean active) {
238 			this.active = active;
239 		}
240 
241         @Override
242 		public String getCode() {
243 			return code;
244 		}
245 
246         @Override
247 		public String getApplicationId() {
248 			return applicationId;
249 		}
250 
251         @Override
252 		public Long getVersionNumber() {
253 			return versionNumber;
254 		}
255 		
256         @Override
257         public String getObjectId() {
258         	return objectId;
259         }
260 
261 		/**
262 		 * Builds an instance of a Namespace based on the current state of the builder.
263 		 *
264 		 * @return the fully-constructed Namespace
265 		 */
266         @Override
267         public Namespace build() {
268             return new Namespace(this);
269         }
270 
271     }
272 	
273 	/**
274 	 * Defines some internal constants used on this class.
275 	 */
276 	static class Constants {
277 		final static String ROOT_ELEMENT_NAME = "namespace";
278 		final static String TYPE_NAME = "NamespaceType";
279 	}
280    
281    /**
282 	* A private class which exposes constants which define the XML element names to use
283 	* when this object is marshalled to XML.
284 	*/
285    static class Elements {
286 	   final static String CODE = "code";
287 	   final static String APPLICATION_ID = "applicationId";
288 	   final static String NAME = "name";
289 	   final static String ACTIVE = "active";
290    }
291 	
292 }