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