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.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.apache.log4j.Logger;
28 import org.kuali.rice.ken.bo.Notification;
29 import org.kuali.rice.ken.bo.NotificationMessageDelivery;
30 import org.kuali.rice.ken.bo.NotificationRecipient;
31 import org.kuali.rice.ken.bo.NotificationSender;
32 import org.kuali.rice.ken.service.NotificationMessageDeliveryService;
33 import org.kuali.rice.ken.service.NotificationService;
34 import org.kuali.rice.ken.service.NotificationWorkflowDocumentService;
35 import org.kuali.rice.ken.util.NotificationConstants;
36 import org.kuali.rice.ken.util.Util;
37 import org.kuali.rice.kew.util.KEWConstants;
38 import org.kuali.rice.kim.api.identity.principal.Principal;
39 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
40 import org.springframework.web.servlet.ModelAndView;
41 import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
42
43
44
45
46
47
48 public class NotificationController extends MultiActionController {
49
50 private static final Logger LOG = Logger.getLogger(NotificationController.class);
51
52 protected NotificationService notificationService;
53 protected NotificationWorkflowDocumentService notificationWorkflowDocService;
54 protected NotificationMessageDeliveryService messageDeliveryService;
55
56
57
58
59
60 public void setNotificationService(NotificationService notificationService) {
61 this.notificationService = notificationService;
62 }
63
64
65
66
67
68 public void setNotificationWorkflowDocumentService(NotificationWorkflowDocumentService s) {
69 this.notificationWorkflowDocService = s;
70 }
71
72
73
74
75
76 public void setMessageDeliveryService(NotificationMessageDeliveryService messageDeliveryService) {
77 this.messageDeliveryService = messageDeliveryService;
78 }
79
80
81
82
83
84
85
86
87
88 public ModelAndView displayHome(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
89 String view = "HomePage";
90 LOG.debug("remoteUser: "+request.getRemoteUser());
91 Map<String, Object> model = new HashMap<String, Object>();
92 return new ModelAndView(view, model);
93 }
94
95
96
97
98
99
100
101
102
103 public ModelAndView displayNotificationsSent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
104 String view = "NotificationsSent";
105 LOG.debug("remoteUser: "+request.getRemoteUser());
106 Map<String, Object> model = new HashMap<String, Object>();
107 model.put("userId", request.getRemoteUser());
108 return new ModelAndView(view, model);
109 }
110
111
112
113
114
115
116
117
118
119 public ModelAndView displaySearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
120 String view = "Search";
121 LOG.debug("remoteUser: "+request.getRemoteUser());
122 Map<String, Object> model = new HashMap<String, Object>();
123 return new ModelAndView(view, model);
124 }
125
126
127
128
129
130
131
132
133
134 public ModelAndView displayLookupUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
135 String view = "LookupUsers";
136 LOG.debug("remoteUser: "+request.getRemoteUser());
137 Map<String, Object> model = new HashMap<String, Object>();
138 return new ModelAndView(view, model);
139 }
140
141
142
143
144
145
146
147
148
149 public ModelAndView displayLookupWorkgroups(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
150 String view = "LookupWorkgroups";
151 LOG.debug("remoteUser: "+request.getRemoteUser());
152 Map<String, Object> model = new HashMap<String, Object>();
153 return new ModelAndView(view, model);
154 }
155
156
157
158
159
160
161
162
163
164 protected NotificationMessageDelivery determineMessageFromRequest(HttpServletRequest request) {
165
166
167
168
169
170 String messageDeliveryId = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.MSG_DELIVERY_ID);
171 String delivererId = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.DELIVERER_ID);
172 if (delivererId == null) {
173 delivererId = request.getParameter(KEWConstants.DOCUMENT_ID_PARAMETER);
174 }
175
176 NotificationMessageDelivery messageDelivery;
177 if (messageDeliveryId != null) {
178 LOG.debug("Looking up notification with messageDeliveryId: "+messageDeliveryId);
179 try {
180 messageDelivery = messageDeliveryService.getNotificationMessageDelivery(new Long(messageDeliveryId));
181 } catch (Exception e) {
182 throw new RuntimeException("Error getting message with id: " + messageDeliveryId, e);
183 }
184 } else if (delivererId != null) {
185 LOG.debug("Looking up notification with workflowId: "+delivererId);
186 try {
187 messageDelivery = messageDeliveryService.getNotificationMessageDeliveryByDelivererId(delivererId);
188 } catch (Exception e) {
189 LOG.error("Error getting message with from deliverer id: " + delivererId, e);
190 throw new RuntimeException("Error getting message with deliverer id: " + delivererId, e);
191 }
192 } else {
193 throw new RuntimeException("Neither message ('" + NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.MSG_DELIVERY_ID
194 + "') nor deliverer id ('" + NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.DELIVERER_ID + "') were specified in the request");
195 }
196
197 return messageDelivery;
198 }
199
200
201
202
203
204 protected boolean requestIsFromKEW(HttpServletRequest req) {
205 return req.getParameter(KEWConstants.DOCUMENT_ID_PARAMETER) != null;
206 }
207
208
209
210
211
212
213
214
215
216 public ModelAndView displayNotificationDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
217 String view = "NotificationDetail";
218
219 String principalNm = request.getRemoteUser();
220 String command = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.COMMAND);
221 String standaloneWindow = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW);
222
223 NotificationMessageDelivery messageDelivery = determineMessageFromRequest(request);
224
225 Notification notification = messageDelivery.getNotification();
226 boolean actionable = false;
227
228 if (requestIsFromKEW(request)) {
229
230 if(command != null &&
231 (command.equals(NotificationConstants.NOTIFICATION_DETAIL_VIEWS.NORMAL_VIEW) ||
232 command.equals(NotificationConstants.NOTIFICATION_DETAIL_VIEWS.DOC_SEARCH_VIEW))) {
233 standaloneWindow = "true";
234 }
235
236
237 command = NotificationConstants.NOTIFICATION_DETAIL_VIEWS.INLINE;
238 }
239
240 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalNm);
241 if (principal != null) {
242 actionable = (principal.getPrincipalId()).equals(messageDelivery.getUserRecipientId()) && NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED.equals(messageDelivery.getMessageDeliveryStatus());
243 } else {
244 throw new RuntimeException("There is no principal for principalNm " + principalNm);
245 }
246
247 List<NotificationSender> senders = notification.getSenders();
248 List<NotificationRecipient> recipients = notification.getRecipients();
249
250 String contenthtml = Util.transformContent(notification);
251
252
253 if (command != null && command.equals(NotificationConstants.NOTIFICATION_DETAIL_VIEWS.INLINE)) {
254 view = "NotificationDetailInline";
255 }
256
257 Map<String, Object> model = new HashMap<String, Object>();
258 model.put("notification", notification);
259 model.put("senders", senders);
260 model.put("recipients", recipients);
261 model.put("contenthtml", contenthtml);
262 model.put("messageDeliveryId", messageDelivery.getId());
263 model.put("command", command);
264 model.put("actionable", actionable);
265 model.put(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW, standaloneWindow);
266 return new ModelAndView(view, model);
267 }
268
269
270
271
272
273
274
275 public ModelAndView dismissMessage(HttpServletRequest request, HttpServletResponse response) {
276 String command = request.getParameter("action");
277 if (command == null) throw new RuntimeException("Dismissal command not specified");
278
279 if (NotificationConstants.ACK_CAUSE.equals(command)) {
280 return dismissMessage(command, "Notificaton acknowledged. Please refresh your action list.", request, response);
281 } else if (NotificationConstants.FYI_CAUSE.equals(command)) {
282 return dismissMessage(command, "Action Taken. Please refresh your action list.", request, response);
283 } else {
284 throw new RuntimeException("Unknown dismissal command: " + command);
285 }
286 }
287
288
289
290
291
292
293
294
295
296
297 private ModelAndView dismissMessage(String action, String message, HttpServletRequest request, HttpServletResponse response) {
298 String view = "NotificationDetail";
299
300 String principalNm = request.getRemoteUser();
301 String messageDeliveryId = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.MSG_DELIVERY_ID);
302 String command = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.COMMAND);
303 String standaloneWindow = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW);
304
305 if (messageDeliveryId == null) {
306 throw new RuntimeException("A null messageDeliveryId was provided.");
307 }
308
309 LOG.debug("messageDeliveryId: "+messageDeliveryId);
310 LOG.debug("command: "+command);
311
312
313
314
315
316
317 NotificationMessageDelivery delivery = messageDeliveryService.getNotificationMessageDelivery(Long.decode(messageDeliveryId));
318 if (delivery == null) {
319 throw new RuntimeException("Could not find message delivery with id " + messageDeliveryId);
320 }
321 Notification notification = delivery.getNotification();
322
323
324
325
326
327 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalNm);
328 notificationService.dismissNotificationMessageDelivery(delivery.getId(), principal.getPrincipalId(), action);
329
330 List<NotificationSender> senders = notification.getSenders();
331 List<NotificationRecipient> recipients = notification.getRecipients();
332
333 String contenthtml = Util.transformContent(notification);
334
335
336 if(standaloneWindow != null && standaloneWindow.equals("true")) {
337 view = "NotificationActionTakenCloseWindow";
338 } else {
339 if (command != null && command.equals(NotificationConstants.NOTIFICATION_DETAIL_VIEWS.INLINE)) {
340 view = "NotificationDetailInline";
341 }
342 }
343
344 Map<String, Object> model = new HashMap<String, Object>();
345 model.put("notification", notification);
346 model.put("message", message);
347 model.put("senders", senders);
348 model.put("recipients", recipients);
349 model.put("contenthtml", contenthtml);
350 model.put("messageDeliveryId", messageDeliveryId);
351 model.put("command", command);
352 model.put(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW, standaloneWindow);
353 return new ModelAndView(view, model);
354 }
355 }