001/**
002 * Copyright 2005-2016 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.kew.api.note;
017
018import java.io.Serializable;
019import java.util.Collection;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAnyElement;
024import javax.xml.bind.annotation.XmlElement;
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.XmlType;
027import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
028
029import org.apache.commons.lang.StringUtils;
030import org.joda.time.DateTime;
031import org.kuali.rice.core.api.CoreConstants;
032import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
033import org.kuali.rice.core.api.mo.ModelBuilder;
034import org.kuali.rice.core.api.util.jaxb.DateTimeAdapter;
035import org.w3c.dom.Element;
036
037@XmlRootElement(name = Note.Constants.ROOT_ELEMENT_NAME)
038@XmlAccessorType(XmlAccessType.NONE)
039@XmlType(name = Note.Constants.TYPE_NAME, propOrder = {
040            Note.Elements.ID,
041                Note.Elements.DOCUMENT_ID,
042                Note.Elements.AUTHOR_PRINCIPAL_ID,
043                Note.Elements.CREATE_DATE,
044                Note.Elements.TEXT,
045                Note.Elements.CREATE_DATE_VALUE,
046        CoreConstants.CommonElements.VERSION_NUMBER,
047                CoreConstants.CommonElements.FUTURE_ELEMENTS
048})
049public final class Note extends AbstractDataTransferObject implements NoteContract {
050
051        private static final long serialVersionUID = 6619061362854480922L;
052
053        @XmlElement(name = Elements.ID, required = false)
054    private final String id;
055        
056    @XmlElement(name = Elements.DOCUMENT_ID, required = true)
057    private final String documentId;
058    
059    @XmlElement(name = Elements.AUTHOR_PRINCIPAL_ID, required = true)
060    private final String authorPrincipalId;
061
062    @Deprecated
063    @XmlElement(name = Elements.CREATE_DATE, required = false)
064    private final DateTime createDate;
065
066    @XmlElement(name = Elements.CREATE_DATE_VALUE, required = false)
067    @XmlJavaTypeAdapter(DateTimeAdapter.class)
068    private final DateTime createDateValue;
069    
070    @XmlElement(name = Elements.TEXT, required = false)
071    private final String text;
072    
073    @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
074    private final Long versionNumber;
075    
076    @SuppressWarnings("unused")
077    @XmlAnyElement
078    private final Collection<Element> _futureElements = null;
079
080    /**
081     * Private constructor used only by JAXB.
082     * 
083     */
084    private Note() {
085        this.id = null;
086        this.documentId = null;
087        this.authorPrincipalId = null;
088        this.createDate = null;
089        this.createDateValue = null;
090        this.text = null;
091        this.versionNumber = null;
092    }
093
094    private Note(Builder builder) {
095        this.id = builder.getId();
096        this.documentId = builder.getDocumentId();
097        this.authorPrincipalId = builder.getAuthorPrincipalId();
098        this.createDate = builder.getCreateDate();
099        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