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