1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kcb.web.spring;
17
18 import org.apache.log4j.Logger;
19 import org.kuali.rice.kcb.bo.RecipientDelivererConfig;
20 import org.kuali.rice.kcb.deliverer.MessageDeliverer;
21 import org.kuali.rice.kcb.exception.ErrorList;
22 import org.kuali.rice.kcb.service.KENIntegrationService;
23 import org.kuali.rice.kcb.service.MessageDelivererRegistryService;
24 import org.kuali.rice.kcb.service.RecipientPreferenceService;
25 import org.springframework.beans.factory.annotation.Required;
26 import org.springframework.web.servlet.ModelAndView;
27 import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
28
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.Map;
37
38
39
40
41
42 public class UserPreferencesController extends MultiActionController {
43
44 private static final Logger LOG = Logger.getLogger(UserPreferencesController.class);
45
46 private static final String VIEW = "DelivererPreferences";
47 private static final String KEW_CHANNEL = "KEW";
48 protected RecipientPreferenceService recipientPreferenceService;
49 protected MessageDelivererRegistryService messageDelivererRegistryService;
50 protected KENIntegrationService kenIntegrationService;
51
52
53
54
55
56 @Required
57 public void setRecipientPreferenceService(RecipientPreferenceService userPreferenceService) {
58 this.recipientPreferenceService = userPreferenceService;
59 }
60
61
62
63
64
65 @Required
66 public void setMessageDelivererRegistryService(MessageDelivererRegistryService messageDelivererRegistryService) {
67 this.messageDelivererRegistryService = messageDelivererRegistryService;
68 }
69
70
71
72
73
74 @Required
75 public void setKenIntegrationService(KENIntegrationService kis) {
76 this.kenIntegrationService = kis;
77 }
78
79
80
81
82 protected Collection<String> getAllChannels() {
83
84 Collection<String> allChannels = new ArrayList<String>();
85
86 allChannels.addAll(kenIntegrationService.getAllChannelNames());
87 return allChannels;
88 }
89
90
91
92
93
94
95
96
97
98
99 public ModelAndView displayDelivererConfigurationForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
100 String userid = request.getRemoteUser();
101 LOG.debug("remoteUser: "+userid);
102
103
104 Collection<MessageDeliverer> deliveryTypes = this.messageDelivererRegistryService.getAllDeliverers();
105
106
107 Collection<String> channels = getAllChannels();
108
109
110 HashMap<String, String> preferences = this.recipientPreferenceService.getRecipientPreferences(userid);
111
112
113 Collection<RecipientDelivererConfig> currentDeliverers = this.recipientPreferenceService.getDeliverersForRecipient(userid);
114
115 Map<String, Boolean> currentDeliverersMap = new HashMap<String, Boolean>();
116 for (RecipientDelivererConfig udc: currentDeliverers) {
117 String channelName = udc.getChannel();
118 currentDeliverersMap.put(udc.getDelivererName() + "." + channelName, Boolean.TRUE);
119 }
120
121 Map<String, Object> model = new HashMap<String, Object>();
122 model.put("channels", channels);
123 model.put("deliveryTypes", deliveryTypes);
124 model.put("preferences", preferences);
125 model.put("currentDeliverersMap", currentDeliverersMap);
126 return new ModelAndView(VIEW, model);
127 }
128
129
130
131
132
133
134
135
136
137 public ModelAndView saveDelivererConfiguration(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
138 String userid = request.getRemoteUser();
139 LOG.debug("remoteUser: "+userid);
140 boolean error = false;
141
142 Map<String, Object> model = new HashMap<String, Object>();
143
144
145 HashMap<String, String> preferences = new HashMap<String, String>();
146
147
148
149
150
151 Collection<MessageDeliverer> deliveryTypes = this.messageDelivererRegistryService.getAllDeliverers();
152
153
154 this.recipientPreferenceService.removeRecipientDelivererConfigs(userid);
155
156 for (MessageDeliverer dt: deliveryTypes) {
157 String deliveryTypeName = dt.getName();
158 HashMap<String,String> prefMap = dt.getPreferenceKeys();
159 LOG.debug("deliveryName: "+deliveryTypeName);
160 HashMap<String, String> userprefs = new HashMap<String, String>();
161 for (String prefKey:prefMap.keySet()) {
162 LOG.debug(" key: "+prefKey+", value: "+request.getParameter(deliveryTypeName+"."+prefKey));
163 userprefs.put(deliveryTypeName+"."+prefKey, request.getParameter(deliveryTypeName+"."+prefKey ));
164 preferences.put(deliveryTypeName+"."+prefKey, request.getParameter(deliveryTypeName+"."+prefKey ));
165 }
166 try {
167 this.recipientPreferenceService.saveRecipientPreferences(userid, userprefs, dt);
168 } catch (ErrorList errorlist) {
169 error = true;
170 model.put("errorList", errorlist.getErrors()) ;
171 }
172
173
174 String[] channels = request.getParameterValues(deliveryTypeName+".channels");
175 if (channels != null && channels.length > 0) {
176 for (int j=0; j < channels.length; j++) {
177 LOG.debug(deliveryTypeName+".channels["+j+"] "+channels[j]);
178 }
179 }
180
181 this.recipientPreferenceService.saveRecipientDelivererConfig(userid, deliveryTypeName, channels);
182 }
183
184
185 Collection<String> channels = getAllChannels();
186
187
188 Collection<RecipientDelivererConfig> currentDeliverers = this.recipientPreferenceService.getDeliverersForRecipient(userid);
189 Map<String, Object> currentDeliverersMap = new HashMap<String, Object>();
190 for (RecipientDelivererConfig udc: currentDeliverers) {
191 String channelId = udc.getChannel();
192 currentDeliverersMap.put(udc.getDelivererName()+"."+channelId, Boolean.TRUE);
193 }
194
195
196
197
198
199
200
201
202
203 model.put("channels", channels);
204 model.put("deliveryTypes", deliveryTypes);
205 model.put("preferences", preferences);
206 model.put("currentDeliverersMap", currentDeliverersMap);
207 model.put("message", "Update Successful");
208
209 return new ModelAndView(VIEW, model);
210 }
211 }