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.campus;
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     * An immutable representation of a {@link CampusContract}.
035     *
036     * <p>To construct an instance of a Campus, use the {@link Campus.Builder} class.
037     *
038     * @see CampusContract
039     * @author Kuali Rice Team (rice.collab@kuali.org)
040     */
041    @XmlRootElement(name = Campus.Constants.ROOT_ELEMENT_NAME)
042    @XmlAccessorType(XmlAccessType.NONE)
043    @XmlType(name = Campus.Constants.TYPE_NAME, propOrder = {
044            Campus.Elements.CODE,
045            Campus.Elements.NAME,
046            Campus.Elements.SHORT_NAME,
047            Campus.Elements.CAMPUS_TYPE,
048            Campus.Elements.ACTIVE,
049            CoreConstants.CommonElements.VERSION_NUMBER,
050            CoreConstants.CommonElements.OBJECT_ID,
051            CoreConstants.CommonElements.FUTURE_ELEMENTS
052    })
053    public final class Campus extends AbstractDataTransferObject implements CampusContract {
054            private static final long serialVersionUID = 2288194493838509380L;
055    
056            @XmlElement(name = Elements.CODE, required=true)
057            private final String code;
058    
059            @XmlElement(name = Elements.NAME, required=false)
060            private final String name;
061    
062            @XmlElement(name = Elements.SHORT_NAME, required=false)
063            private final String shortName;
064    
065            @XmlElement(name = Elements.CAMPUS_TYPE, required=false)
066            private final CampusType campusType;
067    
068            @XmlElement(name = Elements.ACTIVE, required=false)
069            private final boolean active;
070    
071        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
072        private final Long versionNumber;
073        
074            @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
075            private final String objectId;
076            
077            @SuppressWarnings("unused")
078        @XmlAnyElement
079        private final Collection<Element> _futureElements = null;
080            
081            
082             /** 
083         * This constructor should never be called.  It is only present for use during JAXB unmarshalling. 
084         */
085        @SuppressWarnings("unused")
086        private Campus() {
087            this.code = null;
088            this.name = null;
089            this.shortName = null;
090            this.campusType = null;
091            this.active = false;
092            this.versionNumber = null;
093            this.objectId = null;
094        }
095        
096        /**
097             * Constructs a Campus from the given builder.  This constructor is private and should only
098             * ever be invoked from the builder.
099             * 
100             * @param builder the Builder from which to construct the campus
101             */
102        private Campus(Builder builder) {
103            this.code = builder.getCode();
104            this.name = builder.getName();
105            this.shortName = builder.getShortName();
106            if (builder.campusType != null) {
107                    this.campusType = builder.getCampusType().build();
108            } else {
109                this.campusType = null;
110            }
111            this.active = builder.isActive();
112            this.versionNumber = builder.getVersionNumber();
113            this.objectId = builder.getObjectId();
114        }
115    
116            /** {@inheritDoc} */
117            @Override
118            public String getCode() {
119                    return this.code;
120            }
121    
122            /** {@inheritDoc} */
123            @Override
124            public String getName() {
125                    return this.name;
126            }
127    
128            /** {@inheritDoc} */
129            @Override
130            public String getShortName() {
131                    return this.shortName;
132            }
133    
134            /** {@inheritDoc} */
135            @Override
136            public CampusType getCampusType() {
137                    return this.campusType;
138            }
139    
140            /** {@inheritDoc} */
141            @Override
142            public boolean isActive() {
143                    return this.active;
144            }
145    
146        /** {@inheritDoc} */
147        @Override
148        public Long getVersionNumber() {
149            return versionNumber;
150        }
151            
152            /** {@inheritDoc} */
153            @Override
154            public String getObjectId() {
155                    return objectId;
156            }
157    
158            /**
159         * This builder is used to construct instances of Campus.  It enforces the constraints of the {@link CampusContract}.
160         */
161        public static class Builder implements CampusContract, ModelBuilder, Serializable {
162                    private static final long serialVersionUID = -3130728718673871762L;
163                    private String code;
164            private String name;
165            private String shortName;
166            private CampusType.Builder campusType;
167            private boolean active;
168            private Long versionNumber;
169            private String objectId;
170    
171                    /**
172                     * Private constructor for creating a builder with all of it's required attributes.
173                     */
174            private Builder(String code) {
175                setCode(code);
176                            setActive(true);
177            }
178    
179            /**
180             * Creates a builder from the given campus code.
181             * 
182             * @param code the campus code
183             * @return an instance of the builder with the code already populated
184             * @throws IllegalArgumentException if the code is null or blank
185             */
186            public static Builder create(String code) {
187                return new Builder(code);
188            }
189    
190            /**
191             * Creates a builder by populating it with data from the given {@link CampusContract}.
192             * 
193             * @param contract the contract from which to populate this builder
194             * @return an instance of the builder populated with data from the contract
195             */
196            public static Builder create(CampusContract contract) {
197                    if (contract == null) {
198                    throw new IllegalArgumentException("contract is null");
199                }
200                Builder builder =  new Builder(contract.getCode());
201                builder.setName(contract.getName());
202                builder.setShortName(contract.getShortName());
203                if (contract.getCampusType() != null) {
204                    builder.setCampusType(CampusType.Builder.create(contract.getCampusType()));
205                }
206                builder.setActive(contract.isActive());
207                builder.setVersionNumber(contract.getVersionNumber());
208                builder.setObjectId(contract.getObjectId());
209                return builder;
210            }
211    
212                    /**
213                     * Sets the value of the code on this builder to the given value.
214                     * 
215                     * @param code the code value to set, must not be null or blank
216                     * @throws IllegalArgumentException if the code is null or blank
217                     */
218            public void setCode(String code) {
219                if (StringUtils.isBlank(code)) {
220                    throw new IllegalArgumentException("code is blank");
221                }
222                this.code = code;
223            }
224    
225                    public void setName(String name) {
226                            this.name = name;
227                    }
228                    
229                    public void setShortName(String shortName) {
230                            this.shortName = shortName;
231                    }
232                    
233                    public void setCampusType(CampusType.Builder campusType) {
234                            this.campusType = campusType;
235                    }
236    
237                    public void setActive(boolean active) {
238                            this.active = active;
239                    }
240    
241            public void setVersionNumber(Long versionNumber){
242                this.versionNumber = versionNumber;
243            }
244            
245            public void setObjectId(String objectId) {
246                    this.objectId = objectId;
247            }
248    
249                    @Override
250                    public String getCode() {
251                            return code;
252                    }
253    
254                    @Override
255                    public String getName() {
256                            return name;
257                    }
258                    
259                    @Override
260                    public String getShortName() {
261                            return shortName;
262                    }
263                    
264                    @Override 
265                    public CampusType.Builder getCampusType() {
266                            return campusType;
267                    }
268                    
269                    @Override
270                    public boolean isActive() {
271                            return active;
272                    }
273    
274            @Override
275            public Long getVersionNumber() {
276                return versionNumber;
277            }
278    
279            @Override
280            public String getObjectId() {
281                    return objectId;
282            }
283    
284                    /**
285                     * Builds an instance of a Campus based on the current state of the builder.
286                     * 
287                     * @return the fully-constructed Campus
288                     */
289            @Override
290            public Campus build() {
291                return new Campus(this);
292            }
293                    
294        }
295    
296            /**
297             * Defines some internal constants used on this class.
298             */
299            static class Constants {
300                    final static String ROOT_ELEMENT_NAME = "campus";
301                    final static String TYPE_NAME = "CampusType";
302            }
303            
304            /**
305         * A private class which exposes constants which define the XML element names to use
306         * when this object is marshalled to XML.
307         */
308        static class Elements {
309            final static String CODE = "code";
310            final static String NAME = "name";
311            final static String SHORT_NAME = "shortName";
312            final static String CAMPUS_TYPE = "campusType";
313            final static String ACTIVE = "active";
314        }
315    
316        public static class Cache {
317            public static final String NAME = LocationConstants.Namespaces.LOCATION_NAMESPACE_2_0 + "/" + Campus.Constants.TYPE_NAME;
318        }
319    }