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