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