1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ken.web.spring;
17
18 import java.io.IOException;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.Map;
23
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.apache.log4j.Logger;
29 import org.kuali.rice.ken.bo.NotificationChannelBo;
30 import org.kuali.rice.ken.bo.UserChannelSubscriptionBo;
31 import org.kuali.rice.ken.service.NotificationChannelService;
32 import org.kuali.rice.ken.service.UserPreferenceService;
33 import org.springframework.web.servlet.ModelAndView;
34 import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
35
36
37
38
39
40 public class UserPreferencesController extends MultiActionController {
41
42 private static String view = "";
43
44
45 private static final Logger LOG = Logger.getLogger(UserPreferencesController.class);
46
47 protected NotificationChannelService notificationChannelService;
48 protected UserPreferenceService userPreferenceService;
49 protected Object notificationMessageDelivererRegistryService;
50
51
52
53
54
55
56 public void setNotificationChannelService(NotificationChannelService notificationChannelService) {
57 this.notificationChannelService = notificationChannelService;
58 }
59
60
61
62
63
64 public void setUserPreferenceService(UserPreferenceService userPreferenceService) {
65 this.userPreferenceService = userPreferenceService;
66 }
67
68
69
70
71
72 public void setNotificationMessageDelivererRegistryService(Object notificationMessageDelivererRegistryService) {
73 this.notificationMessageDelivererRegistryService = notificationMessageDelivererRegistryService;
74 }
75
76
77
78
79
80
81
82
83
84 public ModelAndView displayActionListPreferences(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
85 view = "ActionListPreferences";
86 LOG.debug("remoteUser: "+request.getRemoteUser());
87 Map<String, Object> model = new HashMap<String, Object>();
88 return new ModelAndView(view, model);
89 }
90
91
92
93
94
95
96
97
98
99 public ModelAndView displayUserPreferences(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
100
101 view = "UserPreferencesForm";
102 String userid = request.getRemoteUser();
103 LOG.debug("remoteUser: "+userid);
104
105 Collection<NotificationChannelBo> channels = this.notificationChannelService.getSubscribableChannels();
106
107 Collection<UserChannelSubscriptionBo> subscriptions = this.userPreferenceService.getCurrentSubscriptions(userid);
108 Map<String, Object> currentsubs = new HashMap<String, Object>();
109 Iterator<UserChannelSubscriptionBo> i = subscriptions.iterator();
110 while (i.hasNext()) {
111 UserChannelSubscriptionBo sub = i.next();
112 String subid = Long.toString(sub.getChannel().getId());
113 currentsubs.put(subid, subid);
114 LOG.debug("currently subscribed to: "+sub.getChannel().getId());
115 }
116 Map<String, Object> model = new HashMap<String, Object>();
117 model.put("channels", channels);
118 model.put("currentsubs", currentsubs);
119
120 return new ModelAndView(view, model);
121 }
122
123
124
125
126
127
128
129
130
131 public ModelAndView subscribeToChannel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
132
133 view = "UserPreferencesForm";
134 String userid = request.getRemoteUser();
135 LOG.debug("remoteUser: "+userid);
136 String channelid = request.getParameter("channelid");
137 NotificationChannelBo newChannel = this.notificationChannelService.getNotificationChannel(channelid);
138 LOG.debug("newChannel name:"+newChannel.getName());
139 UserChannelSubscriptionBo newSub = new UserChannelSubscriptionBo();
140 newSub.setUserId(userid);
141 newSub.setChannel(newChannel);
142 LOG.debug("Calling service to subscribe to channel: "+newChannel.getName());
143 this.userPreferenceService.subscribeToChannel(newSub);
144
145
146 Collection<UserChannelSubscriptionBo> subscriptions = this.userPreferenceService.getCurrentSubscriptions(userid);
147 Map<String, Object> currentsubs = new HashMap<String, Object>();;
148 Iterator<UserChannelSubscriptionBo> i = subscriptions.iterator();
149 while (i.hasNext()) {
150 UserChannelSubscriptionBo sub = i.next();
151 String subid = Long.toString(sub.getChannel().getId());
152 currentsubs.put(subid, subid);
153 LOG.debug("currently subscribed to: "+sub.getChannel().getId());
154 }
155
156
157 Collection<NotificationChannelBo> channels = this.notificationChannelService.getSubscribableChannels();
158
159 Map<String, Object> model = new HashMap<String, Object>();
160 model.put("channels", channels);
161 model.put("currentsubs", currentsubs);
162 return new ModelAndView(view, model);
163 }
164
165
166
167
168
169
170
171
172
173 public ModelAndView unsubscribeFromChannel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
174 view = "UserPreferencesForm";
175 String userid = request.getRemoteUser();
176 LOG.debug("remoteUser: "+userid);
177 String channelid = request.getParameter("channelid");
178
179 NotificationChannelBo newChannel = this.notificationChannelService.getNotificationChannel(channelid);
180 LOG.debug("getting channel (id, user): "+channelid+","+userid);
181 UserChannelSubscriptionBo oldsub = this.userPreferenceService.getSubscription(channelid, userid);
182 oldsub.setChannel(newChannel);
183
184 LOG.debug("Calling service to unsubscribe: "+newChannel.getName());
185 this.userPreferenceService.unsubscribeFromChannel(oldsub);
186 LOG.debug("Finished unsubscribe service: "+newChannel.getName());
187
188
189 Collection<UserChannelSubscriptionBo> subscriptions = this.userPreferenceService.getCurrentSubscriptions(userid);
190 Map<String, Object> currentsubs = new HashMap<String, Object>();
191 Iterator<UserChannelSubscriptionBo> i = subscriptions.iterator();
192 while (i.hasNext()) {
193 UserChannelSubscriptionBo sub = i.next();
194 String subid = Long.toString(sub.getChannel().getId());
195 currentsubs.put(subid, subid);
196 LOG.debug("currently subscribed to: "+sub.getChannel().getId());
197 }
198
199
200 Collection<NotificationChannelBo> channels = this.notificationChannelService.getSubscribableChannels();
201
202 Map<String, Object> model = new HashMap<String, Object>();
203 model.put("channels", channels);
204 model.put("currentsubs", currentsubs);
205 return new ModelAndView(view, model);
206
207 }
208 }