001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.location.api.county;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.CoreConstants;
020    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021    import org.kuali.rice.core.api.mo.ModelBuilder;
022    import org.kuali.rice.location.api.LocationConstants;
023    import org.w3c.dom.Element;
024    
025    import javax.xml.bind.annotation.XmlAccessType;
026    import javax.xml.bind.annotation.XmlAccessorType;
027    import javax.xml.bind.annotation.XmlAnyElement;
028    import javax.xml.bind.annotation.XmlElement;
029    import javax.xml.bind.annotation.XmlRootElement;
030    import javax.xml.bind.annotation.XmlType;
031    import java.io.Serializable;
032    import java.util.Collection;
033    
034    /**
035     * An immutable representation of a {@link CountyContract}.
036     *
037     * <p>To construct an instance of a County, use the {@link County.Builder} class.
038     *
039     * @see CountyContract
040     * @author Kuali Rice Team (rice.collab@kuali.org)
041     */
042    @XmlRootElement(name = County.Constants.ROOT_ELEMENT_NAME)
043    @XmlAccessorType(XmlAccessType.NONE)
044    @XmlType(name = County.Constants.TYPE_NAME, propOrder = {
045            County.Elements.CODE,
046            County.Elements.NAME,
047            County.Elements.COUNTRY_CODE,
048            County.Elements.STATE_CODE,
049            County.Elements.ACTIVE,
050            CoreConstants.CommonElements.VERSION_NUMBER,
051            CoreConstants.CommonElements.FUTURE_ELEMENTS
052    })
053    public final class County extends AbstractDataTransferObject implements CountyContract {
054    
055        private static final long serialVersionUID = 6097498602725305353L;
056    
057        @XmlElement(name = Elements.CODE, required = true)
058        private final String code;
059    
060        @XmlElement(name = Elements.NAME, required = true)
061        private final String name;
062    
063        @XmlElement(name = Elements.COUNTRY_CODE, required = true)
064        private final String countryCode;
065    
066        @XmlElement(name = Elements.STATE_CODE, required = true)
067        private final String stateCode;
068    
069        @XmlElement(name = Elements.ACTIVE, required = true)
070        private final boolean active;
071    
072        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
073        private final Long versionNumber;
074    
075        @SuppressWarnings("unused")
076        @XmlAnyElement
077        private final Collection<Element> _futureElements = null;
078    
079        /**
080         * This constructor should never be called except during JAXB unmarshalling.
081         */
082        @SuppressWarnings("unused")
083        private County() {
084            this.code = null;
085            this.name = null;
086            this.countryCode = null;
087            this.stateCode = null;
088            this.active = false;
089            this.versionNumber = null;
090        }
091    
092        private County(Builder builder) {
093            code = builder.getCode();
094            name = builder.getName();
095            countryCode = builder.getCountryCode();
096            stateCode = builder.getStateCode();
097            active = builder.isActive();
098            versionNumber = builder.getVersionNumber();
099        }
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    }