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.service.impl;
017
018import org.apache.log4j.Logger;
019import org.kuali.rice.core.framework.persistence.dao.GenericDao;
020import org.kuali.rice.ken.bo.UserChannelSubscriptionBo;
021import org.kuali.rice.ken.service.NotificationChannelService;
022import org.kuali.rice.ken.service.UserPreferenceService;
023import org.kuali.rice.ken.util.NotificationConstants;
024
025import java.util.Collection;
026import java.util.HashMap;
027
028/**
029 * UserPreferenceService implementation - uses the businessObjectDao to get at data in the underlying database.
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class UserPreferenceServiceImpl implements UserPreferenceService {
033    private GenericDao businessObjectDao;
034    private NotificationChannelService notificationChannelService;
035
036    private static final Logger LOG = Logger.getLogger(UserPreferenceServiceImpl.class);
037
038    /**
039     * Constructs a UserPreferenceServiceImpl 
040     * @param businessObjectDao
041     * @param notificationChannelService
042     */
043    public UserPreferenceServiceImpl(GenericDao businessObjectDao, NotificationChannelService notificationChannelService) {
044        this.businessObjectDao = businessObjectDao;
045        this.notificationChannelService = notificationChannelService;
046    }
047
048    /**
049     * @see org.kuali.rice.ken.service.UserPreferenceService#getCurrentSubscriptions(java.lang.String)
050     */
051    public Collection<UserChannelSubscriptionBo> getCurrentSubscriptions(String userid) {
052        UserChannelSubscriptionBo userChannelSubscription = new UserChannelSubscriptionBo();
053        userChannelSubscription.setUserId(userid);
054
055        return businessObjectDao.findMatchingByExample(userChannelSubscription);
056    }
057
058    /**
059     * @see org.kuali.rice.ken.service.UserPreferenceService#getSubscription(java.lang.String, java.lang.String)
060     */
061    public UserChannelSubscriptionBo getSubscription(String channelid, String userid) {
062        HashMap<String, String> uniqueKeys = new HashMap<String,String>();
063
064        uniqueKeys.put(NotificationConstants.BO_PROPERTY_NAMES.CHANNEL_ID, channelid);
065        uniqueKeys.put(NotificationConstants.BO_PROPERTY_NAMES.USER_ID, userid);
066
067        UserChannelSubscriptionBo
068                subscription = (UserChannelSubscriptionBo) businessObjectDao.findByUniqueKey(UserChannelSubscriptionBo.class, uniqueKeys);
069
070        return subscription; 
071    }
072
073    /**
074     * @see org.kuali.rice.ken.service.UserPreferenceService#subscribeToChannel(org.kuali.rice.ken.bo.UserChannelSubscriptionBo)
075     */
076    public void subscribeToChannel(UserChannelSubscriptionBo userChannelSubscription) {
077        LOG.info("Saving channel subscription");
078        try {
079            businessObjectDao.save(userChannelSubscription);
080        } catch(Exception e) {
081            LOG.error("Exception when saving userChannelSubscription");             
082        }
083        LOG.debug("Channel subscription saved");
084    }
085
086    /**
087     * @see org.kuali.rice.ken.service.UserPreferenceService#unsubscribeFromChannel(org.kuali.rice.ken.bo.UserChannelSubscriptionBo)
088     */
089    public void unsubscribeFromChannel(UserChannelSubscriptionBo userChannelSubscription) {
090        LOG.info("unsubscribing from channel"); 
091        try {
092            businessObjectDao.delete(userChannelSubscription);
093        } catch(Exception e) {
094            LOG.error("Exception when deleting userChannelSubscription");                   
095        }
096
097    }
098}