View Javadoc

1   /**
2    * Copyright 2005-2012 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.ken.bo;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.hibernate.annotations.GenericGenerator;
21  import org.hibernate.annotations.Parameter;
22  import org.joda.time.DateTime;
23  import org.kuali.rice.ken.api.notification.Notification;
24  import org.kuali.rice.ken.api.notification.NotificationContract;
25  import org.kuali.rice.ken.api.notification.NotificationRecipient;
26  import org.kuali.rice.ken.api.notification.NotificationRecipientContract;
27  import org.kuali.rice.ken.api.notification.NotificationSender;
28  import org.kuali.rice.ken.api.notification.NotificationSenderContract;
29  import org.kuali.rice.ken.util.NotificationConstants;
30  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
31  
32  import javax.persistence.Basic;
33  import javax.persistence.CascadeType;
34  import javax.persistence.Column;
35  import javax.persistence.Entity;
36  import javax.persistence.FetchType;
37  import javax.persistence.GeneratedValue;
38  import javax.persistence.Id;
39  import javax.persistence.JoinColumn;
40  import javax.persistence.Lob;
41  import javax.persistence.OneToMany;
42  import javax.persistence.OneToOne;
43  import javax.persistence.OrderBy;
44  import javax.persistence.Table;
45  import java.sql.Timestamp;
46  import java.util.ArrayList;
47  import java.util.List;
48  
49  /**
50   * This class represents an instace of a notification message that is received by the overall 
51   * system.
52   * @author Kuali Rice Team (rice.collab@kuali.org)
53   */
54  @Entity
55  @Table(name="KREN_NTFCTN_T")
56  public class NotificationBo extends PersistableBusinessObjectBase implements NotificationContract, Lockable {
57     
58      @Id
59      @GeneratedValue(generator="KREN_NTFCTN_S")
60  	@GenericGenerator(name="KREN_NTFCTN_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
61  			@Parameter(name="sequence_name",value="KREN_NTFCTN_S"),
62  			@Parameter(name="value_column",value="id")
63  	})
64  	@Column(name="NTFCTN_ID")
65  	private Long id;
66      @Column(name="DELIV_TYP", nullable=false)
67  	private String deliveryType;
68  	@Column(name="CRTE_DTTM", nullable=false)
69  	private Timestamp creationDateTimeValue;
70  	@Column(name="SND_DTTM", nullable=true)
71  	private Timestamp sendDateTimeValue;
72  	@Column(name="AUTO_RMV_DTTM", nullable=true)
73  	private Timestamp autoRemoveDateTimeValue;
74      @Column(name="TTL", nullable=true)
75  	private String title;
76      @Lob
77  	@Basic(fetch=FetchType.LAZY)
78  	@Column(name="CNTNT", nullable=false)
79  	private String content;
80      @Column(name="PROCESSING_FLAG", nullable=false)
81  	private String processingFlag;
82  	@Column(name="LOCKD_DTTM", nullable=true)
83  	private Timestamp lockedDateValue;
84      /**
85       * Lock column for OJB optimistic locking
86       */
87  //    @Version
88  //	@Column(name="VER_NBR")
89  //	private Integer lockVerNbr;
90      
91      // object references
92      @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.REFRESH, CascadeType.DETACH})
93  	@JoinColumn(name="PRIO_ID")
94  	private NotificationPriorityBo priority;
95      @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.REFRESH, CascadeType.DETACH})
96  	@JoinColumn(name="CNTNT_TYP_ID")
97  	private NotificationContentTypeBo contentType;
98      @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.REFRESH, CascadeType.DETACH})
99  	@JoinColumn(name="CHNL_ID")
100 	private NotificationChannelBo channel;
101     @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.REFRESH, CascadeType.DETACH})
102 	@JoinColumn(name="PRODCR_ID")
103 	private NotificationProducerBo producer;
104     
105     // lists
106     @OneToMany(cascade={CascadeType.ALL},
107            targetEntity=NotificationRecipientBo.class, mappedBy="notification")
108     @OrderBy("id ASC")
109 	private List<NotificationRecipientBo> recipients;
110     @OneToMany(cascade={CascadeType.ALL},
111            targetEntity=NotificationSenderBo.class, mappedBy="notification")
112 	@OrderBy("id ASC")
113     private List<NotificationSenderBo> senders;
114     
115     /**
116      * Constructs a Notification instance.
117      */
118     public NotificationBo() {
119         recipients = new ArrayList<NotificationRecipientBo>();
120         senders = new ArrayList<NotificationSenderBo>();
121         processingFlag = NotificationConstants.PROCESSING_FLAGS.UNRESOLVED;
122     }
123 
124     /**
125      * Returns when this Notification entry was created 
126      * @return when this Notification entry was created
127      */
128     public Timestamp getCreationDateTimeValue() {
129         return creationDateTimeValue;
130     }
131 
132     @Override
133     public DateTime getCreationDateTime() {
134         return this.creationDateTimeValue == null ? null : new DateTime(this.creationDateTimeValue);
135     }
136 
137     /**
138      * Sets the creation date of this Notification entry
139      * @param created the creation date of this Notification entry
140      */
141     public void setCreationDateTimeValue(Timestamp created) {
142         this.creationDateTimeValue = created;
143     }
144 
145     /**
146      * Return value of lock column for OJB optimistic locking
147      * @return value of lock column for OJB optimistic locking
148      */
149  // should discard this method and call super directly
150     public Integer getLockVerNbr() {
151     	return Integer.valueOf(super.getVersionNumber().intValue());
152     }
153 
154     /**
155      * Set value of lock column for OJB optimistic locking
156      * @param lockVerNbr value of lock column for OJB optimistic locking
157      */
158  // should discard this method and call super directly
159     public void setLockVerNbr(Integer lockVerNbr) {
160     	super.setVersionNumber(lockVerNbr.longValue());
161     }
162 
163     /**
164      * Gets the recipients attribute. 
165      * @return Returns the recipients.
166      */
167     public List<NotificationRecipientBo> getRecipients() {
168         return recipients;
169     }
170 
171     /**
172      * Sets the recipients attribute value.
173      * @param recipients The recipients to set.
174      */
175     public void setRecipients(List<NotificationRecipientBo> recipients) {
176         this.recipients = recipients;
177     }
178 
179     /**
180      * Retrieves a recipient at the specified index
181      * @param index the index in the recipients collection
182      * @return the recipient if found or null
183      */
184     public NotificationRecipientBo getRecipient(int index) {
185         return (NotificationRecipientBo) recipients.get(index);
186     }
187     
188     /**
189      * Adds a recipient
190      * @param recipient The recipient to add
191      */
192     public void addRecipient(NotificationRecipientBo recipient) {
193         recipients.add(recipient);
194     }
195 
196     /**
197      * Gets the senders attribute. 
198      * @return Returns the senders.
199      */
200     public List<NotificationSenderBo> getSenders() {
201         return senders;
202     }
203 
204     /**
205      * Sets the senders attribute value.
206      * @param senders The senders to set.
207      */
208     public void setSenders(List<NotificationSenderBo> senders) {
209         this.senders = senders;
210     }
211 
212     /**
213      * Retrieves a sender at the specified index
214      * @param index the index in the senders collection
215      * @return the sender if found or null
216      */
217     public NotificationSenderBo getSender(int index) {
218         return (NotificationSenderBo) senders.get(index);
219     }
220     /**
221      * Adds a sender
222      * @param sender The sender to add
223      */
224     public void addSender(NotificationSenderBo sender) {
225         senders.add(sender);
226     }
227 
228     /**
229      * Gets the autoRemoveDateTime attribute. 
230      * @return Returns the autoRemoveDateTime.
231      */
232     public Timestamp getAutoRemoveDateTimeValue() {
233 	    return this.autoRemoveDateTimeValue;
234     }
235 
236     @Override
237     public DateTime getAutoRemoveDateTime() {
238         return this.autoRemoveDateTimeValue == null ? null : new DateTime(this.autoRemoveDateTimeValue);
239     }
240 
241     /**
242      * Sets the autoRemoveDateTime attribute value.
243      * @param autoRemoveDateTimeValue The autoRemoveDateTime to set.
244      */
245     public void setAutoRemoveDateTimeValue(Timestamp autoRemoveDateTimeValue) {
246 	    this.autoRemoveDateTimeValue = autoRemoveDateTimeValue;
247     }
248 
249     /**
250      * Gets the channel attribute. 
251      * @return Returns the channel.
252      */
253     public NotificationChannelBo getChannel() {
254 	    return channel;
255     }
256 
257     /**
258      * Sets the channel attribute value.
259      * @param channel The channel to set.
260      */
261     public void setChannel(NotificationChannelBo channel) {
262 	    this.channel = channel;
263     }
264 
265     /**
266      * Gets the content attribute. 
267      * @return Returns the content.
268      */
269     public String getContent() {
270 	    return content;
271     }
272 
273     /**
274      * Sets the content attribute value.
275      * @param content The content to set.
276      */
277     public void setContent(String content) {
278 	    this.content = content;
279     }
280 
281     /**
282      * Gets the contentType attribute. 
283      * @return Returns the contentType.
284      */
285     public NotificationContentTypeBo getContentType() {
286 	    return contentType;
287     }
288 
289     /**
290      * Sets the contentType attribute value.
291      * @param contentType The contentType to set.
292      */
293     public void setContentType(NotificationContentTypeBo contentType) {
294 	    this.contentType = contentType;
295     }
296 
297     /**
298      * Gets the deliveryType attribute. 
299      * @return Returns the deliveryType.
300      */
301     public String getDeliveryType() {
302 	    return deliveryType;
303     }
304 
305     /**
306      * Sets the deliveryType attribute value.
307      * @param deliveryType The deliveryType to set.
308      */
309     public void setDeliveryType(String deliveryType) {
310 	    this.deliveryType = deliveryType.toUpperCase();
311     }
312 
313     /**
314      * Gets the id attribute. 
315      * @return Returns the id.
316      */
317     public Long getId() {
318 	    return id;
319     }
320 
321     /**
322      * Sets the id attribute value.
323      * @param id The id to set.
324      */
325     public void setId(Long id) {
326 	    this.id = id;
327     }
328 
329     /**
330      * Gets the priority attribute. 
331      * @return Returns the priority.
332      */
333     public NotificationPriorityBo getPriority() {
334 	    return priority;
335     }
336 
337     /**
338      * Sets the priority attribute value.
339      * @param priority The priority to set.
340      */
341     public void setPriority(NotificationPriorityBo priority) {
342 	    this.priority = priority;
343     }
344 
345     /**
346      * Gets the producer attribute. 
347      * @return Returns the producer.
348      */
349     public NotificationProducerBo getProducer() {
350 	    return producer;
351     }
352 
353     /**
354      * Sets the producer attribute value.
355      * @param producer The producer to set.
356      */
357     public void setProducer(NotificationProducerBo producer) {
358 	    this.producer = producer;
359     }
360 
361     /**
362      * Gets the sendDateTime attribute. 
363      * @return Returns the sendDateTime.
364      */
365     public Timestamp getSendDateTimeValue() {
366 	    return this.sendDateTimeValue;
367     }
368 
369     @Override
370     public DateTime getSendDateTime() {
371         return this.sendDateTimeValue == null ? null : new DateTime(this.sendDateTimeValue);
372     }
373 
374     /**
375      * Sets the sendDateTime attribute value.
376      * @param sendDateTimeValue The sendDateTime to set.
377      */
378     public void setSendDateTimeValue(Timestamp sendDateTimeValue) {
379 	    this.sendDateTimeValue = sendDateTimeValue;
380     }
381 
382     /**
383      * Gets the processingFlag attribute. 
384      * @return Returns the processingFlag.
385      */
386     public String getProcessingFlag() {
387         return processingFlag;
388     }
389 
390     /**
391      * Sets the processingFlag attribute value.
392      * @param processingFlag The processingFlag to set.
393      */
394     public void setProcessingFlag(String processingFlag) {
395         this.processingFlag = processingFlag;
396     }
397     
398     /**
399      * Gets the lockedDate attribute. 
400      * @return Returns the lockedDate.
401      */
402     public Timestamp getLockedDateValue() {
403         return this.lockedDateValue;
404     }
405 
406     @Override
407     public DateTime getLockedDate() {
408         return this.lockedDateValue == null ? null : new DateTime(this.lockedDateValue);
409     }
410     
411     /**
412      * Sets the lockedDate attribute value.
413      * @param lockedDateValue The lockedDate to set.
414      */
415     public void setLockedDateValue(Timestamp lockedDateValue) {
416         this.lockedDateValue = lockedDateValue;
417     }
418 
419     /**
420      * Gets the title
421      * @return the title of this notification
422      */
423     public String getTitle() {
424         return title;
425     }
426 
427     /**
428      * Sets the title
429      * @param title the title of this notification
430      */
431     public void setTitle(String title) {
432         this.title = title;
433     }
434 
435     /**
436      * This method just uses StringUtils to get at the content of the <message> tag 
437      * that exists in the notification content.
438      * @return String
439      */
440     public String getContentMessage() {
441 	    return StringUtils.substringBetween(content, NotificationConstants.XML_MESSAGE_CONSTANTS.MESSAGE_OPEN, NotificationConstants.XML_MESSAGE_CONSTANTS.MESSAGE_CLOSE);	
442     }
443 
444 
445     /**
446      * Converts a mutable bo to its immutable counterpart
447      * @param bo the mutable business object
448      * @return the immutable object
449      */
450     public static Notification to(NotificationBo bo) {
451         if (bo == null) {
452             return null;
453         }
454 
455         return Notification.Builder.create(bo).build();
456     }
457 
458     /**
459      * Converts a immutable object to its mutable counterpart
460      * @param im immutable object
461      * @return the mutable bo
462      */
463     public static NotificationBo from(Notification im) {
464         if (im == null) {
465             return null;
466         }
467 
468         NotificationBo bo = new NotificationBo();
469         bo.setId(im.getId());
470         bo.setVersionNumber(im.getVersionNumber());
471         bo.setObjectId(im.getObjectId());
472         bo.setDeliveryType(im.getDeliveryType());
473         bo.setCreationDateTimeValue(im.getCreationDateTime() == null ? null : new Timestamp(im.getCreationDateTime().getMillis()));
474         bo.setSendDateTimeValue(im.getSendDateTime() == null ? null : new Timestamp(im.getSendDateTime().getMillis()));
475         bo.setAutoRemoveDateTimeValue(im.getAutoRemoveDateTime() == null ? null : new Timestamp(im.getAutoRemoveDateTime().getMillis()));
476         bo.setTitle(im.getTitle());
477         bo.setContent(im.getContent());
478         bo.setLockedDateValue(im.getLockedDate() == null ? null : new Timestamp(im.getLockedDate().getMillis()));
479 
480         // object references
481         bo.setPriority(NotificationPriorityBo.from(im.getPriority()));
482         bo.setContentType(NotificationContentTypeBo.from(im.getContentType()));
483         bo.setChannel(NotificationChannelBo.from(im.getChannel()));
484         bo.setProducer(NotificationProducerBo.from(im.getProducer()));
485 
486         // lists
487         List<NotificationRecipientBo> tempRecipients = new ArrayList<NotificationRecipientBo>();
488         if (CollectionUtils.isNotEmpty(im.getRecipients())) {
489             for (NotificationRecipient recipient : im.getRecipients()) {
490                 tempRecipients.add(NotificationRecipientBo.from(recipient));
491             }
492             bo.setRecipients(tempRecipients);
493         }
494         List<NotificationSenderBo> tempSenders = new ArrayList<NotificationSenderBo>();
495         if (CollectionUtils.isNotEmpty(im.getSenders())) {
496             for (NotificationSender sender : im.getSenders()) {
497                 tempSenders.add(NotificationSenderBo.from(sender));
498             }
499             bo.setSenders(tempSenders);
500         }
501 
502         return bo;
503     }
504 }