001/**
002 * Copyright 2005-2016 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.hibernate.annotations.GenericGenerator;
020import org.hibernate.annotations.Parameter;
021import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
022import org.kuali.rice.ken.api.notification.NotificationProducer;
023import org.kuali.rice.ken.api.notification.NotificationProducerContract;
024import org.kuali.rice.ken.service.NotificationChannelService;
025import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
026
027import javax.persistence.CascadeType;
028import javax.persistence.Column;
029import javax.persistence.Entity;
030import javax.persistence.FetchType;
031import javax.persistence.GeneratedValue;
032import javax.persistence.Id;
033import javax.persistence.JoinColumn;
034import javax.persistence.JoinTable;
035import javax.persistence.ManyToMany;
036import javax.persistence.OrderBy;
037import javax.persistence.Table;
038import java.util.ArrayList;
039import java.util.List;
040
041/**
042 * This class represents an instance of who can actually submit notification messages to the system 
043 * for processing.
044 * @author Kuali Rice Team (rice.collab@kuali.org)
045 */
046@Entity
047@Table(name="KREN_PRODCR_T")
048public class NotificationProducerBo extends PersistableBusinessObjectBase implements NotificationProducerContract {
049    @Id
050    @GeneratedValue(generator="KREN_PRODCR_S")
051        @GenericGenerator(name="KREN_PRODCR_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
052                        @Parameter(name="sequence_name",value="KREN_PRODCR_S"),
053                        @Parameter(name="value_column",value="id")
054        })
055        @Column(name="PRODCR_ID")
056        private Long id;
057    @Column(name="NM", nullable=false)
058        private String name;
059    @Column(name="DESC_TXT", nullable=false)
060        private String description;
061    @Column(name="CNTCT_INFO", nullable=false)
062        private String contactInfo;
063    
064    // List references
065    @ManyToMany(fetch=FetchType.EAGER, cascade={CascadeType.ALL})@JoinTable(name="KREN_CHNL_PRODCR_T", 
066                   joinColumns=@JoinColumn(name="PRODCR_ID"), 
067                   inverseJoinColumns=@JoinColumn(name="CHNL_ID"))
068        @OrderBy("id ASC")
069        private List<NotificationChannelBo> channels;
070    
071    /**
072     * Constructs a NotificationProducer instance.
073     */
074    public NotificationProducerBo() {
075        channels = new ArrayList<NotificationChannelBo>();
076    }
077
078    /**
079     * Gets the contactInfo attribute. 
080     * @return Returns the contactInfo.
081     */
082    public String getContactInfo() {
083        return contactInfo;
084    }
085
086    @Override
087    public List<Long> getChannelIds() {
088        List<Long> ids = new ArrayList<Long>();
089        for (NotificationChannelBo bo : this.getChannels()) {
090            ids.add(bo.getId());
091        }
092        return ids;
093    }
094
095    /**
096     * Sets the contactInfo attribute value.
097     * @param contactInfo The contactInfo to set.
098     */
099    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}