1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33
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
43
44
45
46 public UserPreferenceServiceImpl(DataObjectService dataObjectService, NotificationChannelService notificationChannelService) {
47 this.dataObjectService = dataObjectService;
48 this.notificationChannelService = notificationChannelService;
49 }
50
51
52
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
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
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
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 }