View Javadoc

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