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