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