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