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