View Javadoc
1   /**
2    * Copyright 2005-2016 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.kuali.rice.ken.api.notification.NotificationChannel;
20  import org.kuali.rice.ken.api.notification.NotificationChannelContract;
21  import org.kuali.rice.ken.api.notification.NotificationChannelReviewer;
22  import org.kuali.rice.ken.api.notification.NotificationListRecipient;
23  import org.kuali.rice.ken.api.notification.NotificationProducer;
24  import org.kuali.rice.ken.api.notification.UserChannelSubscription;
25  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
26  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
27  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
28  
29  import javax.persistence.CascadeType;
30  import javax.persistence.Column;
31  import javax.persistence.Convert;
32  import javax.persistence.Entity;
33  import javax.persistence.FetchType;
34  import javax.persistence.GeneratedValue;
35  import javax.persistence.Id;
36  import javax.persistence.JoinColumn;
37  import javax.persistence.JoinTable;
38  import javax.persistence.ManyToMany;
39  import javax.persistence.OneToMany;
40  import javax.persistence.OrderBy;
41  import javax.persistence.Table;
42  import java.util.ArrayList;
43  import java.util.List;
44  
45  /**
46   * This class represents and instance of a Notification Channel. A Notification Channel is correlated to a specific class of
47   * notification, or in other words a specific business purpose. For instance, all overdue books from a specific library could
48   * be a channel or a channel for concerts coming to campus could be another channel.
49   * 
50   * @author Kuali Rice Team (rice.collab@kuali.org)
51   */
52  @Entity
53  @Table(name = "KREN_CHNL_T")
54  public class NotificationChannelBo extends PersistableBusinessObjectBase implements NotificationChannelContract {
55  	@Id
56  	@GeneratedValue(generator="KREN_CHNL_S")
57      @PortableSequenceGenerator(name="KREN_CHNL_S")
58  	@Column(name = "CHNL_ID")
59  	private Long id;
60  	@Column(name = "NM", nullable = false)
61  	private String name;
62  	@Column(name = "DESC_TXT", nullable = false)
63  	private String description;
64      @Convert(converter = BooleanYNConverter.class)
65      @Column(name = "SUBSCRB_IND", nullable = false)
66      private boolean subscribable;
67  
68  	// List references
69  	@OneToMany(cascade={CascadeType.REFRESH, CascadeType.DETACH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}, 
70  			targetEntity=NotificationRecipientListBo.class, mappedBy="channel")
71  	@OrderBy ("id ASC")
72  	private List<NotificationRecipientListBo> recipientLists;
73  	
74  	@ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})@JoinTable(name="KREN_CHNL_PRODCR_T", 
75  			joinColumns=@JoinColumn(name="CHNL_ID"), 
76  			inverseJoinColumns=@JoinColumn(name="PRODCR_ID"))
77  	@OrderBy ("id ASC")
78  	private List<NotificationProducerBo> producers;
79  	
80  	@OneToMany(cascade={CascadeType.REFRESH, CascadeType.DETACH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}, 
81  			targetEntity=NotificationChannelReviewerBo.class, mappedBy="channel")
82  	@OrderBy ("id ASC")
83  	private List<NotificationChannelReviewerBo> reviewers = new ArrayList<NotificationChannelReviewerBo>();
84  	
85  	@OneToMany(cascade={CascadeType.REFRESH, CascadeType.DETACH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}, 
86  			targetEntity=UserChannelSubscriptionBo.class, mappedBy="channel")
87  	@OrderBy ("id ASC")
88  	private List<UserChannelSubscriptionBo> subscriptions = new ArrayList<UserChannelSubscriptionBo>();
89  
90  
91  	/**
92  	 * Constructs a NotificationChannel instance.
93  	 */
94  	public NotificationChannelBo() {
95  		super();
96  		recipientLists = new ArrayList<NotificationRecipientListBo>();
97  		producers = new ArrayList<NotificationProducerBo>();
98  	}
99  
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 }