View Javadoc

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