View Javadoc

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