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