View Javadoc

1   /**
2    * Copyright 2005-2014 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.kew.api.note;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  
21  import javax.xml.bind.annotation.XmlAccessType;
22  import javax.xml.bind.annotation.XmlAccessorType;
23  import javax.xml.bind.annotation.XmlAnyElement;
24  import javax.xml.bind.annotation.XmlElement;
25  import javax.xml.bind.annotation.XmlRootElement;
26  import javax.xml.bind.annotation.XmlType;
27  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.joda.time.DateTime;
31  import org.kuali.rice.core.api.CoreConstants;
32  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
33  import org.kuali.rice.core.api.mo.ModelBuilder;
34  import org.kuali.rice.core.api.util.jaxb.DateTimeAdapter;
35  import org.w3c.dom.Element;
36  
37  @XmlRootElement(name = Note.Constants.ROOT_ELEMENT_NAME)
38  @XmlAccessorType(XmlAccessType.NONE)
39  @XmlType(name = Note.Constants.TYPE_NAME, propOrder = {
40  	    Note.Elements.ID,
41  		Note.Elements.DOCUMENT_ID,
42  		Note.Elements.AUTHOR_PRINCIPAL_ID,
43  		Note.Elements.CREATE_DATE,
44  		Note.Elements.TEXT,
45  		Note.Elements.CREATE_DATE_VALUE,
46          CoreConstants.CommonElements.VERSION_NUMBER,
47  		CoreConstants.CommonElements.FUTURE_ELEMENTS
48  })
49  public final class Note extends AbstractDataTransferObject implements NoteContract {
50  
51  	private static final long serialVersionUID = 6619061362854480922L;
52  
53  	@XmlElement(name = Elements.ID, required = false)
54      private final String id;
55  	
56      @XmlElement(name = Elements.DOCUMENT_ID, required = true)
57      private final String documentId;
58      
59      @XmlElement(name = Elements.AUTHOR_PRINCIPAL_ID, required = true)
60      private final String authorPrincipalId;
61  
62      @Deprecated
63      @XmlElement(name = Elements.CREATE_DATE, required = false)
64      private final DateTime createDate;
65  
66      @XmlElement(name = Elements.CREATE_DATE_VALUE, required = false)
67      @XmlJavaTypeAdapter(DateTimeAdapter.class)
68      private final DateTime createDateValue;
69      
70      @XmlElement(name = Elements.TEXT, required = false)
71      private final String text;
72      
73      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
74      private final Long versionNumber;
75      
76      @SuppressWarnings("unused")
77      @XmlAnyElement
78      private final Collection<Element> _futureElements = null;
79  
80      /**
81       * Private constructor used only by JAXB.
82       * 
83       */
84      private Note() {
85          this.id = null;
86      	this.documentId = null;
87          this.authorPrincipalId = null;
88          this.createDate = null;
89          this.createDateValue = null;
90          this.text = null;
91          this.versionNumber = null;
92      }
93  
94      private Note(Builder builder) {
95          this.id = builder.getId();
96      	this.documentId = builder.getDocumentId();
97          this.authorPrincipalId = builder.getAuthorPrincipalId();
98          this.createDate = builder.getCreateDate();
99          this.createDateValue = builder.getCreateDate();
100         this.text = builder.getText();
101         this.versionNumber = builder.getVersionNumber();
102     }
103 
104     @Override
105     public String getId() {
106         return this.id;
107     }
108 
109     @Override
110     public String getDocumentId() {
111         return this.documentId;
112     }
113 
114     @Override
115     public String getAuthorPrincipalId() {
116         return this.authorPrincipalId;
117     }
118 
119     @Override
120     public DateTime getCreateDate() {
121         return this.createDateValue == null ? this.createDate : this.createDateValue;
122     }
123 
124     @Override
125     public String getText() {
126         return this.text;
127     }
128 
129     @Override
130     public Long getVersionNumber() {
131         return this.versionNumber;
132     }
133 
134     /**
135      * A builder which can be used to construct {@link Note} instances.  Enforces the constraints of the {@link NoteContract}.
136      */
137     public final static class Builder implements Serializable, ModelBuilder, NoteContract {
138 
139 		private static final long serialVersionUID = 6457130539374835936L;
140 
141 		private String id;
142         private String documentId;
143         private String authorPrincipalId;
144         private DateTime createDate;
145         private String text;
146         private Long versionNumber;
147 
148         private Builder(String documentId, String authorPrincipalId) {
149             setDocumentId(documentId);
150             setAuthorPrincipalId(authorPrincipalId);
151         }
152 
153         public static Builder create(String documentId, String authorPrincipalId) {
154             return new Builder(documentId, authorPrincipalId);
155         }
156 
157         public static Builder create(NoteContract contract) {
158             if (contract == null) {
159                 throw new IllegalArgumentException("contract was null");
160             }
161             // TODO if create() is modified to accept required parameters, this will need to be modified
162             Builder builder = create(contract.getDocumentId(), contract.getAuthorPrincipalId());
163             builder.setId(contract.getId());
164             builder.setCreateDate(contract.getCreateDate());
165             builder.setText(contract.getText());
166             builder.setVersionNumber(contract.getVersionNumber());
167             return builder;
168         }
169 
170         public Note build() {
171             return new Note(this);
172         }
173         
174         @Override
175         public String getId() {
176             return this.id;
177         }
178 
179         @Override
180         public String getDocumentId() {
181             return this.documentId;
182         }
183 
184         @Override
185         public String getAuthorPrincipalId() {
186             return this.authorPrincipalId;
187         }
188 
189         @Override
190         public DateTime getCreateDate() {
191             return this.createDate;
192         }
193 
194         @Override
195         public String getText() {
196             return this.text;
197         }
198 
199         @Override
200         public Long getVersionNumber() {
201             return this.versionNumber;
202         }
203 
204         public void setId(String id) {
205             this.id = id;
206         }
207 
208         public void setDocumentId(String documentId) {
209         	if (StringUtils.isBlank(documentId)) {
210         		throw new IllegalArgumentException("documentId was null or blank");
211         	}
212             this.documentId = documentId;
213         }
214 
215         public void setAuthorPrincipalId(String authorPrincipalId) {
216         	if (StringUtils.isBlank(authorPrincipalId)) {
217         		throw new IllegalArgumentException("authorPrincipalId was null or blank");
218         	}
219             this.authorPrincipalId = authorPrincipalId;
220         }
221 
222         public void setCreateDate(DateTime createDate) {
223             this.createDate = createDate;
224         }
225 
226         public void setText(String text) {
227             this.text = text;
228         }
229         
230         public void setVersionNumber(Long versionNumber) {
231             this.versionNumber = versionNumber;
232         }
233 
234     }
235 
236     /**
237      * Defines some internal constants used on this class.
238      */
239     static class Constants {
240         final static String ROOT_ELEMENT_NAME = "note";
241         final static String TYPE_NAME = "NoteType";
242     }
243 
244     /**
245      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
246      */
247     static class Elements {
248         final static String DOCUMENT_ID = "documentId";
249         final static String AUTHOR_PRINCIPAL_ID = "authorPrincipalId";
250         final static String CREATE_DATE = "createDate";
251         final static String CREATE_DATE_VALUE = "createDateValue";
252         final static String TEXT = "text";
253         final static String ID = "id";
254     }
255 
256 }
257