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