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.ken.bo;
017
018import org.apache.commons.collections.CollectionUtils;
019import org.apache.commons.lang.StringUtils;
020import org.hibernate.annotations.GenericGenerator;
021import org.hibernate.annotations.Parameter;
022import org.joda.time.DateTime;
023import org.kuali.rice.ken.api.notification.Notification;
024import org.kuali.rice.ken.api.notification.NotificationContract;
025import org.kuali.rice.ken.api.notification.NotificationRecipient;
026import org.kuali.rice.ken.api.notification.NotificationRecipientContract;
027import org.kuali.rice.ken.api.notification.NotificationSender;
028import org.kuali.rice.ken.api.notification.NotificationSenderContract;
029import org.kuali.rice.ken.util.NotificationConstants;
030import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
031
032import javax.persistence.Basic;
033import javax.persistence.CascadeType;
034import javax.persistence.Column;
035import javax.persistence.Entity;
036import javax.persistence.FetchType;
037import javax.persistence.GeneratedValue;
038import javax.persistence.Id;
039import javax.persistence.JoinColumn;
040import javax.persistence.Lob;
041import javax.persistence.OneToMany;
042import javax.persistence.OneToOne;
043import javax.persistence.OrderBy;
044import javax.persistence.Table;
045import java.sql.Timestamp;
046import java.util.ArrayList;
047import java.util.List;
048
049/**
050 * This class represents an instace of a notification message that is received by the overall 
051 * system.
052 * @author Kuali Rice Team (rice.collab@kuali.org)
053 */
054@Entity
055@Table(name="KREN_NTFCTN_T")
056public class NotificationBo extends PersistableBusinessObjectBase implements NotificationContract, Lockable {
057   
058    @Id
059    @GeneratedValue(generator="KREN_NTFCTN_S")
060        @GenericGenerator(name="KREN_NTFCTN_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
061                        @Parameter(name="sequence_name",value="KREN_NTFCTN_S"),
062                        @Parameter(name="value_column",value="id")
063        })
064        @Column(name="NTFCTN_ID")
065        private Long id;
066    @Column(name="DELIV_TYP", nullable=false)
067        private String deliveryType;
068        @Column(name="CRTE_DTTM", nullable=false)
069        private Timestamp creationDateTimeValue;
070        @Column(name="SND_DTTM", nullable=true)
071        private Timestamp sendDateTimeValue;
072        @Column(name="AUTO_RMV_DTTM", nullable=true)
073        private Timestamp autoRemoveDateTimeValue;
074    @Column(name="TTL", nullable=true)
075        private String title;
076    @Lob
077        @Basic(fetch=FetchType.LAZY)
078        @Column(name="CNTNT", nullable=false)
079        private String content;
080    @Column(name="PROCESSING_FLAG", nullable=false)
081        private String processingFlag;
082        @Column(name="LOCKD_DTTM", nullable=true)
083        private Timestamp lockedDateValue;
084    /**
085     * Lock column for OJB optimistic locking
086     */
087//    @Version
088//      @Column(name="VER_NBR")
089//      private Integer lockVerNbr;
090    
091    // object references
092    @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.REFRESH, CascadeType.DETACH})
093        @JoinColumn(name="PRIO_ID")
094        private NotificationPriorityBo priority;
095    @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.REFRESH, CascadeType.DETACH})
096        @JoinColumn(name="CNTNT_TYP_ID")
097        private NotificationContentTypeBo contentType;
098    @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.REFRESH, CascadeType.DETACH})
099        @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}