View Javadoc
1   /**
2    * Copyright 2005-2015 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.core.api.resourceloader.GlobalResourceLoader;
22  import org.kuali.rice.ken.api.notification.NotificationProducer;
23  import org.kuali.rice.ken.api.notification.NotificationProducerContract;
24  import org.kuali.rice.ken.service.NotificationChannelService;
25  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
26  
27  import javax.persistence.CascadeType;
28  import javax.persistence.Column;
29  import javax.persistence.Entity;
30  import javax.persistence.FetchType;
31  import javax.persistence.GeneratedValue;
32  import javax.persistence.Id;
33  import javax.persistence.JoinColumn;
34  import javax.persistence.JoinTable;
35  import javax.persistence.ManyToMany;
36  import javax.persistence.OrderBy;
37  import javax.persistence.Table;
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  /**
42   * This class represents an instance of who can actually submit notification messages to the system 
43   * for processing.
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  @Entity
47  @Table(name="KREN_PRODCR_T")
48  public class NotificationProducerBo extends PersistableBusinessObjectBase implements NotificationProducerContract {
49      @Id
50      @GeneratedValue(generator="KREN_PRODCR_S")
51  	@GenericGenerator(name="KREN_PRODCR_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
52  			@Parameter(name="sequence_name",value="KREN_PRODCR_S"),
53  			@Parameter(name="value_column",value="id")
54  	})
55  	@Column(name="PRODCR_ID")
56  	private Long id;
57      @Column(name="NM", nullable=false)
58  	private String name;
59      @Column(name="DESC_TXT", nullable=false)
60  	private String description;
61      @Column(name="CNTCT_INFO", nullable=false)
62  	private String contactInfo;
63      
64      // List references
65      @ManyToMany(fetch=FetchType.EAGER, cascade={CascadeType.ALL})@JoinTable(name="KREN_CHNL_PRODCR_T", 
66  	           joinColumns=@JoinColumn(name="PRODCR_ID"), 
67  	           inverseJoinColumns=@JoinColumn(name="CHNL_ID"))
68  	@OrderBy("id ASC")
69  	private List<NotificationChannelBo> channels;
70      
71      /**
72       * Constructs a NotificationProducer instance.
73       */
74      public NotificationProducerBo() {
75  	channels = new ArrayList<NotificationChannelBo>();
76      }
77  
78      /**
79       * Gets the contactInfo attribute. 
80       * @return Returns the contactInfo.
81       */
82      public String getContactInfo() {
83  	return contactInfo;
84      }
85  
86      @Override
87      public List<Long> getChannelIds() {
88          List<Long> ids = new ArrayList<Long>();
89          for (NotificationChannelBo bo : this.getChannels()) {
90              ids.add(bo.getId());
91          }
92          return ids;
93      }
94  
95      /**
96       * Sets the contactInfo attribute value.
97       * @param contactInfo The contactInfo to set.
98       */
99      public void setContactInfo(String contactInfo) {
100 	this.contactInfo = contactInfo;
101     }
102 
103     /**
104      * Gets the description attribute. 
105      * @return Returns the description.
106      */
107     public String getDescription() {
108 	return description;
109     }
110 
111     /**
112      * Sets the description attribute value.
113      * @param description The description to set.
114      */
115     public void setDescription(String description) {
116 	this.description = description;
117     }
118 
119     /**
120      * Gets the id attribute. 
121      * @return Returns the id.
122      */
123     public Long getId() {
124 	return id;
125     }
126 
127     /**
128      * Sets the id attribute value.
129      * @param id The id to set.
130      */
131     public void setId(Long id) {
132 	this.id = id;
133     }
134 
135     /**
136      * Gets the name attribute. 
137      * @return Returns the name.
138      */
139     public String getName() {
140 	return name;
141     }
142 
143     /**
144      * Sets the name attribute value.
145      * @param name The name to set.
146      */
147     public void setName(String name) {
148 	this.name = name;
149     }
150 
151     /**
152      * Gets the channels attribute. 
153      * @return Returns the channels.
154      */
155     public List<NotificationChannelBo> getChannels() {
156         return channels;
157     }
158 
159     /**
160      * Sets the channels attribute value.
161      * @param channels The channels to set.
162      */
163     public void setChannels(List<NotificationChannelBo> channels) {
164         this.channels = channels;
165     }
166 
167     /**
168      * Converts a mutable bo to its immutable counterpart
169      * @param bo the mutable business object
170      * @return the immutable object
171      */
172     public static NotificationProducer to(NotificationProducerBo bo) {
173         if (bo == null) {
174             return null;
175         }
176 
177         return NotificationProducer.Builder.create(bo).build();
178     }
179 
180 
181     /**
182      * Converts a immutable object to its mutable counterpart
183      * @param im immutable object
184      * @return the mutable bo
185      */
186     public static NotificationProducerBo from(NotificationProducer im) {
187         if (im == null) {
188             return null;
189         }
190 
191         NotificationProducerBo bo = new NotificationProducerBo();
192         bo.setId(im.getId());
193         bo.setVersionNumber(im.getVersionNumber());
194         bo.setObjectId(im.getObjectId());
195 
196         bo.setName(im.getName());
197         bo.setDescription(im.getDescription());
198         bo.setContactInfo(im.getContactInfo());
199 
200         List<NotificationChannelBo> tempChannels = new ArrayList<NotificationChannelBo>();
201         if (CollectionUtils.isNotEmpty(im.getChannelIds())) {
202             NotificationChannelService ncs = GlobalResourceLoader.getService("notificationChannelService");
203             for (Long channelId : im.getChannelIds()) {
204                 tempChannels.add(ncs.getNotificationChannel(channelId.toString()));
205             }
206             bo.setChannels(tempChannels);
207         }
208         return bo;
209     }
210 }