Coverage Report - org.kuali.rice.ken.web.spring.NotificationController
 
Classes in this File Line Coverage Branch Coverage Complexity
NotificationController
0%
0/114
0%
0/42
3.308
 
 1  
 /*
 2  
  * Copyright 2007 The Kuali Foundation
 3  
  * 
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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.springframework.web.servlet.ModelAndView;
 39  
 import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
 40  
 import org.springframework.web.servlet.view.RedirectView;
 41  
 
 42  
 
 43  
 /**
 44  
  * This class is the controller for the basic notification related actions - viewing, etc.
 45  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 46  
  */
 47  0
 public class NotificationController extends MultiActionController {
 48  
     /** Logger for this class and subclasses */
 49  0
     private static final Logger LOG = Logger.getLogger(NotificationController.class);
 50  
     
 51  
     protected NotificationService notificationService;
 52  
     protected NotificationWorkflowDocumentService notificationWorkflowDocService;
 53  
     protected NotificationMessageDeliveryService messageDeliveryService;
 54  
    
 55  
     /**
 56  
      * Set the NotificationService
 57  
      * @param notificationService
 58  
      */   
 59  
     public void setNotificationService(NotificationService notificationService) {
 60  0
         this.notificationService = notificationService;
 61  0
     }
 62  
 
 63  
     /**
 64  
      * This method sets the NotificationWorkflowDocumentService
 65  
      * @param s
 66  
      */
 67  
     public void setNotificationWorkflowDocumentService(NotificationWorkflowDocumentService s) {
 68  0
         this.notificationWorkflowDocService = s;
 69  0
     }
 70  
 
 71  
     /**
 72  
      * Sets the messageDeliveryService attribute value.
 73  
      * @param messageDeliveryService The messageDeliveryService to set.
 74  
      */
 75  
     public void setMessageDeliveryService(NotificationMessageDeliveryService messageDeliveryService) {
 76  0
         this.messageDeliveryService = messageDeliveryService;
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Handles the display of the main home page in the system.
 81  
      * @param request : a servlet request
 82  
      * @param response : a servlet response
 83  
      * @throws ServletException : an exception
 84  
      * @throws IOException : an exception
 85  
      * @return a ModelAndView object
 86  
      */   
 87  
     public ModelAndView displayHome(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 88  0
         String view = "HomePage";
 89  0
         LOG.debug("remoteUser: "+request.getRemoteUser());
 90  0
         Map<String, Object> model = new HashMap<String, Object>(); 
 91  0
         return new ModelAndView(view, model);
 92  
     }
 93  
    
 94  
     /**
 95  
      * This method handles displaying the notifications that an individual sent.
 96  
      * @param request
 97  
      * @param response
 98  
      * @return ModelAndView
 99  
      * @throws ServletException
 100  
      * @throws IOException
 101  
      */
 102  
     public ModelAndView displayNotificationsSent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 103  0
         String view = "NotificationsSent";
 104  0
         LOG.debug("remoteUser: "+request.getRemoteUser());
 105  0
         Map<String, Object> model = new HashMap<String, Object>();
 106  0
         model.put("userId", request.getRemoteUser());
 107  0
         return new ModelAndView(view, model);
 108  
     }
 109  
 
 110  
     /**
 111  
      * This method handles displaying the search screen.
 112  
      * @param request
 113  
      * @param response
 114  
      * @return ModelAndView
 115  
      * @throws ServletException
 116  
      * @throws IOException
 117  
      */
 118  
     public ModelAndView displaySearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 119  0
         String view = "Search";
 120  0
         LOG.debug("remoteUser: "+request.getRemoteUser());
 121  0
         Map<String, Object> model = new HashMap<String, Object>(); 
 122  0
         return new ModelAndView(view, model);
 123  
     }
 124  
 
 125  
     /**
 126  
      * This method displays the user lookup screen.
 127  
      * @param request
 128  
      * @param response
 129  
      * @return
 130  
      * @throws ServletException
 131  
      * @throws IOException
 132  
      */
 133  
     public ModelAndView displayLookupUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 134  0
         String view = "LookupUsers";
 135  0
         LOG.debug("remoteUser: "+request.getRemoteUser());
 136  0
         Map<String, Object> model = new HashMap<String, Object>(); 
 137  0
         return new ModelAndView(view, model);
 138  
     }
 139  
 
 140  
     /**
 141  
      * This method displays the workgroup lookup screen.
 142  
      * @param request
 143  
      * @param response
 144  
      * @return
 145  
      * @throws ServletException
 146  
      * @throws IOException
 147  
      */
 148  
     public ModelAndView displayLookupWorkgroups(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 149  0
         String view = "LookupWorkgroups";
 150  0
         LOG.debug("remoteUser: "+request.getRemoteUser());
 151  0
         Map<String, Object> model = new HashMap<String, Object>(); 
 152  0
         return new ModelAndView(view, model);
 153  
     }
 154  
 
 155  
 
 156  
     /**
 157  
      * This method retrieves the NotificationMessageDelivery given an HttpServletRequest which
 158  
      * may contain EITHER a message delivery id or a workflow doc id.  Therefore, this is a
 159  
      * "special case" for handling the workflow deliverer.
 160  
      * @param request the incoming {@link HttpServletRequest}
 161  
      * @return the {@link NotificationMessageDelivery} or null if not found
 162  
      */
 163  
     protected NotificationMessageDelivery determineMessageFromRequest(HttpServletRequest request) {
 164  
         /**
 165  
          * We can get the NotificationMessageDelivery object given a workflow ID or a NotificationMessageDelivery
 166  
          * Id.  This method might be called either from a workflow action list or
 167  
          * as a link from a message deliverer endpoint such as an email message.
 168  
          */
 169  0
         String messageDeliveryId = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.MSG_DELIVERY_ID);
 170  0
         String delivererId = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.DELIVERER_ID);
 171  0
         if (delivererId == null) {
 172  0
             delivererId = request.getParameter(KEWConstants.DOCUMENT_ID_PARAMETER);
 173  
         }
 174  
 
 175  
         NotificationMessageDelivery messageDelivery;
 176  0
         if (messageDeliveryId != null) { // this means that the request came in not from the action list, but rather from a delivery end point
 177  0
             LOG.debug("Looking up notification with messageDeliveryId: "+messageDeliveryId);
 178  
             try {
 179  0
                 messageDelivery = messageDeliveryService.getNotificationMessageDelivery(new Long(messageDeliveryId));
 180  0
             } catch (Exception e) {
 181  0
                 throw new RuntimeException("Error getting message with id: " + messageDeliveryId, e);
 182  0
             }
 183  0
         } else if (delivererId != null) {  // this means that the request was triggered via the action list
 184  0
             LOG.debug("Looking up notification with workflowId: "+delivererId);
 185  
             try {
 186  0
                 messageDelivery = messageDeliveryService.getNotificationMessageDeliveryByDelivererId(delivererId);
 187  0
             } catch (Exception e) {
 188  0
                 LOG.error("Error getting message with from deliverer id: " + delivererId, e);
 189  0
                 throw new RuntimeException("Error getting message with deliverer id: " + delivererId, e);
 190  0
             }
 191  
         } else {
 192  0
             throw new RuntimeException("Neither message ('" + NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.MSG_DELIVERY_ID
 193  
                                        + "') nor deliverer id ('" + NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.DELIVERER_ID + "') were specified in the request");
 194  
         }
 195  
         
 196  0
         return messageDelivery;
 197  
     }
 198  
 
 199  
     /**
 200  
      * @param req the {@link HttpServletRequest}
 201  
      * @return whether the incoming request was from the action list
 202  
      */
 203  
     protected boolean requestIsFromKEW(HttpServletRequest req) {
 204  0
         return req.getParameter(KEWConstants.DOCUMENT_ID_PARAMETER) != null;
 205  
     }
 206  
 
 207  
     /**
 208  
      * This controller handles displaying the appropriate notification details for a specific record.
 209  
      * @param request : a servlet request
 210  
      * @param response : a servlet response
 211  
      * @throws ServletException : an exception
 212  
      * @throws IOException : an exception
 213  
      * @return a ModelAndView object
 214  
      */   
 215  
     public ModelAndView displayNotificationDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 216  0
         String view = "NotificationDetail"; // default to full view
 217  
 
 218  0
         String user = request.getRemoteUser();
 219  0
         String command = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.COMMAND);
 220  0
         String standaloneWindow = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW);
 221  
 
 222  0
         NotificationMessageDelivery messageDelivery = determineMessageFromRequest(request);
 223  
         // now get the notification from the message delivery object
 224  0
         Notification notification = messageDelivery.getNotification();
 225  0
         boolean actionable = false;
 226  
 
 227  0
         if (requestIsFromKEW(request)) {
 228  
             // check to see if this was a standalone window by examining the command from KEW before setting it to INLINE to force an inline view
 229  0
             if(command != null && 
 230  
                     (command.equals(NotificationConstants.NOTIFICATION_DETAIL_VIEWS.NORMAL_VIEW) || 
 231  
                             command.equals(NotificationConstants.NOTIFICATION_DETAIL_VIEWS.DOC_SEARCH_VIEW))) {
 232  0
                 standaloneWindow = "true";
 233  
             }
 234  
 
 235  
             // we want all messages from the action list in line
 236  0
             command = NotificationConstants.NOTIFICATION_DETAIL_VIEWS.INLINE;
 237  
         }
 238  
 
 239  0
         actionable = user.equals(messageDelivery.getUserRecipientId()) && NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED.equals(messageDelivery.getMessageDeliveryStatus());
 240  
 
 241  0
         List<NotificationSender> senders = notification.getSenders();
 242  0
         List<NotificationRecipient> recipients = notification.getRecipients();
 243  
 
 244  0
         String contenthtml = Util.transformContent(notification);
 245  
 
 246  
         // check to see if the details need to be rendered in line (no stuff around them)
 247  0
         if (command != null && command.equals(NotificationConstants.NOTIFICATION_DETAIL_VIEWS.INLINE)) {
 248  0
             view = "NotificationDetailInline";   
 249  
         } 
 250  
 
 251  0
         Map<String, Object> model = new HashMap<String, Object>();
 252  0
         model.put("notification", notification);
 253  0
         model.put("senders", senders);
 254  0
         model.put("recipients", recipients);
 255  0
         model.put("contenthtml", contenthtml);
 256  0
         model.put("messageDeliveryId", messageDelivery.getId());
 257  0
         model.put("command", command);
 258  0
         model.put("actionable", actionable);
 259  0
         model.put(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW, standaloneWindow);
 260  0
         return new ModelAndView(view, model);
 261  
     }
 262  
 
 263  
     /**
 264  
      * This method handles user dismissal of a message
 265  
      * @param request : a servlet request
 266  
      * @param response : a servlet response
 267  
      * @return a ModelAndView object
 268  
      */   
 269  
     public ModelAndView dismissMessage(HttpServletRequest request, HttpServletResponse response) {
 270  0
         String command = request.getParameter("action");
 271  0
         if (command == null) throw new RuntimeException("Dismissal command not specified");
 272  
 
 273  0
         if (NotificationConstants.ACK_CAUSE.equals(command)) {
 274  0
             return dismissMessage(command, "Notificaton acknowledged.  Please refresh your action list.", request, response);
 275  0
         } else if (NotificationConstants.FYI_CAUSE.equals(command)) {
 276  0
             return dismissMessage(command, "Action Taken.  Please refresh your action list.", request, response);
 277  
         } else {
 278  0
             throw new RuntimeException("Unknown dismissal command: " + command);
 279  
         }
 280  
     }
 281  
 
 282  
     /**
 283  
      * This method takes an action on the message delivery - dismisses it with the action/cause that comes from the
 284  
      * UI layer
 285  
      * @param action the action or cause of the dismissal
 286  
      * @param message the message to display to the user
 287  
      * @param request the HttpServletRequest
 288  
      * @param response the HttpServletResponse
 289  
      * @return an appropriate ModelAndView
 290  
      */
 291  
     private ModelAndView dismissMessage(String action, String message, HttpServletRequest request, HttpServletResponse response) {
 292  0
         String view = "NotificationDetail";
 293  
 
 294  0
         String user = request.getRemoteUser();
 295  0
         String messageDeliveryId = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.MSG_DELIVERY_ID);
 296  0
         String command = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.COMMAND);
 297  0
         String standaloneWindow = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW);
 298  
 
 299  0
         if (messageDeliveryId == null) {
 300  0
             throw new RuntimeException("A null messageDeliveryId was provided.");
 301  
         }
 302  
 
 303  0
         LOG.debug("messageDeliveryId: "+messageDeliveryId);
 304  0
         LOG.debug("command: "+command);
 305  
 
 306  
         /**
 307  
          * We can get the notification object given a workflow ID or a notification
 308  
          * Id.  This method might be called either from a workflow action list or
 309  
          * as a link from a message deliverer endpoint such as an email message.  
 310  
          */        
 311  0
         NotificationMessageDelivery delivery = messageDeliveryService.getNotificationMessageDelivery(Long.decode(messageDeliveryId));
 312  0
         if (delivery == null) {
 313  0
             throw new RuntimeException("Could not find message delivery with id " + messageDeliveryId);
 314  
         }
 315  0
         Notification notification = delivery.getNotification();
 316  
 
 317  
         /*
 318  
          * dismiss the message delivery
 319  
          */
 320  0
         notificationService.dismissNotificationMessageDelivery(delivery.getId(), user, action);
 321  
 
 322  0
         List<NotificationSender> senders = notification.getSenders();
 323  0
         List<NotificationRecipient> recipients = notification.getRecipients();
 324  
 
 325  0
         String contenthtml = Util.transformContent(notification);       
 326  
 
 327  
         // first check to see if this is a standalone window, b/c if it is, we'll want to close
 328  0
         if(standaloneWindow != null && standaloneWindow.equals("true")) {
 329  0
             view = "NotificationActionTakenCloseWindow";
 330  
         } else { // otherwise check to see if the details need to be rendered in line (no stuff around them)
 331  0
             if (command != null && command.equals(NotificationConstants.NOTIFICATION_DETAIL_VIEWS.INLINE)) { 
 332  0
                 view = "NotificationDetailInline";   
 333  
             }
 334  
         }
 335  
 
 336  0
         Map<String, Object> model = new HashMap<String, Object>();
 337  0
         model.put("notification", notification);
 338  0
         model.put("message", message);
 339  0
         model.put("senders", senders);
 340  0
         model.put("recipients", recipients);
 341  0
         model.put("contenthtml", contenthtml);
 342  0
         model.put("messageDeliveryId", messageDeliveryId);
 343  0
         model.put("command", command);
 344  0
         model.put(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW, standaloneWindow);
 345  0
         return new ModelAndView(view, model);
 346  
     }
 347  
 }