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