001/**
002 * Copyright 2005-2017 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.notes;
017
018import org.joda.time.DateTime;
019import org.kuali.rice.core.api.util.RiceConstants;
020import org.kuali.rice.kew.api.KewApiConstants;
021import org.kuali.rice.kew.api.note.NoteContract;
022import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
023
024import javax.persistence.CascadeType;
025import javax.persistence.Column;
026import javax.persistence.Entity;
027import javax.persistence.FetchType;
028import javax.persistence.GeneratedValue;
029import javax.persistence.Id;
030import javax.persistence.NamedQueries;
031import javax.persistence.NamedQuery;
032import javax.persistence.OneToMany;
033import javax.persistence.Table;
034import javax.persistence.Transient;
035import javax.persistence.Version;
036import java.io.Serializable;
037import java.sql.Timestamp;
038import java.text.DateFormat;
039import java.text.SimpleDateFormat;
040import java.util.ArrayList;
041import java.util.Calendar;
042import java.util.Date;
043import java.util.List;
044import java.util.Map;
045
046/**
047 * A note attached to a document.  May also contain a List of attachments.
048 * 
049 * @see Attachment
050 *
051 * @author Kuali Rice Team (rice.collab@kuali.org)
052 */
053@Entity(name="org.kuali.rice.kew.notes.Note")
054@Table(name="KREW_DOC_NTE_T")
055@NamedQueries({
056        @NamedQuery(name="KewNote.FindNoteByDocumentId", query="select n from org.kuali.rice.kew.notes.Note as n "
057            + "where n.documentId = :documentId order by n.noteId")
058})
059public class Note implements Serializable, NoteContract {
060
061        private static final long serialVersionUID = -6136544551121011531L;
062
063        @Id
064    @GeneratedValue(generator = "KREW_DOC_NTE_S")
065        @PortableSequenceGenerator(name="KREW_DOC_NTE_S")
066        @Column(name="DOC_NTE_ID")
067        private String noteId;
068
069    @Column(name="DOC_HDR_ID")
070        private String documentId;
071
072    @Column(name="AUTH_PRNCPL_ID")
073        private String noteAuthorWorkflowId;
074
075        @Column(name="CRT_DT")
076        private Timestamp noteCreateDate;
077
078    @Column(name="TXT")
079        private String noteText;
080
081    @Version
082        @Column(name="VER_NBR")
083        private Integer lockVerNbr;
084    
085    @OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="note")
086    private List<Attachment> attachments = new ArrayList<Attachment>();
087
088    //additional data not in database
089    @Transient
090    private String noteAuthorEmailAddress;
091    @Transient
092    private String noteAuthorNetworkId;
093    @Transient
094    private String noteAuthorFullName;
095    @Transient
096    private Long noteCreateLongDate;
097    @Transient
098    private Boolean authorizedToEdit; 
099    @Transient
100    private Boolean editingNote;
101    
102    public Integer getLockVerNbr() {
103        return lockVerNbr;
104    }
105
106    public void setLockVerNbr(Integer lockVerNbr) {
107        this.lockVerNbr = lockVerNbr;
108    }
109
110    public String getNoteAuthorWorkflowId() {
111        return noteAuthorWorkflowId;
112    }
113
114    public void setNoteAuthorWorkflowId(String noteAuthorWorkflowId) {
115        this.noteAuthorWorkflowId = noteAuthorWorkflowId;
116    }
117
118    public Timestamp getNoteCreateDate() {
119        return noteCreateDate;
120    }
121
122    public void setNoteCreateDate(Timestamp noteCreateDate) {
123        this.noteCreateDate = noteCreateDate;
124    }
125
126    public String getNoteId() {
127        return noteId;
128    }
129 
130    public void setNoteId(String noteId) {
131        this.noteId = noteId;
132    }
133 
134    public String getNoteText() {
135        return noteText;
136    }
137
138    public void setNoteText(String noteText) {
139        this.noteText = noteText;
140    }
141
142    public String getDocumentId() {
143        return documentId;
144    }
145
146    public void setDocumentId(String documentId) {
147        this.documentId = documentId;
148    }
149
150    public String getNoteAuthorEmailAddress() {
151        return noteAuthorEmailAddress;
152    }
153 
154    public void setNoteAuthorEmailAddress(String noteAuthorEmailAddress) {
155        this.noteAuthorEmailAddress = noteAuthorEmailAddress;
156    }
157
158    public String getNoteAuthorFullName() {
159        return noteAuthorFullName;
160    }
161
162    public void setNoteAuthorFullName(String noteAuthorFullName) {
163        this.noteAuthorFullName = noteAuthorFullName;
164    }
165
166    public String getNoteAuthorNetworkId() {
167        return noteAuthorNetworkId;
168    }
169
170    public void setNoteAuthorNetworkId(String noteAuthorNetworkId) {
171        this.noteAuthorNetworkId = noteAuthorNetworkId;
172    }
173
174    public Long getNoteCreateLongDate() {
175        return noteCreateLongDate;
176    }
177
178    public void setNoteCreateLongDate(Long noteCreateLongDate) {
179        this.noteCreateLongDate = noteCreateLongDate;
180    }
181
182    public Boolean getAuthorizedToEdit() {
183        return authorizedToEdit;
184    }
185
186    public void setAuthorizedToEdit(Boolean authorizedToEdit) {
187        this.authorizedToEdit = authorizedToEdit;
188    }
189
190    public Boolean getEditingNote() {
191        return editingNote;
192    }
193
194    public void setEditingNote(Boolean editingNote) {
195        this.editingNote = editingNote;
196    }
197
198    public String getFormattedCreateDateTime() {
199        long time = getNoteCreateDate().getTime();
200        Calendar calendar = Calendar.getInstance();
201        calendar.setTimeInMillis(time);
202        Date date = calendar.getTime();
203        DateFormat dateFormat = new SimpleDateFormat(KewApiConstants.TIMESTAMP_DATE_FORMAT_PATTERN2);
204        return dateFormat.format(date);
205    }
206
207    public String getFormattedCreateDate() {
208        long time = getNoteCreateDate().getTime();
209        Calendar calendar = Calendar.getInstance();
210        calendar.setTimeInMillis(time);
211        Date date = calendar.getTime();
212        DateFormat dateFormat = RiceConstants.getDefaultDateFormat();
213        return dateFormat.format(date);
214    }
215    
216    public String getFormattedCreateTime() {
217        long time = getNoteCreateDate().getTime();
218        Calendar calendar = Calendar.getInstance();
219        calendar.setTimeInMillis(time);
220        Date date = calendar.getTime();
221        DateFormat dateFormat = RiceConstants.getDefaultTimeFormat();
222        return dateFormat.format(date);
223    }
224
225        public List<Attachment> getAttachments() {
226                return attachments;
227        }
228
229        public void setAttachments(List<Attachment> attachments) {
230                this.attachments = attachments;
231        }
232
233        // new methods from NoteContract in 2.0
234
235        @Override
236        public String getId() {
237                if (getNoteId() == null) {
238                        return null;
239                }
240                return getNoteId().toString();
241        }
242
243        @Override
244        public Long getVersionNumber() {
245                if (getLockVerNbr() == null) {
246                        return null;
247                }
248                return new Long(getLockVerNbr().longValue());
249        }
250
251        @Override
252        public String getAuthorPrincipalId() {
253                return getNoteAuthorWorkflowId();
254        }
255
256        @Override
257        public DateTime getCreateDate() {
258                if (getNoteCreateDate() == null) {
259                        return null;
260                }
261                return new DateTime(getNoteCreateDate().getTime());
262        }
263
264        @Override
265        public String getText() {
266                return getNoteText();
267        }
268
269    public Note deepCopy(Map<Object, Object> visited) {
270        if (visited.containsKey(this)) {
271            return (Note)visited.get(this);
272        }
273        Note copy = new Note();
274        visited.put(this, copy);
275        copy.noteId = noteId;
276        copy.documentId = documentId;
277        copy.noteAuthorWorkflowId = noteAuthorWorkflowId;
278        if (noteCreateDate != null) {
279            copy.noteCreateDate = new Timestamp(noteCreateDate.getTime());
280        }
281        copy.noteText = noteText;
282        copy.lockVerNbr = lockVerNbr;
283        copy.noteAuthorEmailAddress = noteAuthorEmailAddress;
284        copy.noteAuthorNetworkId = noteAuthorNetworkId;
285        copy.noteAuthorFullName = noteAuthorFullName;
286        copy.noteCreateLongDate = noteCreateLongDate;
287        copy.authorizedToEdit = authorizedToEdit;
288        copy.editingNote = editingNote;
289        if (attachments != null) {
290            List<Attachment> copies = new ArrayList<Attachment>();
291            for (Attachment attachment : attachments) {
292                copies.add(attachment.deepCopy(visited));
293            }
294            copy.attachments = copies;
295        }
296        return copy;
297    }
298        
299        public static org.kuali.rice.kew.api.note.Note to(Note note) {
300                if (note == null) {
301                        return null;
302                }
303                return org.kuali.rice.kew.api.note.Note.Builder.create(note).build();
304        }
305        
306        public static Note from(org.kuali.rice.kew.api.note.Note note) {
307                if (note == null) {
308                        return null;
309                }
310                Note noteBo = new Note();
311                if (note.getId() != null) {
312                        noteBo.setNoteId(note.getId());
313                }
314                noteBo.setDocumentId(note.getDocumentId());
315                noteBo.setNoteAuthorWorkflowId(note.getAuthorPrincipalId());
316                if (note.getCreateDate() != null) {
317                        noteBo.setNoteCreateDate(new Timestamp(note.getCreateDate().getMillis()));
318                }
319                noteBo.setNoteText(note.getText());
320                if (note.getVersionNumber() != null) {
321                        noteBo.setLockVerNbr(Integer.valueOf(note.getVersionNumber().intValue()));
322                }
323                return noteBo;
324        }
325        
326}
327