001/**
002 * Copyright 2005-2015 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.kim.api.common.attribute;
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.w3c.dom.Element;
023
024import javax.xml.bind.annotation.XmlAccessType;
025import javax.xml.bind.annotation.XmlAccessorType;
026import javax.xml.bind.annotation.XmlAnyElement;
027import javax.xml.bind.annotation.XmlElement;
028import javax.xml.bind.annotation.XmlRootElement;
029import javax.xml.bind.annotation.XmlType;
030import java.io.Serializable;
031import java.util.Collection;
032
033/**
034 * An immutable representation of a {@link KimAttributeContract}.
035 *
036 * <p>To construct an instance of a KimAttribute, use the {@link KimAttribute.Builder} class.<p/>
037 *
038 * @see KimAttributeContract
039 */
040@XmlRootElement(name = KimAttribute.Constants.ROOT_ELEMENT_NAME)
041@XmlAccessorType(XmlAccessType.NONE)
042@XmlType(name = KimAttribute.Constants.TYPE_NAME, propOrder = {
043        KimAttribute.Elements.ID,
044        KimAttribute.Elements.COMPONENT_NAME,
045        KimAttribute.Elements.ATTRIBUTE_NAME,
046        KimAttribute.Elements.NAMESPACE_CODE,
047        KimAttribute.Elements.ATTRIBUTE_LABEL,
048        KimAttribute.Elements.ACTIVE,
049        CoreConstants.CommonElements.VERSION_NUMBER,
050        CoreConstants.CommonElements.OBJECT_ID,
051        CoreConstants.CommonElements.FUTURE_ELEMENTS
052})
053public final class KimAttribute extends AbstractDataTransferObject implements KimAttributeContract {
054    private static final long serialVersionUID = 1L;
055
056    @XmlElement(name = KimAttribute.Elements.ID, required = false)
057    private final String id;
058
059    @XmlElement(name = KimAttribute.Elements.COMPONENT_NAME, required = false)
060    private final String componentName;
061
062    @XmlElement(name = KimAttribute.Elements.ATTRIBUTE_NAME, required = true)
063    private final String attributeName;
064
065    @XmlElement(name = KimAttribute.Elements.NAMESPACE_CODE, required = true)
066    private final String namespaceCode;
067
068    @XmlElement(name = KimAttribute.Elements.ATTRIBUTE_LABEL, required = false)
069    private final String attributeLabel;
070
071    @XmlElement(name = KimAttribute.Elements.ACTIVE, required = false)
072    private final boolean active;
073
074    @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
075    private final Long versionNumber;
076
077    @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
078    private final String objectId;
079
080    @SuppressWarnings("unused")
081    @XmlAnyElement
082    private final Collection<Element> _futureElements = null;
083
084    /**
085     * This constructor should never be called except during JAXB unmarshalling.
086     */
087    private KimAttribute() {
088        this.id = null;
089        this.componentName = null;
090        this.attributeName = null;
091        this.namespaceCode = null;
092        this.attributeLabel = null;
093        this.active = false;
094        this.versionNumber = Long.valueOf(1L);
095        this.objectId = null;
096    }
097
098    private KimAttribute(Builder builder) {
099        this.id = builder.getId();
100        this.componentName = builder.getComponentName();
101        this.attributeName = builder.getAttributeName();
102        this.namespaceCode = builder.getNamespaceCode();
103        this.attributeLabel = builder.getAttributeLabel();
104        this.active = builder.isActive();
105        this.versionNumber = builder.getVersionNumber();
106        this.objectId = builder.getObjectId();
107    }
108
109    @Override
110    public String getId() {
111        return id;
112    }
113
114    @Override
115    public String getComponentName() {
116        return componentName;
117    }
118
119    @Override
120    public String getAttributeName() {
121        return attributeName;
122    }
123
124    @Override
125    public String getNamespaceCode() {
126        return namespaceCode;
127    }
128
129    @Override
130    public String getAttributeLabel() {
131        return attributeLabel;
132    }
133
134    @Override
135    public Long getVersionNumber() {
136        return versionNumber;
137    }
138
139    @Override
140    public boolean isActive() {
141        return active;
142    }
143
144    @Override
145    public String getObjectId() {
146        return objectId;
147    }
148
149    /**
150     * This builder constructs an KimAttribute enforcing the constraints of the {@link KimAttributeContract}.
151     */
152    public static final class Builder implements KimAttributeContract, ModelBuilder, Serializable {
153        private String id;
154        private String componentName;
155        private String attributeName;
156        private String namespaceCode;
157        private String attributeLabel;
158        private boolean active;
159        private Long versionNumber = 1L;
160        private String objectId;
161
162        private Builder(String componentName, String attributeName, String namespaceCode) {
163            setComponentName(componentName);
164            setAttributeName(attributeName);
165            setNamespaceCode(namespaceCode);
166        }
167
168        /**
169         * creates a KimAttribute with the required fields.
170         */
171        public static Builder create(String componentName, String attributeName, String namespaceCode) {
172            return new Builder(componentName, attributeName, namespaceCode);
173        }
174
175        /**
176         * creates a KimAttribute from an existing {@link KimAttributeContract}.
177         */
178        public static Builder create(KimAttributeContract contract) {
179            Builder builder = new Builder(contract.getComponentName(), contract.getAttributeName(), contract.getNamespaceCode());
180            builder.setId(contract.getId());
181            builder.setAttributeLabel(contract.getAttributeLabel());
182            builder.setActive(contract.isActive());
183            builder.setVersionNumber(contract.getVersionNumber());
184            builder.setObjectId(contract.getObjectId());
185            return builder;
186        }
187
188
189        @Override
190        public String getId() {
191            return id;
192        }
193
194        public void setId(final String id) {
195            this.id = id;
196        }
197
198        @Override
199        public String getComponentName() {
200            return componentName;
201        }
202
203        public void setComponentName(final String componentName) {
204            this.componentName = componentName;
205        }
206
207        @Override
208        public String getAttributeName() {
209            return attributeName;
210        }
211
212        public void setAttributeName(final String attributeName) {
213            if (StringUtils.isBlank(attributeName)) {
214                throw new IllegalArgumentException("attributeName is blank");
215            }
216
217            this.attributeName = attributeName;
218        }
219
220        @Override
221        public String getNamespaceCode() {
222            return namespaceCode;
223        }
224
225        public void setNamespaceCode(final String namespaceCode) {
226            if (StringUtils.isBlank(namespaceCode)) {
227                throw new IllegalArgumentException("namespaceCode is blank");
228            }
229
230            this.namespaceCode = namespaceCode;
231        }
232
233        @Override
234        public String getAttributeLabel() {
235            return attributeLabel;
236        }
237
238        public void setAttributeLabel(final String attributeLabel) {
239            this.attributeLabel = attributeLabel;
240        }
241
242        @Override
243        public boolean isActive() {
244            return active;
245        }
246
247        public void setActive(final boolean active) {
248            this.active = active;
249        }
250
251        @Override
252        public Long getVersionNumber() {
253            return versionNumber;
254        }
255
256        public void setVersionNumber(final Long versionNumber) {
257            if (versionNumber <= 0) {
258                throw new IllegalArgumentException("versionNumber is invalid");
259            }
260
261            this.versionNumber = versionNumber;
262        }
263
264        @Override
265        public String getObjectId() {
266            return objectId;
267        }
268
269        public void setObjectId(final String objectId) {
270            this.objectId = objectId;
271        }
272
273        @Override
274        public KimAttribute build() {
275            return new KimAttribute(this);
276        }
277    }
278
279
280    /**
281     * Defines some internal constants used on this class.
282     */
283    static class Constants {
284        static final String ROOT_ELEMENT_NAME = "kimAttribute";
285        static final String TYPE_NAME = "KimAttributeType";
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        static final String ID = "id";
294        static final String COMPONENT_NAME = "componentName";
295        static final String ATTRIBUTE_NAME = "attributeName";
296        static final String NAMESPACE_CODE = "namespaceCode";
297        static final String ATTRIBUTE_LABEL = "attributeLabel";
298        static final String ACTIVE = "active";
299    }
300
301}