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.county;
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  /**
35   * An immutable representation of a {@link CountyContract}.
36   *
37   * <p>To construct an instance of a County, use the {@link County.Builder} class.
38   *
39   * @see CountyContract
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  @XmlRootElement(name = County.Constants.ROOT_ELEMENT_NAME)
43  @XmlAccessorType(XmlAccessType.NONE)
44  @XmlType(name = County.Constants.TYPE_NAME, propOrder = {
45          County.Elements.CODE,
46          County.Elements.NAME,
47          County.Elements.COUNTRY_CODE,
48          County.Elements.STATE_CODE,
49          County.Elements.ACTIVE,
50          CoreConstants.CommonElements.VERSION_NUMBER,
51          CoreConstants.CommonElements.FUTURE_ELEMENTS
52  })
53  public final class County extends AbstractDataTransferObject implements CountyContract {
54  
55      private static final long serialVersionUID = 6097498602725305353L;
56  
57      @XmlElement(name = Elements.CODE, required = true)
58      private final String code;
59  
60      @XmlElement(name = Elements.NAME, required = true)
61      private final String name;
62  
63      @XmlElement(name = Elements.COUNTRY_CODE, required = true)
64      private final String countryCode;
65  
66      @XmlElement(name = Elements.STATE_CODE, required = true)
67      private final String stateCode;
68  
69      @XmlElement(name = Elements.ACTIVE, required = true)
70      private final boolean active;
71  
72      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
73      private final Long versionNumber;
74  
75      @SuppressWarnings("unused")
76      @XmlAnyElement
77      private final Collection<Element> _futureElements = null;
78  
79      /**
80       * This constructor should never be called except during JAXB unmarshalling.
81       */
82      @SuppressWarnings("unused")
83      private County() {
84          this.code = null;
85          this.name = null;
86          this.countryCode = null;
87          this.stateCode = null;
88          this.active = false;
89          this.versionNumber = null;
90      }
91  
92      private County(Builder builder) {
93          code = builder.getCode();
94          name = builder.getName();
95          countryCode = builder.getCountryCode();
96          stateCode = builder.getStateCode();
97          active = builder.isActive();
98          versionNumber = builder.getVersionNumber();
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public String getCode() {
104         return code;
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public String getName() {
110         return name;
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public String getCountryCode() {
116         return countryCode;
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public String getStateCode() {
122         return stateCode;
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public boolean isActive() {
128         return active;
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     public Long getVersionNumber() {
134         return versionNumber;
135     }
136 
137     /**
138      * This builder constructs an County enforcing the constraints of the {@link CountyContract}.
139      */
140     public static class Builder implements CountyContract, ModelBuilder, Serializable {
141 
142         private static final long serialVersionUID = 7077484401017765844L;
143 
144         private String code;
145         private String name;
146         private String countryCode;
147         private String stateCode;
148         private boolean active;
149         private Long versionNumber;
150 
151         private Builder(String code, String name, String countryCode, String stateCode) {
152             setCode(code);
153             setName(name);
154             setCountryCode(countryCode);
155             setStateCode(stateCode);
156             setVersionNumber(versionNumber);
157         }
158 
159         /**
160          * creates a County Builder with the required fields.
161          */
162         public static Builder create(String code, String name, String countryCode, String stateCode) {
163             final Builder builder = new Builder(code, name, countryCode, stateCode);
164             builder.setActive(true);
165             return builder;
166         }
167 
168         /**
169          * creates a County Builder from an existing {@link CountyContract}.
170          */
171         public static Builder create(CountyContract contract) {
172             final Builder builder = new Builder(contract.getCode(), contract.getName(), contract.getCountryCode(), contract.getStateCode());
173             builder.setActive(contract.isActive());
174             builder.setVersionNumber(contract.getVersionNumber());
175             return builder;
176         }
177 
178         @Override
179         public String getCode() {
180             return code;
181         }
182 
183         /**
184          * Sets the code to be used for the County created from this Builder.
185          * @param code String code for a County
186          * @throws IllegalArgumentException if the passed in code is null or a blank String.
187          */
188         public void setCode(String code) {
189             if (StringUtils.isBlank(code)) {
190                 throw new IllegalArgumentException("code is blank");
191             }
192 
193             this.code = code;
194         }
195 
196         @Override
197         public String getName() {
198             return name;
199         }
200 
201         /**
202          * Sets the full name of the County created from this Builder.
203          * @param name String representing the full name for the County
204          * @throws IllegalArgumentException if the passed in name is null or a blank String.
205          */
206         public void setName(String name) {
207             if (StringUtils.isBlank(name)) {
208                 throw new IllegalArgumentException("name is blank");
209             }
210 
211             this.name = name;
212         }
213 
214         @Override
215         public String getCountryCode() {
216             return countryCode;
217         }
218 
219         /**
220          * Sets the Country code to be associated with the County created from this Builder.
221          * @param countryCode String representing the Country Code
222          * @throws IllegalArgumentException if the passed in countryCode is null or a blank String.
223          * @see org.kuali.rice.location.api.country.CountryContract
224          */
225         public void setCountryCode(String countryCode) {
226             if (StringUtils.isBlank(countryCode)) {
227                 throw new IllegalArgumentException("countryCode is blank");
228             }
229 
230             this.countryCode = countryCode;
231         }
232 
233         @Override
234         public String getStateCode() {
235             return stateCode;
236         }
237 
238         /**
239          * Sets the State code to be associated with the County created from this Builder.
240          * @param stateCode String representing the State code
241          * @throws  IllegalArgumentException if the passed in statecode is null or a blank String.
242          * @see org.kuali.rice.location.api.state.StateContract
243          */
244         public void setStateCode(String stateCode) {
245             if (StringUtils.isBlank(stateCode)) {
246                 throw new IllegalArgumentException("stateCode is blank");
247             }
248 
249             this.stateCode = stateCode;
250         }
251 
252         @Override
253         public boolean isActive() {
254             return active;
255         }
256 
257         /**
258          * Sets the active flag for the County created from this Builder.
259          * @param active
260          */
261         public void setActive(boolean active) {
262             this.active = active;
263         }
264 
265         @Override
266         public Long getVersionNumber() {
267             return versionNumber;
268         }
269 
270         public void setVersionNumber(Long versionNumber) {
271             this.versionNumber = versionNumber;
272         }
273 
274         @Override
275         public County build() {
276             return new County(this);
277         }
278     }
279 
280     /**
281      * Defines some internal constants used on this class.
282      */
283     static class Constants {
284         final static String ROOT_ELEMENT_NAME = "county";
285         final static String TYPE_NAME = "CountyType";
286     }
287 
288     /**
289      * A private class which exposes constants which define the XML element names to use
290      * when this object is marshalled to XML.
291      */
292     static class Elements {
293         final static String CODE = "code";
294         final static String NAME = "name";
295         final static String COUNTRY_CODE = "countryCode";
296         final static String STATE_CODE = "stateCode";
297         final static String ACTIVE = "active";
298     }
299 
300     public static class Cache {
301         public static final String NAME = LocationConstants.Namespaces.LOCATION_NAMESPACE_2_0 + "/" + County.Constants.TYPE_NAME;
302     }
303 }