001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.location.api.country;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.CoreConstants;
020import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021import org.kuali.rice.core.api.mo.ModelBuilder;
022import org.kuali.rice.location.api.LocationConstants;
023import org.w3c.dom.Element;
024
025import javax.xml.bind.annotation.XmlAccessType;
026import javax.xml.bind.annotation.XmlAccessorType;
027import javax.xml.bind.annotation.XmlAnyElement;
028import javax.xml.bind.annotation.XmlElement;
029import javax.xml.bind.annotation.XmlRootElement;
030import javax.xml.bind.annotation.XmlType;
031import java.io.Serializable;
032import java.util.Collection;
033
034/**
035 * POJO implementation of CountryContract that is immutable. Instances of Country can be (un)marshalled to and from XML.
036 *
037 * @see CountryContract
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 */
040@XmlRootElement(name = Country.Constants.ROOT_ELEMENT_NAME)
041@XmlAccessorType(XmlAccessType.NONE)
042@XmlType(name = Country.Constants.TYPE_NAME, propOrder = {
043        Country.Elements.CODE,
044        Country.Elements.ALTERNATE_CODE,
045        Country.Elements.NAME,
046        Country.Elements.RESTRICTED,
047        Country.Elements.ACTIVE,
048        CoreConstants.CommonElements.VERSION_NUMBER,
049        CoreConstants.CommonElements.FUTURE_ELEMENTS
050})
051public final class Country extends AbstractDataTransferObject implements CountryContract {
052    private static final long serialVersionUID = -8975392777320033940L;
053
054    @XmlElement(name = Elements.CODE, required = true)
055    private final String code;
056
057    @XmlElement(name = Elements.ALTERNATE_CODE, required = false)
058    private final String alternateCode;
059
060    @XmlElement(name = Elements.NAME, required = false)
061    private final String name;
062
063    @XmlElement(name = Elements.RESTRICTED, required = true)
064    private final boolean restricted;
065
066    @XmlElement(name = Elements.ACTIVE, required = true)
067    private final boolean active;
068
069    @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
070    private final Long versionNumber;
071
072    @SuppressWarnings("unused")
073    @XmlAnyElement
074    private final Collection<Element> _futureElements = null;
075
076    /**
077     * This constructor should never be called except during JAXB unmarshalling.
078     */
079    @SuppressWarnings("unused")
080    private Country() {
081        this.code = null;
082        this.alternateCode = null;
083        this.name = null;
084        this.restricted = false;
085        this.active = false;
086        this.versionNumber = null;
087    }
088
089    private Country(Builder builder) {
090        this.code = builder.getCode();
091        this.alternateCode = builder.getAlternateCode();
092        this.name = builder.getName();
093        this.restricted = builder.isRestricted();
094        this.active = builder.isActive();
095        this.versionNumber = builder.getVersionNumber();
096    }
097
098    /** {@inheritDoc} */
099    @Override
100    public String getCode() {
101        return this.code;
102    }
103
104    /** {@inheritDoc} */
105    @Override
106    public String getAlternateCode() {
107        return this.alternateCode;
108    }
109
110    /** {@inheritDoc} */
111    @Override
112    public String getName() {
113        return this.name;
114    }
115
116    /** {@inheritDoc} */
117    @Override
118    public boolean isActive() {
119        return this.active;
120    }
121
122    /** {@inheritDoc} */
123    @Override
124    public boolean isRestricted() {
125        return this.restricted;
126    }
127
128    /** {@inheritDoc} */
129    @Override
130    public Long getVersionNumber() {
131        return this.versionNumber;
132    }
133
134    /**
135     * Builder for immutable Country objects.
136     */
137    public static class Builder implements CountryContract, ModelBuilder, Serializable {
138        private static final long serialVersionUID = -4786917485397379322L;
139
140        private String code;
141        private String alternateCode;
142        private String name;
143        private boolean restricted;
144        private boolean active;
145        private Long versionNumber;
146
147        private Builder(String code, String alternateCode, String name,
148                        boolean restricted, boolean active) {
149            this.setCode(code);
150            this.setAlternateCode(alternateCode);
151            this.setName(name);
152            this.setRestricted(restricted);
153            this.setActive(active);
154        }
155
156        public static Builder create(String code, String name) {
157            return new Builder(code, null, name, false, true);
158        }
159
160        public static Builder create(String code, String alternatePostalCode, String name,
161                                     boolean restricted, boolean active) {
162            return new Builder(code, alternatePostalCode, name, restricted, active);
163        }
164
165        public static Builder create(CountryContract cc) {
166            Builder builder = new Builder(cc.getCode(), cc.getAlternateCode(),
167                    cc.getName(), cc.isRestricted(), cc.isActive());
168            builder.setVersionNumber(cc.getVersionNumber());
169            return builder;
170        }
171
172        @Override
173        public Country build() {
174            return new Country(this);
175        }
176
177        /**
178         * Sets code property.
179         *
180         * @param code required to be not null and not empty.
181         */
182        public void setCode(String code) {
183            if (StringUtils.isBlank(code)) {
184                throw new IllegalArgumentException("code cannot be blank or null");
185            }
186            this.code = code;
187        }
188
189        @Override
190        public String getCode() {
191            return this.code;
192        }
193
194        /**
195         * Sets the optional alternatePostalCode property
196         *
197         * @param alternatePostalCode
198         */
199        public void setAlternateCode(String alternatePostalCode) {
200            this.alternateCode = alternatePostalCode;
201        }
202
203        @Override
204        public String getAlternateCode() {
205            return this.alternateCode;
206        }
207
208        /**
209         * Sets the optional name property.
210         *
211         * @param name
212         */
213        public void setName(String name) {
214            this.name = name;
215        }
216
217        @Override
218        public String getName() {
219            return this.name;
220        }
221
222        /**
223         * Sets the active property.
224         *
225         * @param active
226         */
227        public void setActive(boolean active) {
228            this.active = active;
229        }
230
231        @Override
232        public boolean isActive() {
233            return this.active;
234        }
235
236        /**
237         * Sets the versionNumber property.
238         *
239         * @param versionNumber
240         */
241        public void setVersionNumber(Long versionNumber) {
242            this.versionNumber = versionNumber;
243        }
244
245        @Override
246        public Long getVersionNumber() {
247            return this.versionNumber;
248        }
249
250        /**
251         * Sets the restrictedProperty
252         *
253         * @param restricted
254         */
255        public void setRestricted(boolean restricted) {
256            this.restricted = restricted;
257        }
258
259        @Override
260        public boolean isRestricted() {
261            return this.restricted;
262        }
263    }
264
265    /**
266     * A private class which exposes constants which define the XML element names to use
267     * when this object is marshalled to XML.
268     */
269    static class Elements {
270        final static String CODE = "code";
271        final static String ALTERNATE_CODE = "alternateCode";
272        final static String NAME = "name";
273        final static String RESTRICTED = "restricted";
274        final static String ACTIVE = "active";
275    }
276
277    /**
278     * Defines some internal constants used on this class.
279     */
280    static class Constants {
281        final static String ROOT_ELEMENT_NAME = "country";
282        final static String TYPE_NAME = "CountryType";
283    }
284
285    public static class Cache {
286        public static final String NAME = LocationConstants.Namespaces.LOCATION_NAMESPACE_2_0 + "/" + Country.Constants.TYPE_NAME;
287    }
288}