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.api.criteria.QueryByCriteria;
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;
024import org.kuali.rice.krad.data.DataObjectService;
025
026import java.util.Collection;
027import java.util.List;
028
029import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
030
031/**
032 * UserPreferenceService implementation - uses the businessObjectDao to get at data in the underlying database.
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035public class UserPreferenceServiceImpl implements UserPreferenceService {
036    private NotificationChannelService notificationChannelService;
037    private DataObjectService dataObjectService;
038
039    private static final Logger LOG = Logger.getLogger(UserPreferenceServiceImpl.class);
040
041    /**
042     * Constructs a UserPreferenceServiceImpl 
043     * @param dataObjectService
044     * @param notificationChannelService
045     */
046    public UserPreferenceServiceImpl(DataObjectService dataObjectService, NotificationChannelService notificationChannelService) {
047        this.dataObjectService = dataObjectService;
048        this.notificationChannelService = notificationChannelService;
049    }
050
051    /**
052     * @see org.kuali.rice.ken.service.UserPreferenceService#getCurrentSubscriptions(java.lang.String)
053     */
054    @Override
055    public Collection<UserChannelSubscriptionBo> getCurrentSubscriptions(String userid) {
056        QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
057        criteria.setPredicates(equal("userId", userid));
058
059        return dataObjectService.findMatching(UserChannelSubscriptionBo.class, criteria.build()).getResults();
060    }
061
062    /**
063     * @see org.kuali.rice.ken.service.UserPreferenceService#getSubscription(java.lang.String, java.lang.String)
064     */
065    @Override
066    public UserChannelSubscriptionBo getSubscription(String channelid, String userid) {
067        QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
068        criteria.setPredicates(equal(NotificationConstants.BO_PROPERTY_NAMES.CHANNEL_ID, channelid),
069                equal(NotificationConstants.BO_PROPERTY_NAMES.USER_ID, userid));
070
071        List<UserChannelSubscriptionBo> subscriptions = dataObjectService.findMatching(UserChannelSubscriptionBo.class,criteria.build()).getResults();
072
073        if (!subscriptions.isEmpty() && subscriptions.size() == 1) {
074            return subscriptions.get(0);
075        } else {
076            return null;
077        }
078    }
079
080    /**
081     * @see org.kuali.rice.ken.service.UserPreferenceService#subscribeToChannel(org.kuali.rice.ken.bo.UserChannelSubscriptionBo)
082     */
083    @Override
084    public void subscribeToChannel(UserChannelSubscriptionBo userChannelSubscription) {
085        LOG.info("Saving channel subscription");
086        try {
087            dataObjectService.save(userChannelSubscription);
088        } catch(Exception e) {
089            LOG.error("Exception when saving userChannelSubscription");             
090        }
091        LOG.debug("Channel subscription saved");
092    }
093
094    /**
095     * @see org.kuali.rice.ken.service.UserPreferenceService#unsubscribeFromChannel(org.kuali.rice.ken.bo.UserChannelSubscriptionBo)
096     */
097    @Override
098    public void unsubscribeFromChannel(UserChannelSubscriptionBo userChannelSubscription) {
099        LOG.info("unsubscribing from channel"); 
100        try {
101            dataObjectService.delete(userChannelSubscription);
102        } catch(Exception e) {
103            LOG.error("Exception when deleting userChannelSubscription");                   
104        }
105
106    }
107}