View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.api.common.attribute;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreConstants;
20  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
21  import org.kuali.rice.core.api.mo.ModelBuilder;
22  import org.w3c.dom.Element;
23  
24  import javax.xml.bind.annotation.XmlAccessType;
25  import javax.xml.bind.annotation.XmlAccessorType;
26  import javax.xml.bind.annotation.XmlAnyElement;
27  import javax.xml.bind.annotation.XmlElement;
28  import javax.xml.bind.annotation.XmlRootElement;
29  import javax.xml.bind.annotation.XmlType;
30  import java.io.Serializable;
31  import java.util.Collection;
32  
33  /**
34   * An immutable representation of a {@link KimAttributeContract}.
35   *
36   * <p>To construct an instance of a KimAttribute, use the {@link KimAttribute.Builder} class.<p/>
37   *
38   * @see KimAttributeContract
39   */
40  @XmlRootElement(name = KimAttribute.Constants.ROOT_ELEMENT_NAME)
41  @XmlAccessorType(XmlAccessType.NONE)
42  @XmlType(name = KimAttribute.Constants.TYPE_NAME, propOrder = {
43          KimAttribute.Elements.ID,
44          KimAttribute.Elements.COMPONENT_NAME,
45          KimAttribute.Elements.ATTRIBUTE_NAME,
46          KimAttribute.Elements.NAMESPACE_CODE,
47          KimAttribute.Elements.ATTRIBUTE_LABEL,
48          KimAttribute.Elements.ACTIVE,
49          CoreConstants.CommonElements.VERSION_NUMBER,
50          CoreConstants.CommonElements.OBJECT_ID,
51          CoreConstants.CommonElements.FUTURE_ELEMENTS
52  })
53  public final class KimAttribute extends AbstractDataTransferObject implements KimAttributeContract {
54      private static final long serialVersionUID = 1L;
55  
56      @XmlElement(name = KimAttribute.Elements.ID, required = false)
57      private final String id;
58  
59      @XmlElement(name = KimAttribute.Elements.COMPONENT_NAME, required = true)
60      private final String componentName;
61  
62      @XmlElement(name = KimAttribute.Elements.ATTRIBUTE_NAME, required = true)
63      private final String attributeName;
64  
65      @XmlElement(name = KimAttribute.Elements.NAMESPACE_CODE, required = true)
66      private final String namespaceCode;
67  
68      @XmlElement(name = KimAttribute.Elements.ATTRIBUTE_LABEL, required = false)
69      private final String attributeLabel;
70  
71      @XmlElement(name = KimAttribute.Elements.ACTIVE, required = false)
72      private final boolean active;
73  
74      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
75      private final Long versionNumber;
76  
77      @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
78      private final String objectId;
79  
80      @SuppressWarnings("unused")
81      @XmlAnyElement
82      private final Collection<Element> _futureElements = null;
83  
84      /**
85       * This constructor should never be called except during JAXB unmarshalling.
86       */
87      private KimAttribute() {
88          this.id = null;
89          this.componentName = null;
90          this.attributeName = null;
91          this.namespaceCode = null;
92          this.attributeLabel = null;
93          this.active = false;
94          this.versionNumber = Long.valueOf(1L);
95          this.objectId = null;
96      }
97  
98      private KimAttribute(Builder builder) {
99          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             if (StringUtils.isBlank(componentName)) {
205                 throw new IllegalArgumentException("componentName is blank");
206             }
207 
208             this.componentName = componentName;
209         }
210 
211         @Override
212         public String getAttributeName() {
213             return attributeName;
214         }
215 
216         public void setAttributeName(final String attributeName) {
217             if (StringUtils.isBlank(attributeName)) {
218                 throw new IllegalArgumentException("attributeName is blank");
219             }
220 
221             this.attributeName = attributeName;
222         }
223 
224         @Override
225         public String getNamespaceCode() {
226             return namespaceCode;
227         }
228 
229         public void setNamespaceCode(final String namespaceCode) {
230             if (StringUtils.isBlank(namespaceCode)) {
231                 throw new IllegalArgumentException("namespaceCode is blank");
232             }
233 
234             this.namespaceCode = namespaceCode;
235         }
236 
237         @Override
238         public String getAttributeLabel() {
239             return attributeLabel;
240         }
241 
242         public void setAttributeLabel(final String attributeLabel) {
243             this.attributeLabel = attributeLabel;
244         }
245 
246         @Override
247         public boolean isActive() {
248             return active;
249         }
250 
251         public void setActive(final boolean active) {
252             this.active = active;
253         }
254 
255         @Override
256         public Long getVersionNumber() {
257             return versionNumber;
258         }
259 
260         public void setVersionNumber(final Long versionNumber) {
261             if (versionNumber == null || versionNumber <= 0) {
262                 throw new IllegalArgumentException("versionNumber is invalid");
263             }
264 
265             this.versionNumber = versionNumber;
266         }
267 
268         @Override
269         public String getObjectId() {
270             return objectId;
271         }
272 
273         public void setObjectId(final String objectId) {
274             this.objectId = objectId;
275         }
276 
277         @Override
278         public KimAttribute build() {
279             return new KimAttribute(this);
280         }
281     }
282 
283 
284     /**
285      * Defines some internal constants used on this class.
286      */
287     static class Constants {
288         static final String ROOT_ELEMENT_NAME = "kimAttribute";
289         static final String TYPE_NAME = "KimAttributeType";
290     }
291 
292     /**
293      * A private class which exposes constants which define the XML element names to use
294      * when this object is marshalled to XML.
295      */
296     static class Elements {
297         static final String ID = "id";
298         static final String COMPONENT_NAME = "componentName";
299         static final String ATTRIBUTE_NAME = "attributeName";
300         static final String NAMESPACE_CODE = "namespaceCode";
301         static final String ATTRIBUTE_LABEL = "attributeLabel";
302         static final String ACTIVE = "active";
303     }
304 
305 }