View Javadoc

1   /**
2    * Copyright 2005-2013 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.identity;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  import javax.xml.bind.annotation.XmlAccessType;
21  import javax.xml.bind.annotation.XmlAccessorType;
22  import javax.xml.bind.annotation.XmlAnyElement;
23  import javax.xml.bind.annotation.XmlElement;
24  import javax.xml.bind.annotation.XmlRootElement;
25  import javax.xml.bind.annotation.XmlType;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.kuali.rice.core.api.CoreConstants;
29  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
30  import org.kuali.rice.core.api.mo.ModelBuilder;
31  import org.kuali.rice.kim.api.KimConstants;
32  import org.w3c.dom.Element;
33  
34  @XmlRootElement(name = CodedAttribute.Constants.ROOT_ELEMENT_NAME)
35  @XmlAccessorType(XmlAccessType.NONE)
36  @XmlType(name = CodedAttribute.Constants.TYPE_NAME, propOrder = {
37      CodedAttribute.Elements.CODE,
38      CodedAttribute.Elements.NAME,
39      CodedAttribute.Elements.SORT_CODE,
40      CodedAttribute.Elements.ACTIVE,
41      CoreConstants.CommonElements.VERSION_NUMBER,
42      CoreConstants.CommonElements.OBJECT_ID,
43      CoreConstants.CommonElements.FUTURE_ELEMENTS
44  })
45  public final class CodedAttribute extends AbstractDataTransferObject
46      implements CodedAttributeContract
47  {
48      @XmlElement(name = Elements.CODE, required = true)
49      private final String code;
50      @XmlElement(name = Elements.NAME, required = false)
51      private final String name;
52      @XmlElement(name = Elements.SORT_CODE, required = false)
53      private final String sortCode;
54      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
55      private final Long versionNumber;
56      @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
57      private final String objectId;
58      @XmlElement(name = Elements.ACTIVE, required = false)
59      private final boolean active;
60      @SuppressWarnings("unused")
61      @XmlAnyElement
62      private final Collection<Element> _futureElements = null;
63  
64      /**
65       * Private constructor used only by JAXB.
66       * 
67       */
68      private CodedAttribute() {
69          this.name = null;
70          this.code = null;
71          this.sortCode = null;
72          this.versionNumber = null;
73          this.objectId = null;
74          this.active = false;
75      }
76  
77      private CodedAttribute(Builder builder) {
78          this.name = builder.getName();
79          this.code = builder.getCode();
80          this.sortCode = builder.getSortCode();
81          this.versionNumber = builder.getVersionNumber();
82          this.objectId = builder.getObjectId();
83          this.active = builder.isActive();
84      }
85  
86      @Override
87      public String getName() {
88          return this.name;
89      }
90  
91      @Override
92      public String getCode() {
93          return this.code;
94      }
95  
96      @Override
97      public String getSortCode() {
98          return this.sortCode;
99      }
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 }