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.kuali.rice.ken.api.notification.UserChannelSubscription;
019import org.kuali.rice.ken.api.notification.UserChannelSubscriptionContract;
020import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
021import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
022
023import javax.persistence.CascadeType;
024import javax.persistence.Column;
025import javax.persistence.Entity;
026import javax.persistence.FetchType;
027import javax.persistence.GeneratedValue;
028import javax.persistence.Id;
029import javax.persistence.JoinColumn;
030import javax.persistence.OneToOne;
031import javax.persistence.Table;
032
033/**
034 * This class represents an instance of a user's subscription to a specific 
035 * notification channel.
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038@Entity
039@Table(name="KREN_CHNL_SUBSCRP_T")
040public class UserChannelSubscriptionBo extends PersistableBusinessObjectBase implements UserChannelSubscriptionContract {
041    @Id
042    @GeneratedValue(generator="KREN_CHNL_SUBSCRP_S")
043    @PortableSequenceGenerator(name="KREN_CHNL_SUBSCRP_S")
044        @Column(name="CHNL_SUBSCRP_ID")
045        private Long id;
046    @Column(name="PRNCPL_ID", nullable=false)
047        private String userId;
048    
049    @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.REFRESH, CascadeType.MERGE})
050        @JoinColumn(name="CHNL_ID")
051        private NotificationChannelBo channel;
052    
053    /**
054     * Constructs a UserChannelSubscription instance.
055     */
056    public UserChannelSubscriptionBo() {
057    }
058
059    /**
060     * Gets the channel attribute. 
061     * @return Returns the channel.
062     */
063    public NotificationChannelBo getChannel() {
064            return channel;
065    }
066
067    /**
068     * Sets the channel attribute value.
069     * @param channel The channel to set.
070     */
071    public void setChannel(NotificationChannelBo channel) {
072            this.channel = channel;
073    }
074
075    /**
076     * Gets the id attribute. 
077     * @return Returns the id.
078     */
079    public Long getId() {
080            return id;
081    }
082
083    /**
084     * Sets the id attribute value.
085     * @param id The id to set.
086     */
087    public void setId(Long id) {
088            this.id = id;
089    }
090
091    /**
092     * Gets the userId attribute. 
093     * @return Returns the userId.
094     */
095    public String getUserId() {
096            return userId;
097    }
098
099    /**
100     * Sets the userId attribute value.
101     * @param userId The userId to set.
102     */
103    public void setUserId(String userId) {
104            this.userId = userId;
105    }
106
107    /**
108     * Converts a mutable bo to its immutable counterpart
109     * @param bo the mutable business object
110     * @return the immutable object
111     */
112    public static UserChannelSubscription to(UserChannelSubscriptionBo bo) {
113        if (bo == null) {
114            return null;
115        }
116
117        return UserChannelSubscription.Builder.create(bo).build();
118    }
119
120    /**
121     * Converts a immutable object to its mutable counterpart
122     * @param im immutable object
123     * @return the mutable bo
124     */
125    public static UserChannelSubscriptionBo from(UserChannelSubscription im) {
126        if (im == null) {
127            return null;
128        }
129
130        UserChannelSubscriptionBo bo = new UserChannelSubscriptionBo();
131        bo.setId(im.getId());
132        bo.setVersionNumber(im.getVersionNumber());
133        bo.setObjectId(im.getObjectId());
134
135        bo.setUserId(im.getUserId());
136        bo.setChannel(im.getChannel() == null ? null : NotificationChannelBo.from(im.getChannel()));
137        
138        return bo;
139    }
140}
141