View Javadoc

1   /**
2    * Copyright 2005-2011 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.notes;
17  
18  import org.hibernate.annotations.Fetch;
19  import org.hibernate.annotations.FetchMode;
20  import org.hibernate.annotations.GenericGenerator;
21  import org.hibernate.annotations.Parameter;
22  import org.joda.time.DateTime;
23  import org.kuali.rice.core.api.util.RiceConstants;
24  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
25  import org.kuali.rice.kew.api.note.NoteContract;
26  import org.kuali.rice.kew.service.KEWServiceLocator;
27  import org.kuali.rice.kew.api.KewApiConstants;
28  
29  import javax.persistence.CascadeType;
30  import javax.persistence.Column;
31  import javax.persistence.Entity;
32  import javax.persistence.FetchType;
33  import javax.persistence.GeneratedValue;
34  import javax.persistence.Id;
35  import javax.persistence.NamedQueries;
36  import javax.persistence.NamedQuery;
37  import javax.persistence.OneToMany;
38  import javax.persistence.Table;
39  import javax.persistence.Transient;
40  import javax.persistence.Version;
41  import java.io.Serializable;
42  import java.sql.Timestamp;
43  import java.text.DateFormat;
44  import java.text.SimpleDateFormat;
45  import java.util.ArrayList;
46  import java.util.Calendar;
47  import java.util.Date;
48  import java.util.List;
49  
50  
51  /**
52   * A note attached to a document.  May also contain a List of attachments.
53   * 
54   * @see Attachment
55   *
56   * @author Kuali Rice Team (rice.collab@kuali.org)
57   */
58  @Entity(name="org.kuali.rice.kew.notes.Note")
59  @Table(name="KREW_DOC_NTE_T")
60  //@Sequence(name="KREW_DOC_NTE_S",property="noteId")
61  @NamedQueries({
62  	@NamedQuery(name="KewNote.FindNoteByNoteId",query="select n from org.kuali.rice.kew.notes.Note as n where n.noteId = :noteId"),
63  	@NamedQuery(name="KewNote.FindNoteByDocumentId", query="select n from org.kuali.rice.kew.notes.Note as n where n.documentId = :documentId order by n.noteId")
64  })
65  public class Note implements Serializable, NoteContract {
66  
67  	private static final long serialVersionUID = -6136544551121011531L;
68  	@Id
69  	@GeneratedValue(generator="KREW_DOC_NTE_S")
70  	@GenericGenerator(name="KREW_DOC_NTE_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
71  			@Parameter(name="sequence_name",value="KREW_DOC_NTE_S"),
72  			@Parameter(name="value_column",value="id")
73  	})
74  	@Column(name="DOC_NTE_ID")
75  	private String noteId;
76      @Column(name="DOC_HDR_ID")
77  	private String documentId;
78      @Column(name="AUTH_PRNCPL_ID")
79  	private String noteAuthorWorkflowId;
80  	@Column(name="CRT_DT")
81  	private Timestamp noteCreateDate;
82      @Column(name="TXT")
83  	private String noteText;
84      @Version
85  	@Column(name="VER_NBR")
86  	private Integer lockVerNbr;
87      
88      @OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST},
89      	targetEntity=org.kuali.rice.kew.notes.Attachment.class, mappedBy="note")
90      @Fetch(value = FetchMode.SELECT)
91      private List<Attachment> attachments = new ArrayList<Attachment>();
92  
93      //additional data not in database
94      @Transient
95      private String noteAuthorEmailAddress;
96      @Transient
97      private String noteAuthorNetworkId;
98      @Transient
99      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