001    /**
002     * Copyright 2005-2014 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     */
016    package org.kuali.rice.krad.bo;
017    
018    import org.hibernate.annotations.GenericGenerator;
019    import org.hibernate.annotations.Parameter;
020    import org.kuali.rice.core.api.CoreApiServiceLocator;
021    import org.kuali.rice.kim.api.identity.Person;
022    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
023    import org.kuali.rice.krad.service.KRADServiceLocator;
024    import org.kuali.rice.krad.util.KRADConstants;
025    import org.kuali.rice.krad.util.UrlFactory;
026    
027    import javax.persistence.CascadeType;
028    import javax.persistence.Column;
029    import javax.persistence.Entity;
030    import javax.persistence.FetchType;
031    import javax.persistence.GeneratedValue;
032    import javax.persistence.Id;
033    import javax.persistence.JoinColumn;
034    import javax.persistence.OneToOne;
035    import javax.persistence.Table;
036    import javax.persistence.Transient;
037    import java.sql.Timestamp;
038    import java.util.Properties;
039    
040    /**
041     * Represents a user note in the system.
042     */
043    @Entity
044    @Table(name="KRNS_NTE_T")
045    public class Note extends PersistableBusinessObjectBase {
046        private static final long serialVersionUID = -7647166354016356770L;
047    
048        @Id
049        @GeneratedValue(generator="KRNS_NTE_S")
050            @GenericGenerator(name="KRNS_NTE_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
051                            @Parameter(name="sequence_name",value="KRNS_NTE_S"),
052                            @Parameter(name="value_column",value="id")
053            })
054            @Column(name="NTE_ID")
055            private Long noteIdentifier;
056        @Column(name="RMT_OBJ_ID")
057            private String remoteObjectIdentifier;
058        @Column(name="AUTH_PRNCPL_ID")
059            private String authorUniversalIdentifier;
060            @Column(name="POST_TS")
061            private Timestamp notePostedTimestamp;
062        @Column(name="NTE_TYP_CD")
063            private String noteTypeCode;
064        @Column(name="TXT")
065            private String noteText;
066        @Column(name="TPC_TXT")
067            private String noteTopicText;
068        @Column(name="PRG_CD")
069            private String notePurgeCode;
070        @Transient
071        private String attachmentIdentifier;
072    
073        @OneToOne(fetch=FetchType.EAGER)
074            @JoinColumn(name="NTE_TYP_CD", insertable=false, updatable=false)
075            private NoteType noteType;
076        @Transient
077        private transient Person authorUniversal;
078        @OneToOne(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
079            @JoinColumn(name = "NTE_ID", insertable = false, updatable = false)
080            private Attachment attachment;
081        @Transient
082        private AdHocRouteRecipient adHocRouteRecipient;
083    
084        /**
085         * Default constructor.
086         */
087        public Note() {
088            super();
089    
090            //this.setNotePostedTimestampToCurrent();
091            this.setNoteText(KRADConstants.EMPTY_STRING);
092            // for now just do this
093            this.setNoteTypeCode("DH");
094    
095            this.setAdHocRouteRecipient(new AdHocRoutePerson());
096        }
097        
098        /**
099         * Sets the {@link #setNotePostedTimestamp(Timestamp)} to the current time.
100         */
101        public void setNotePostedTimestampToCurrent() {
102            final Timestamp now = CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp();
103            this.setNotePostedTimestamp(now);
104        }
105    
106        /**
107         * Gets the noteIdentifier attribute.
108         * 
109         * @return Returns the noteIdentifier.
110         */
111        public Long getNoteIdentifier() {
112            return noteIdentifier;
113        }
114    
115        /**
116         * Sets the noteIdentifier attribute value.
117         * 
118         * @param noteIdentifier The noteIdentifier to set.
119         */
120        public void setNoteIdentifier(Long noteIdentifier) {
121            this.noteIdentifier = noteIdentifier;
122        }
123    
124        /**
125         * Gets the remoteObjectIdentifier attribute.
126         * 
127         * @return Returns the remoteObjectIdentifier
128         */
129        public String getRemoteObjectIdentifier() {
130            return remoteObjectIdentifier;
131        }
132    
133        /**
134         * Sets the remoteObjectIdentifier attribute.
135         * 
136         * @param remoteObjectIdentifier The remoteObjectIdentifier to set.
137         */
138        public void setRemoteObjectIdentifier(String remoteObjectIdentifier) {
139            this.remoteObjectIdentifier = remoteObjectIdentifier;
140        }
141    
142    
143        /**
144         * Gets the authorUniversalIdentifier attribute.
145         * 
146         * @return Returns the authorUniversalIdentifier
147         */
148        public String getAuthorUniversalIdentifier() {
149            return authorUniversalIdentifier;
150        }
151    
152        /**
153         * Sets the authorUniversalIdentifier attribute.
154         * 
155         * @param noteAuthorIdentifier The author ID to be set as the AuthorUniversalIdentifier
156         */
157        public void setAuthorUniversalIdentifier(String noteAuthorIdentifier) {
158            this.authorUniversalIdentifier = noteAuthorIdentifier;
159        }
160    
161    
162        /**
163         * Gets the notePostedTimestamp attribute.
164         * 
165         * @return Returns the notePostedTimestamp
166         */
167        public Timestamp getNotePostedTimestamp() {
168            return notePostedTimestamp;
169        }
170    
171        /**
172         * Sets the notePostedTimestamp attribute.
173         * 
174         * @param notePostedTimestamp The notePostedTimestamp to set.
175         */
176        public void setNotePostedTimestamp(Timestamp notePostedTimestamp) {
177            this.notePostedTimestamp = notePostedTimestamp;
178        }
179    
180    
181        /**
182         * Gets the noteTypeCode attribute.
183         * 
184         * @return Returns the noteTypeCode
185         */
186        public String getNoteTypeCode() {
187            return noteTypeCode;
188        }
189    
190        /**
191         * Sets the noteTypeCode attribute.
192         * 
193         * @param noteTypeCode The noteTypeCode to set.
194         */
195        public void setNoteTypeCode(String noteTypeCode) {
196            this.noteTypeCode = noteTypeCode;
197        }
198    
199    
200        /**
201         * Gets the noteText attribute.
202         * 
203         * @return Returns the noteText
204         */
205        public String getNoteText() {
206            return noteText;
207        }
208    
209        /**
210         * Sets the noteText attribute.
211         * 
212         * @param noteText The noteText to set.
213         */
214        public void setNoteText(String noteText) {
215            this.noteText = noteText;
216        }
217    
218    
219        /**
220         * Gets the noteTopicText attribute.
221         * 
222         * @return Returns the noteTopicText.
223         */
224        public String getNoteTopicText() {
225            return noteTopicText;
226        }
227    
228        /**
229         * Sets the noteTopicText attribute value.
230         * 
231         * @param noteTopicText The noteTopicText to set.
232         */
233        public void setNoteTopicText(String noteTopicText) {
234            this.noteTopicText = noteTopicText;
235        }
236    
237        /**
238         * Gets the notePurgeCode attribute.
239         * 
240         * @return Returns the notePurgeCode
241         */
242        public String getNotePurgeCode() {
243            return notePurgeCode;
244        }
245    
246        /**
247         * Sets the notePurgeCode attribute.
248         * 
249         * @param notePurgeCode The notePurgeCode to set.
250         */
251        public void setNotePurgeCode(String notePurgeCode) {
252            this.notePurgeCode = notePurgeCode;
253        }
254    
255        /**
256         * Gets the noteType attribute.
257         * 
258         * @return Returns the noteType.
259         */
260        public NoteType getNoteType() {
261            return noteType;
262        }
263    
264        /**
265         * Sets the noteType attribute value.
266         * 
267         * @param noteType The noteType to set.
268         * @deprecated
269         */
270        public void setNoteType(NoteType noteType) {
271            this.noteType = noteType;
272        }
273    
274        /**
275         * Gets the authorUniversal attribute.
276         * 
277         * @return Returns the authorUniversal.
278         */
279        public Person getAuthorUniversal() {
280            authorUniversal = KimApiServiceLocator.getPersonService().updatePersonIfNecessary(authorUniversalIdentifier, authorUniversal);
281            return authorUniversal;
282        }
283    
284        /**
285         * Sets the authorUniversal attribute value.
286         * 
287         * @param authorUniversal The authorUniversal to set.
288         * @deprecated
289         */
290        public void setAuthorUniversal(Person authorUniversal) {
291            this.authorUniversal = authorUniversal;
292        }
293    
294        /**
295         * Gets the attachment attribute.
296         * 
297         * @return Returns the attachment.
298         */
299        public Attachment getAttachment() {
300            return attachment;
301        }
302    
303        /**
304         * Sets the attachment attribute value.
305         * 
306         * @param attachment The attachment to set.
307         */
308        public void setAttachment(Attachment attachment) {
309            this.attachment = attachment;
310        }
311    
312        /**
313         * Gets the attachmentIdentifier attribute.
314         * 
315         * @return Returns the attachmentIdentifier.
316         */
317        public String getAttachmentIdentifier() {
318            return attachmentIdentifier;
319        }
320    
321        /**
322         * Sets the attachmentIdentifier attribute value.
323         * 
324         * @param attachmentIdentifier The attachmentIdentifier to set.
325         */
326        public void setAttachmentIdentifier(String attachmentIdentifier) {
327            this.attachmentIdentifier = attachmentIdentifier;
328        }
329    
330        /**
331         * Adds the given attachment to this note. More specifically, sets both the attachmentIdentifier and the attachment reference,
332         * since they both need to be done separately now that we aren't using anonymous keys.
333         * 
334         * @param attachment
335         */
336        public void addAttachment(Attachment attachment) {
337            setAttachmentIdentifier(attachment.getAttachmentIdentifier());
338            setAttachment(attachment);
339    
340            // copy foreign key and redundant values into attachment
341            attachment.setNoteIdentifier(noteIdentifier);
342            // we'll need this note reference if the attachment is deleted
343            // before the note is saved
344            attachment.setNote(this);
345        }
346    
347        /**
348         * Removes the current attachment, if any. More specifically, clears both the attachmentIdentifier and the attachment reference,
349         * since they both need to be done separately now that we aren't using anonymous keys.
350         */
351        public void removeAttachment() {
352            setAttachment(null);
353            setAttachmentIdentifier(null);
354        }
355    
356        /**
357         * @return the adHocRouteRecipient
358         */
359        public AdHocRouteRecipient getAdHocRouteRecipient() {
360            return adHocRouteRecipient;
361        }
362    
363        /**
364         * @param adHocRouteRecipient the adHocRouteRecipient to set
365         */
366        public void setAdHocRouteRecipient(AdHocRouteRecipient adHocRouteRecipient) {
367            this.adHocRouteRecipient = adHocRouteRecipient;
368        }
369        /**
370         * @return the attachmentLink
371         */
372        public String getAttachmentLink() {
373            //getAttachment() is always return null.
374            if(KRADServiceLocator.getAttachmentService().getAttachmentByNoteId(this.getNoteIdentifier()) == null){
375                return "";
376            }else{
377                Properties params = new Properties();
378                params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.DOWNLOAD_BO_ATTACHMENT_METHOD);
379                params.put(KRADConstants.DOC_FORM_KEY, "88888888");
380                params.put(KRADConstants.NOTE_IDENTIFIER, this.getNoteIdentifier().toString());
381                return UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params);
382            }
383        }
384    }
385    
386