001    /**
002     * Copyright 2005-2014 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.kim.api.identity;
017    
018    import java.io.Serializable;
019    import java.util.Collection;
020    import javax.xml.bind.annotation.XmlAccessType;
021    import javax.xml.bind.annotation.XmlAccessorType;
022    import javax.xml.bind.annotation.XmlAnyElement;
023    import javax.xml.bind.annotation.XmlElement;
024    import javax.xml.bind.annotation.XmlRootElement;
025    import javax.xml.bind.annotation.XmlType;
026    
027    import org.apache.commons.lang.StringUtils;
028    import org.kuali.rice.core.api.CoreConstants;
029    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
030    import org.kuali.rice.core.api.mo.ModelBuilder;
031    import org.kuali.rice.kim.api.KimConstants;
032    import org.w3c.dom.Element;
033    
034    @XmlRootElement(name = CodedAttribute.Constants.ROOT_ELEMENT_NAME)
035    @XmlAccessorType(XmlAccessType.NONE)
036    @XmlType(name = CodedAttribute.Constants.TYPE_NAME, propOrder = {
037        CodedAttribute.Elements.CODE,
038        CodedAttribute.Elements.NAME,
039        CodedAttribute.Elements.SORT_CODE,
040        CodedAttribute.Elements.ACTIVE,
041        CoreConstants.CommonElements.VERSION_NUMBER,
042        CoreConstants.CommonElements.OBJECT_ID,
043        CoreConstants.CommonElements.FUTURE_ELEMENTS
044    })
045    public final class CodedAttribute extends AbstractDataTransferObject
046        implements CodedAttributeContract
047    {
048        @XmlElement(name = Elements.CODE, required = true)
049        private final String code;
050        @XmlElement(name = Elements.NAME, required = false)
051        private final String name;
052        @XmlElement(name = Elements.SORT_CODE, required = false)
053        private final String sortCode;
054        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
055        private final Long versionNumber;
056        @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
057        private final String objectId;
058        @XmlElement(name = Elements.ACTIVE, required = false)
059        private final boolean active;
060        @SuppressWarnings("unused")
061        @XmlAnyElement
062        private final Collection<Element> _futureElements = null;
063    
064        /**
065         * Private constructor used only by JAXB.
066         * 
067         */
068        private CodedAttribute() {
069            this.name = null;
070            this.code = null;
071            this.sortCode = null;
072            this.versionNumber = null;
073            this.objectId = null;
074            this.active = false;
075        }
076    
077        private CodedAttribute(Builder builder) {
078            this.name = builder.getName();
079            this.code = builder.getCode();
080            this.sortCode = builder.getSortCode();
081            this.versionNumber = builder.getVersionNumber();
082            this.objectId = builder.getObjectId();
083            this.active = builder.isActive();
084        }
085    
086        @Override
087        public String getName() {
088            return this.name;
089        }
090    
091        @Override
092        public String getCode() {
093            return this.code;
094        }
095    
096        @Override
097        public String getSortCode() {
098            return this.sortCode;
099        }
100    
101        @Override
102        public Long getVersionNumber() {
103            return this.versionNumber;
104        }
105    
106        @Override
107        public String getObjectId() {
108            return this.objectId;
109        }
110    
111        @Override
112        public boolean isActive() {
113            return this.active;
114        }
115    
116    
117        /**
118         * A builder which can be used to construct {@link CodedAttribute} instances.  Enforces the constraints of the {@link CodedAttributeContract}.
119         * 
120         */
121        public final static class Builder
122            implements Serializable, ModelBuilder, CodedAttributeContract
123        {
124    
125            private String name;
126            private String code;
127            private String sortCode;
128            private Long versionNumber;
129            private String objectId;
130            private boolean active;
131    
132            private Builder(String code) {
133                setCode(code);
134            }
135    
136            public static Builder create(String code) {
137                return new Builder(code);
138            }
139    
140            public static Builder create(CodedAttributeContract contract) {
141                if (contract == null) {
142                    throw new IllegalArgumentException("contract was null");
143                }
144                // TODO if create() is modified to accept required parameters, this will need to be modified
145                Builder builder = create(contract.getCode());
146                builder.setName(contract.getName());
147                builder.setSortCode(contract.getSortCode());
148                builder.setVersionNumber(contract.getVersionNumber());
149                builder.setObjectId(contract.getObjectId());
150                builder.setActive(contract.isActive());
151                return builder;
152            }
153    
154            public CodedAttribute build() {
155                return new CodedAttribute(this);
156            }
157    
158            @Override
159            public String getName() {
160                return this.name;
161            }
162    
163            @Override
164            public String getCode() {
165                return this.code;
166            }
167    
168            @Override
169            public String getSortCode() {
170                return this.sortCode;
171            }
172    
173            @Override
174            public Long getVersionNumber() {
175                return this.versionNumber;
176            }
177    
178            @Override
179            public String getObjectId() {
180                return this.objectId;
181            }
182    
183            @Override
184            public boolean isActive() {
185                return this.active;
186            }
187    
188            public void setName(String name) {
189                this.name = name;
190            }
191    
192            public void setCode(String code) {
193                if (StringUtils.isWhitespace(code)) {
194                    throw new IllegalArgumentException("code is empty");
195                }
196                this.code = code;
197            }
198    
199            public void setSortCode(String sortCode) {
200                this.sortCode = sortCode;
201            }
202    
203            public void setVersionNumber(Long versionNumber) {
204                this.versionNumber = versionNumber;
205            }
206    
207            public void setObjectId(String objectId) {
208                this.objectId = objectId;
209            }
210    
211            public void setActive(boolean active) {
212                this.active = active;
213            }
214    
215        }
216    
217    
218        /**
219         * Defines some internal constants used on this class.
220         * 
221         */
222        static class Constants {
223    
224            final static String ROOT_ELEMENT_NAME = "codedAttribute";
225            final static String TYPE_NAME = "CodedAttributeType";
226    
227        }
228    
229    
230        /**
231         * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
232         * 
233         */
234        static class Elements {
235    
236            final static String NAME = "name";
237            final static String CODE = "code";
238            final static String SORT_CODE = "sortCode";
239            final static String ACTIVE = "active";
240    
241        }
242    
243        public static class Cache {
244            public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + CodedAttribute.Constants.TYPE_NAME;
245        }
246    
247    }