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 org.apache.commons.lang.StringUtils;
19  import org.joda.time.DateTime;
20  import org.kuali.rice.core.api.CoreConstants;
21  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
22  import org.kuali.rice.core.api.mo.ModelBuilder;
23  import org.kuali.rice.core.api.mo.common.active.InactivatableFromToUtils;
24  import org.kuali.rice.core.api.util.jaxb.DateTimeAdapter;
25  import org.kuali.rice.kim.api.KimConstants;
26  import org.w3c.dom.Element;
27  
28  import javax.xml.bind.annotation.XmlAccessType;
29  import javax.xml.bind.annotation.XmlAccessorType;
30  import javax.xml.bind.annotation.XmlAnyElement;
31  import javax.xml.bind.annotation.XmlElement;
32  import javax.xml.bind.annotation.XmlRootElement;
33  import javax.xml.bind.annotation.XmlType;
34  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
35  import java.io.Serializable;
36  import java.util.Collection;
37  
38  @XmlRootElement(name = CodedAttributeHistory.Constants.ROOT_ELEMENT_NAME)
39  @XmlAccessorType(XmlAccessType.NONE)
40  @XmlType(name = CodedAttributeHistory.Constants.TYPE_NAME, propOrder = {
41      CodedAttributeHistory.Elements.CODE,
42      CodedAttributeHistory.Elements.NAME,
43      CodedAttributeHistory.Elements.SORT_CODE,
44      CodedAttributeHistory.Elements.ACTIVE,
45      CoreConstants.CommonElements.HISTORY_ID,
46      CoreConstants.CommonElements.ACTIVE_FROM_DATE,
47      CoreConstants.CommonElements.ACTIVE_TO_DATE,
48      CoreConstants.CommonElements.VERSION_NUMBER,
49      CoreConstants.CommonElements.OBJECT_ID,
50      CoreConstants.CommonElements.FUTURE_ELEMENTS
51  })
52  public final class CodedAttributeHistory extends AbstractDataTransferObject
53      implements CodedAttributeHistoryContract
54  {
55      @XmlElement(name = Elements.CODE, required = true)
56      private final String code;
57      @XmlElement(name = Elements.NAME, required = false)
58      private final String name;
59      @XmlElement(name = Elements.SORT_CODE, required = false)
60      private final String sortCode;
61      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
62      private final Long versionNumber;
63      @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
64      private final String objectId;
65      @XmlElement(name = Elements.ACTIVE, required = false)
66      private final boolean active;
67      @XmlElement(name = CoreConstants.CommonElements.HISTORY_ID, required = false)
68      private final Long historyId;
69      @XmlElement(name = CoreConstants.CommonElements.ACTIVE_FROM_DATE, required = false)
70      @XmlJavaTypeAdapter(DateTimeAdapter.class)
71      private final DateTime activeFromDate;
72      @XmlElement(name = CoreConstants.CommonElements.ACTIVE_TO_DATE, required = false)
73      @XmlJavaTypeAdapter(DateTimeAdapter.class)
74      private final DateTime activeToDate;
75      @SuppressWarnings("unused")
76      @XmlAnyElement
77      private final Collection<Element> _futureElements = null;
78  
79      /**
80       * Private constructor used only by JAXB.
81       *
82       */
83      private CodedAttributeHistory() {
84          this.name = null;
85          this.code = null;
86          this.sortCode = null;
87          this.versionNumber = null;
88          this.objectId = null;
89          this.active = false;
90          this.historyId = null;
91          this.activeFromDate = null;
92          this.activeToDate = null;
93      }
94  
95      private CodedAttributeHistory(Builder builder) {
96          this.name = builder.getName();
97          this.code = builder.getCode();
98          this.sortCode = builder.getSortCode();
99          this.versionNumber = builder.getVersionNumber();
100         this.objectId = builder.getObjectId();
101         this.active = builder.isActive();
102         this.historyId = builder.getHistoryId();
103         this.activeFromDate = builder.getActiveFromDate();
104         this.activeToDate = builder.getActiveToDate();
105     }
106 
107     @Override
108     public String getName() {
109         return this.name;
110     }
111 
112     @Override
113     public String getCode() {
114         return this.code;
115     }
116 
117     @Override
118     public String getSortCode() {
119         return this.sortCode;
120     }
121 
122     @Override
123     public Long getVersionNumber() {
124         return this.versionNumber;
125     }
126 
127     @Override
128     public String getObjectId() {
129         return this.objectId;
130     }
131 
132     @Override
133     public boolean isActive() {
134         return this.active;
135     }
136 
137     @Override
138     public Long getHistoryId() {
139         return this.historyId;
140     }
141 
142     @Override
143     public DateTime getActiveFromDate() {
144         return this.activeFromDate;
145     }
146 
147     @Override
148     public DateTime getActiveToDate() {
149         return this.activeToDate;
150     }
151 
152     @Override
153     public boolean isActiveNow() {
154         return isActive() && InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
155     }
156 
157     @Override
158     public boolean isActive(DateTime activeAsOf) {
159         return isActive() && InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOf);
160     }
161 
162 
163     /**
164      * A builder which can be used to construct {@link org.kuali.rice.kim.api.identity.CodedAttributeHistory} instances.  Enforces the constraints of the {@link org.kuali.rice.kim.api.identity.CodedAttributeContract}.
165      *
166      */
167     public final static class Builder
168         implements Serializable, ModelBuilder, CodedAttributeHistoryContract
169     {
170 
171         private String name;
172         private String code;
173         private String sortCode;
174         private Long versionNumber;
175         private String objectId;
176         private boolean active;
177         private Long historyId;
178         private DateTime activeFromDate;
179         private DateTime activeToDate;
180 
181         private Builder(String code) {
182             setCode(code);
183         }
184 
185         public static Builder create(String code) {
186             return new Builder(code);
187         }
188 
189         public static Builder create(CodedAttributeHistoryContract contract) {
190             if (contract == null) {
191                 throw new IllegalArgumentException("contract was null");
192             }
193             // TODO if create() is modified to accept required parameters, this will need to be modified
194             Builder builder = create(contract.getCode());
195             builder.setName(contract.getName());
196             builder.setSortCode(contract.getSortCode());
197             builder.setVersionNumber(contract.getVersionNumber());
198             builder.setObjectId(contract.getObjectId());
199             builder.setActive(contract.isActive());
200             builder.setActiveFromDate(contract.getActiveFromDate());
201             builder.setActiveToDate(contract.getActiveToDate());
202             return builder;
203         }
204 
205         public CodedAttributeHistory build() {
206             return new CodedAttributeHistory(this);
207         }
208 
209         @Override
210         public String getName() {
211             return this.name;
212         }
213 
214         @Override
215         public String getCode() {
216             return this.code;
217         }
218 
219         @Override
220         public String getSortCode() {
221             return this.sortCode;
222         }
223 
224         @Override
225         public Long getVersionNumber() {
226             return this.versionNumber;
227         }
228 
229         @Override
230         public String getObjectId() {
231             return this.objectId;
232         }
233 
234         @Override
235         public boolean isActive() {
236             return this.active;
237         }
238 
239         @Override
240         public Long getHistoryId() {
241             return this.historyId;
242         }
243 
244         @Override
245         public boolean isActiveNow() {
246             return isActive() && InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
247         }
248 
249         @Override
250         public boolean isActive(DateTime activeAsOf) {
251             return isActive() && InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOf);
252         }
253 
254         @Override
255         public DateTime getActiveFromDate() {
256             return this.activeFromDate;
257         }
258 
259         @Override
260         public DateTime getActiveToDate() {
261             return this.activeToDate;
262         }
263 
264         public void setHistoryId(Long historyId) {
265             this.historyId = historyId;
266         }
267 
268         public void setActiveFromDate(DateTime activeFromDate) {
269             this.activeFromDate = activeFromDate;
270         }
271 
272         public void setActiveToDate(DateTime activeToDate) {
273             this.activeToDate = activeToDate;
274         }
275 
276         public void setName(String name) {
277             this.name = name;
278         }
279 
280         public void setCode(String code) {
281             if (StringUtils.isWhitespace(code)) {
282                 throw new IllegalArgumentException("code is empty");
283             }
284             this.code = code;
285         }
286 
287         public void setSortCode(String sortCode) {
288             this.sortCode = sortCode;
289         }
290 
291         public void setVersionNumber(Long versionNumber) {
292             this.versionNumber = versionNumber;
293         }
294 
295         public void setObjectId(String objectId) {
296             this.objectId = objectId;
297         }
298 
299         public void setActive(boolean active) {
300             this.active = active;
301         }
302 
303     }
304 
305 
306     /**
307      * Defines some internal constants used on this class.
308      *
309      */
310     static class Constants {
311 
312         final static String ROOT_ELEMENT_NAME = "codedAttribute";
313         final static String TYPE_NAME = "CodedAttributeType";
314 
315     }
316 
317 
318     /**
319      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
320      *
321      */
322     static class Elements {
323 
324         final static String NAME = "name";
325         final static String CODE = "code";
326         final static String SORT_CODE = "sortCode";
327         final static String ACTIVE = "active";
328 
329     }
330 
331     public static class Cache {
332         public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + CodedAttributeHistory.Constants.TYPE_NAME;
333     }
334 
335 }