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.hibernate.annotations.GenericGenerator;
020import org.hibernate.annotations.Parameter;
021import org.kuali.rice.ken.api.notification.NotificationChannel;
022import org.kuali.rice.ken.api.notification.NotificationChannelContract;
023import org.kuali.rice.ken.api.notification.NotificationChannelReviewer;
024import org.kuali.rice.ken.api.notification.NotificationChannelReviewerContract;
025import org.kuali.rice.ken.api.notification.NotificationContentType;
026import org.kuali.rice.ken.api.notification.NotificationListRecipient;
027import org.kuali.rice.ken.api.notification.NotificationListRecipientContract;
028import org.kuali.rice.ken.api.notification.NotificationProducer;
029import org.kuali.rice.ken.api.notification.NotificationProducerContract;
030import org.kuali.rice.ken.api.notification.UserChannelSubscription;
031import org.kuali.rice.ken.api.notification.UserChannelSubscriptionContract;
032import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
033
034import javax.persistence.*;
035import java.util.ArrayList;
036import java.util.List;
037
038/**
039 * This class represents and instance of a Notification Channel. A Notification Channel is correlated to a specific class of
040 * notification, or in other words a specific business purpose. For instance, all overdue books from a specific library could
041 * be a channel or a channel for concerts coming to campus could be another channel.
042 * 
043 * @author Kuali Rice Team (rice.collab@kuali.org)
044 */
045@Entity
046@Table(name = "KREN_CHNL_T")
047public class NotificationChannelBo extends PersistableBusinessObjectBase implements NotificationChannelContract {
048        @Id
049        @GeneratedValue(generator="KREN_CHNL_S")
050        @GenericGenerator(name="KREN_CHNL_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
051                        @Parameter(name="sequence_name",value="KREN_CHNL_S"),
052                        @Parameter(name="value_column",value="id")
053        })
054        @Column(name = "CHNL_ID")
055        private Long id;
056        @Column(name = "NM", nullable = false)
057        private String name;
058        @Column(name = "DESC_TXT", nullable = false)
059        private String description;
060        @Column(name = "SUBSCRB_IND", nullable = false)
061        private boolean subscribable;
062
063        // List references
064        @OneToMany(cascade={CascadeType.REFRESH, CascadeType.DETACH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}, 
065                        targetEntity=NotificationRecipientListBo.class, mappedBy="channel")
066        @OrderBy ("id ASC")
067        private List<NotificationRecipientListBo> recipientLists;
068        
069        @ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})@JoinTable(name="KREN_CHNL_PRODCR_T", 
070                        joinColumns=@JoinColumn(name="CHNL_ID"), 
071                        inverseJoinColumns=@JoinColumn(name="PRODCR_ID"))
072        @OrderBy ("id ASC")
073        private List<NotificationProducerBo> producers;
074        
075        @OneToMany(cascade={CascadeType.REFRESH, CascadeType.DETACH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}, 
076                        targetEntity=NotificationChannelReviewerBo.class, mappedBy="channel")
077        @OrderBy ("id ASC")
078        private List<NotificationChannelReviewerBo> reviewers = new ArrayList<NotificationChannelReviewerBo>();
079        
080        @OneToMany(cascade={CascadeType.REFRESH, CascadeType.DETACH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}, 
081                        targetEntity=UserChannelSubscriptionBo.class, mappedBy="channel")
082        @OrderBy ("id ASC")
083        private List<UserChannelSubscriptionBo> subscriptions = new ArrayList<UserChannelSubscriptionBo>();
084
085
086        /**
087         * Constructs a NotificationChannel instance.
088         */
089        public NotificationChannelBo() {
090                super();
091                recipientLists = new ArrayList<NotificationRecipientListBo>();
092                producers = new ArrayList<NotificationProducerBo>();
093        }
094
095        /**
096         * Gets the recipientLists attribute.
097         * 
098         * @return Returns the recipientLists.
099         */
100        public List<NotificationRecipientListBo> getRecipientLists() {
101                return recipientLists;
102        }
103
104        /**
105         * Sets the recipientLists attribute value.
106         * 
107         * @param recipientLists
108         *            The recipientLists to set.
109         */
110        public void setRecipientLists(List<NotificationRecipientListBo> recipientLists) {
111                this.recipientLists = recipientLists;
112        }
113
114        /**
115         * This method adds a recipient list to the overall set of recipient lists that are associated with this channnel.
116         * 
117         * @param recipientList
118         */
119        public void addRecipientList(NotificationRecipientListBo recipientList) {
120                this.recipientLists.add(recipientList);
121        }
122
123        /**
124         * This method removes a recipient list object from the overall list.
125         * 
126         * @param recipientList
127         */
128        public void removeRecipientList(NotificationRecipientListBo recipientList) {
129                this.recipientLists.remove(recipientList);
130        }
131
132        /**
133         * Gets the description attribute.
134         * 
135         * @return Returns the description.
136         */
137        public String getDescription() {
138                return description;
139        }
140
141        /**
142         * Sets the description attribute value.
143         * 
144         * @param description
145         *            The description to set.
146         */
147        public void setDescription(String description) {
148                this.description = description;
149        }
150
151        /**
152         * Gets the id attribute.
153         * 
154         * @return Returns the id.
155         */
156        public Long getId() {
157                return id;
158        }
159
160        /**
161         * Sets the id attribute value.
162         * 
163         * @param id
164         *            The id to set.
165         */
166        public void setId(Long id) {
167                this.id = id;
168        }
169
170        /**
171         * Gets the name attribute.
172         * 
173         * @return Returns the name.
174         */
175        public String getName() {
176                return name;
177        }
178
179        /**
180         * Sets the name attribute value.
181         * 
182         * @param name
183         *            The name to set.
184         */
185        public void setName(String name) {
186                this.name = name;
187        }
188
189        /**
190         * Gets the subscribable attribute.
191         * 
192         * @return Returns the subscribable.
193         */
194        public boolean isSubscribable() {
195                return subscribable;
196        }
197
198        /**
199         * Sets the subscribable attribute value.
200         * 
201         * @param subscribable
202         *            The subscribable to set.
203         */
204        public void setSubscribable(boolean subscribable) {
205                this.subscribable = subscribable;
206        }
207
208        /**
209         * Gets the producers attribute.
210         * 
211         * @return Returns the producers.
212         */
213        public List<NotificationProducerBo> getProducers() {
214                return producers;
215        }
216
217        /**
218         * Sets the producers attribute value.
219         * 
220         * @param producers
221         *            The producers to set.
222         */
223        public void setProducers(List<NotificationProducerBo> producers) {
224                this.producers = producers;
225        }
226
227        /**
228         * Gets the list of reviewers for notification publications to this channel
229         * 
230         * @return the list of reviewers for notification publications to this channel
231         */
232        public List<NotificationChannelReviewerBo> getReviewers() {
233                return reviewers;
234        }
235
236        /**
237         * Sets the list of reviewers for notification publications to this channel
238         * 
239         * @param reviewers
240         *            the list of reviewers for notification publications to this channel
241         */
242        public void setReviewers(List<NotificationChannelReviewerBo> reviewers) {
243                this.reviewers = reviewers;
244        }
245
246        /**
247         * Gets the list of subscriptions to this channel
248         * 
249         * @return the list of subscriptions to this channel
250         */
251        public List<UserChannelSubscriptionBo> getSubscriptions() {
252                return subscriptions;
253        }
254
255        /**
256         * Sets the list of subscriptions to this channel
257         * 
258         * @param subscriptions
259         *            the list of subscriptions to this channel
260         */
261        public void setSubscriptions(List<UserChannelSubscriptionBo> subscriptions) {
262                this.subscriptions = subscriptions;
263        }
264
265        /**
266         * Compares the id values of each NotificationChannel object.
267         * 
268         * @see java.lang.Object#equals(java.lang.Object)
269         */
270        @Override
271        public boolean equals(Object obj) {
272                NotificationChannelBo channelToCompare = (NotificationChannelBo) obj;
273                return this.getId().equals(channelToCompare.getId());
274        }
275
276    /**
277     * Converts a mutable bo to its immutable counterpart
278     * @param bo the mutable business object
279     * @return the immutable object
280     */
281    public static NotificationChannel to(NotificationChannelBo bo) {
282        if (bo == null) {
283            return null;
284        }
285
286        return NotificationChannel.Builder.create(bo).build();
287    }
288
289    /**
290     * Converts a immutable object to its mutable counterpart
291     * @param im immutable object
292     * @return the mutable bo
293     */
294    public static NotificationChannelBo from(NotificationChannel im) {
295        if (im == null) {
296            return null;
297        }
298
299        NotificationChannelBo bo = new NotificationChannelBo();
300        bo.setId(im.getId());
301        bo.setVersionNumber(im.getVersionNumber());
302        bo.setObjectId(im.getObjectId());
303        bo.setName(im.getName());
304        bo.setDescription(im.getDescription());
305
306        bo.setSubscribable(im.isSubscribable());
307
308        List<NotificationRecipientListBo> tempRecipientLists = new ArrayList<NotificationRecipientListBo>();
309        if (CollectionUtils.isNotEmpty(im.getRecipientLists())) {
310            for (NotificationListRecipient listRecipient : im.getRecipientLists()) {
311                tempRecipientLists.add(NotificationRecipientListBo.from(listRecipient));
312            }
313            bo.setRecipientLists(tempRecipientLists);
314        }
315
316        List<NotificationProducerBo> tempProducers = new ArrayList<NotificationProducerBo>();
317        if (CollectionUtils.isNotEmpty(im.getProducers())) {
318            for (NotificationProducer producer : im.getProducers()) {
319                tempProducers.add(NotificationProducerBo.from(producer));
320            }
321            bo.setProducers(tempProducers);
322        }
323
324        List<NotificationChannelReviewerBo> tempReviewers = new ArrayList<NotificationChannelReviewerBo>();
325        if (CollectionUtils.isNotEmpty(im.getReviewers())) {
326            for (NotificationChannelReviewer reviewer : im.getReviewers()) {
327                tempReviewers.add(NotificationChannelReviewerBo.from(reviewer));
328            }
329            bo.setReviewers(tempReviewers);
330        }
331
332        List<UserChannelSubscriptionBo> tempSubscriptions = new ArrayList<UserChannelSubscriptionBo>();
333        if (CollectionUtils.isNotEmpty(im.getSubscriptions())) {
334            for (UserChannelSubscription subscription : im.getSubscriptions()) {
335                tempSubscriptions.add(UserChannelSubscriptionBo.from(subscription));
336            }
337            bo.setSubscriptions(tempSubscriptions);
338        }
339
340        return bo;
341    }
342}