View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ken.service.impl;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.rice.core.api.criteria.QueryByCriteria;
20  import org.kuali.rice.ken.bo.UserChannelSubscriptionBo;
21  import org.kuali.rice.ken.service.NotificationChannelService;
22  import org.kuali.rice.ken.service.UserPreferenceService;
23  import org.kuali.rice.ken.util.NotificationConstants;
24  import org.kuali.rice.krad.data.DataObjectService;
25  
26  import java.util.Collection;
27  import java.util.List;
28  
29  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
30  
31  /**
32   * UserPreferenceService implementation - uses the businessObjectDao to get at data in the underlying database.
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class UserPreferenceServiceImpl implements UserPreferenceService {
36      private NotificationChannelService notificationChannelService;
37      private DataObjectService dataObjectService;
38  
39      private static final Logger LOG = Logger.getLogger(UserPreferenceServiceImpl.class);
40  
41      /**
42       * Constructs a UserPreferenceServiceImpl 
43       * @param dataObjectService
44       * @param notificationChannelService
45       */
46      public UserPreferenceServiceImpl(DataObjectService dataObjectService, NotificationChannelService notificationChannelService) {
47          this.dataObjectService = dataObjectService;
48          this.notificationChannelService = notificationChannelService;
49      }
50  
51      /**
52       * @see org.kuali.rice.ken.service.UserPreferenceService#getCurrentSubscriptions(java.lang.String)
53       */
54      @Override
55      public Collection<UserChannelSubscriptionBo> getCurrentSubscriptions(String userid) {
56          QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
57          criteria.setPredicates(equal("userId", userid));
58  
59          return dataObjectService.findMatching(UserChannelSubscriptionBo.class, criteria.build()).getResults();
60      }
61  
62      /**
63       * @see org.kuali.rice.ken.service.UserPreferenceService#getSubscription(java.lang.String, java.lang.String)
64       */
65      @Override
66      public UserChannelSubscriptionBo getSubscription(String channelid, String userid) {
67          QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
68          criteria.setPredicates(equal(NotificationConstants.BO_PROPERTY_NAMES.CHANNEL_ID, channelid),
69                  equal(NotificationConstants.BO_PROPERTY_NAMES.USER_ID, userid));
70  
71          List<UserChannelSubscriptionBo> subscriptions = dataObjectService.findMatching(UserChannelSubscriptionBo.class,criteria.build()).getResults();
72  
73          if (!subscriptions.isEmpty() && subscriptions.size() == 1) {
74              return subscriptions.get(0);
75          } else {
76              return null;
77          }
78      }
79  
80      /**
81       * @see org.kuali.rice.ken.service.UserPreferenceService#subscribeToChannel(org.kuali.rice.ken.bo.UserChannelSubscriptionBo)
82       */
83      @Override
84      public void subscribeToChannel(UserChannelSubscriptionBo userChannelSubscription) {
85          LOG.info("Saving channel subscription");
86          try {
87              dataObjectService.save(userChannelSubscription);
88          } catch(Exception e) {
89              LOG.error("Exception when saving userChannelSubscription");		    
90          }
91          LOG.debug("Channel subscription saved");
92      }
93  
94      /**
95       * @see org.kuali.rice.ken.service.UserPreferenceService#unsubscribeFromChannel(org.kuali.rice.ken.bo.UserChannelSubscriptionBo)
96       */
97      @Override
98      public void unsubscribeFromChannel(UserChannelSubscriptionBo userChannelSubscription) {
99          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 }