Coverage Report - org.kuali.rice.ken.web.spring.AdministerNotificationRequestController
 
Classes in this File Line Coverage Branch Coverage Complexity
AdministerNotificationRequestController
0%
0/97
0%
0/40
2.238
AdministerNotificationRequestController$AdministerNotificationRequestCommand
0%
0/20
N/A
2.238
 
 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.util.Date;
 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.commons.lang.StringUtils;
 28  
 import org.apache.log4j.Logger;
 29  
 import org.kuali.rice.ken.bo.Notification;
 30  
 import org.kuali.rice.ken.bo.NotificationChannelReviewer;
 31  
 import org.kuali.rice.ken.document.kew.NotificationWorkflowDocument;
 32  
 import org.kuali.rice.ken.service.NotificationMessageContentService;
 33  
 import org.kuali.rice.ken.service.NotificationRecipientService;
 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.api.WorkflowDocument;
 38  
 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
 39  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 40  
 import org.kuali.rice.kim.bo.Person;
 41  
 import org.kuali.rice.kim.util.KimConstants.KimGroupMemberTypes;
 42  
 import org.springframework.validation.BindException;
 43  
 import org.springframework.validation.ValidationUtils;
 44  
 import org.springframework.web.bind.ServletRequestBindingException;
 45  
 import org.springframework.web.servlet.ModelAndView;
 46  
 import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
 47  
 
 48  
 
 49  
 /**
 50  
  * Implements reviewer Approve/Disapprove and initiator Acknowledge of a Notification requests
 51  
  * sent to channels configured with reviewers
 52  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 53  
  */
 54  0
 public class AdministerNotificationRequestController extends MultiActionController {
 55  0
     private static final Logger LOG = Logger.getLogger(AdministerNotificationRequestController.class);
 56  
 
 57  
     /**
 58  
      * Command object for this controller
 59  
      */
 60  0
     public static class AdministerNotificationRequestCommand {
 61  
         // incoming
 62  
         private String docId;
 63  
 
 64  
         // outgoing
 65  
         private WorkflowDocument document;
 66  
         private Notification notification;
 67  
         private String renderedContent;
 68  0
         private boolean valid = true;
 69  
         private String message;
 70  
 
 71  
         public String getDocId() {
 72  0
             return docId;
 73  
         }
 74  
         public void setDocId(String docId) {
 75  0
             this.docId = docId;
 76  0
         }
 77  
         public WorkflowDocument getDocument() {
 78  0
             return document;
 79  
         }
 80  
         public void setDocument(WorkflowDocument document) {
 81  0
             this.document = document;
 82  0
         }
 83  
         public Notification getNotification() {
 84  0
             return notification;
 85  
         }
 86  
         public void setNotification(Notification notification) {
 87  0
             this.notification = notification;
 88  0
         }
 89  
         public String getRenderedContent() {
 90  0
             return renderedContent;
 91  
         }
 92  
         public void setRenderedContent(String renderedContent) {
 93  0
             this.renderedContent = renderedContent;
 94  0
         }
 95  
         public boolean isValid() {
 96  0
             return valid;
 97  
         }
 98  
         public void setValid(boolean valid) {
 99  0
             this.valid = valid;
 100  0
         }
 101  
         public String getMessage() {
 102  0
             return message;
 103  
         }
 104  
         public void setMessage(String message) {
 105  0
             this.message = message;
 106  0
         }
 107  
     }
 108  
 
 109  
     protected NotificationMessageContentService messageContentService;
 110  
     protected NotificationWorkflowDocumentService workflowDocumentService;
 111  
     protected NotificationRecipientService recipientService;
 112  
 
 113  
     /**
 114  
      * Sets the messageContentService attribute value.
 115  
      * @param messageContentService the NotificationMessageContentService impl
 116  
      */
 117  
     public void setMessageContentService(
 118  
             NotificationMessageContentService notificationMessageContentService) {
 119  0
         this.messageContentService = notificationMessageContentService;
 120  0
     }
 121  
 
 122  
     /**
 123  
      * Sets the workflowDocumentService attribute value.
 124  
      * @param workflowDocumentService the NotificationWorkflowDocumentService impl
 125  
      */
 126  
     public void setWorkflowDocumentService(
 127  
             NotificationWorkflowDocumentService notificationWorkflowDocumentService) {
 128  0
         this.workflowDocumentService = notificationWorkflowDocumentService;
 129  0
     }
 130  
 
 131  
     /**
 132  
      * Sets the recipientService attribute value.
 133  
      * @param recipientService the NotificationRecipientService impl
 134  
      */
 135  
     public void setRecipientService(
 136  
             NotificationRecipientService notificationRecipientService) {
 137  0
         this.recipientService = notificationRecipientService;
 138  0
     }
 139  
 
 140  
     /**
 141  
      * Parses the serialized Notification xml from the workflow document application content into a reconstituted
 142  
      * Notification BO
 143  
      * @param document the WorkflowDocument
 144  
      * @return a Notification BO reconstituted from the serialized XML form in the workflow document
 145  
      * @throws Exception if parsing fails
 146  
      */
 147  
     private Notification retrieveNotificationForWorkflowDocument(WorkflowDocument document) throws Exception {
 148  0
         String notificationAsXml = document.getApplicationContent();
 149  
 
 150  
         //parse out the application content into a Notification BO
 151  0
         Notification notification = messageContentService.parseSerializedNotificationXml(notificationAsXml.getBytes());
 152  
 
 153  0
         return notification;
 154  
     }
 155  
 
 156  
     /**
 157  
      * View action that displays an approve/disapprove/acknowledge view
 158  
      * @param request the HttpServletRequest
 159  
      * @param response the HttpServletResponse
 160  
      * @param command the command object bound for this MultiActionController
 161  
      * @return a view ModelAndView
 162  
      */
 163  
     public ModelAndView view(HttpServletRequest request, HttpServletResponse response, AdministerNotificationRequestCommand command) {
 164  
         // obtain a workflow user object first
 165  0
         String initiatorId = request.getRemoteUser();
 166  
 
 167  
         // now construct the workflow document, which will interact with workflow
 168  0
         if (command.getDocId() == null) {
 169  0
             throw new RuntimeException("An invalid document ID was recieved from KEW's action list.");
 170  
         }
 171  
 
 172  
         //check to see which view is being passed to us from the notification list - pop up or inline
 173  0
         String view = request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.COMMAND);
 174  0
         String standaloneWindow = "true";
 175  0
         if(view != null && view.equals(NotificationConstants.NOTIFICATION_DETAIL_VIEWS.INLINE)) {
 176  0
             standaloneWindow = "false";
 177  
         }
 178  
 
 179  
         WorkflowDocument document;
 180  0
         Map<String, Object> model = new HashMap<String, Object>();
 181  
         // set into model whether we are dealing with a pop up or an inline window
 182  0
         model.put(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW, standaloneWindow);
 183  
         try {
 184  0
             document = NotificationWorkflowDocument.loadNotificationDocument(initiatorId, command.getDocId());
 185  
 
 186  0
             Notification notification = retrieveNotificationForWorkflowDocument(document);
 187  
 
 188  
             // set up model
 189  0
             command.setDocument(document);
 190  0
             command.setNotification(notification);
 191  
             // render the event content according to registered XSLT stylesheet
 192  0
             command.setRenderedContent(Util.transformContent(notification));
 193  
 
 194  0
             LOG.info("notification auto remove date time: " + notification.getAutoRemoveDateTime());
 195  0
             if (document.isApproved()) {
 196  0
                 command.setValid(false);
 197  0
                 command.setMessage("This notification request has been approved.");
 198  0
             } else if (document.isDisapproved()) {
 199  0
                 command.setMessage("This notification request has been disapproved.");
 200  0
             } else if (notification.getAutoRemoveDateTime() != null && notification.getAutoRemoveDateTime().before(new Date(System.currentTimeMillis()))) {
 201  
                 /*if (!document.stateIsCanceled()) {
 202  
                 workflowDocumentService.terminateWorkflowDocument(new WorkflowDocument(new NetworkIdVO("notsys"), new Long(command.getDocId())));
 203  
                 }*/
 204  
                 // the autoremove date time has already passed...this notification request is null and void at this time
 205  0
                 boolean disapproved = document.isDisapproved();
 206  0
                 if (!document.isDisapproved()) {
 207  0
                     List<NotificationChannelReviewer> reviewers = notification.getChannel().getReviewers();
 208  0
                     String user = null;
 209  0
                     for (NotificationChannelReviewer reviewer: reviewers) {
 210  0
                         if (KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.equals(reviewer.getReviewerType())) {
 211  0
                             if (reviewer.getReviewerId().equals(request.getRemoteUser())) {
 212  0
                                 user = request.getRemoteUser();
 213  
                             }
 214  0
                         } else if (KimGroupMemberTypes.GROUP_MEMBER_TYPE.equals(reviewer.getReviewerType())) {
 215  
                             // if it's a group
 216  0
                             String[] members = recipientService.getGroupMembers(reviewer.getReviewerId());
 217  0
                             for (String member: members) {
 218  0
                                 if (StringUtils.equals(member, request.getRemoteUser())) {
 219  0
                                     user = request.getRemoteUser();
 220  0
                                     break;
 221  
                                 }
 222  
                             }
 223  0
                         }
 224  
                     }
 225  
                     // if the current user is a reviewer, then disapprove as that user
 226  0
                     if (user != null) {
 227  0
                             WorkflowDocumentFactory.loadDocument(user, command.getDocId()).disapprove("Disapproving notification request.  Auto-remove datetime has already passed.");
 228  0
                         disapproved = true;
 229  
                     }
 230  
                 }
 231  0
                 command.setValid(false);
 232  0
                 if (disapproved) {
 233  0
                     command.setMessage("This notification request is no longer valid because the Auto-Remove date has already passed.  It has been disapproved.  Please refresh your action list.");
 234  
                 } else {
 235  0
                     command.setMessage("This notification request is no longer valid because the Auto-Remove date has already passed.");
 236  
                 }
 237  
             }
 238  
 
 239  0
             model.put(getCommandName(command), command);
 240  0
         } catch (Exception e) {
 241  0
             throw new RuntimeException(e);
 242  0
         }
 243  
 
 244  0
         return new ModelAndView("ViewNotificationRequestDetails", model);
 245  
     }
 246  
 
 247  
     /**
 248  
      * Approve action that approves a notification request
 249  
      * @param request the HttpServletRequest
 250  
      * @param response the HttpServletResponse
 251  
      * @param command the command object bound for this MultiActionController
 252  
      * @return a view ModelAndView
 253  
      * @throws ServletException if an error occurs during approval
 254  
      */
 255  
     public ModelAndView approve(HttpServletRequest request, HttpServletResponse response, AdministerNotificationRequestCommand command) throws ServletException {
 256  0
         administerEventNotificationMessage(request, response, command, "approve");
 257  0
         Map<String, Object> model = new HashMap<String, Object>();
 258  0
         model.put("workflowActionTaken", "Approved");
 259  0
         model.put(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW, request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW));
 260  0
         return new ModelAndView("SendNotificationRequestActionTakenWindow", model);
 261  
     }
 262  
 
 263  
     /**
 264  
      * Disapprove action that disapproves a notification request
 265  
      * @param request the HttpServletRequest
 266  
      * @param response the HttpServletResponse
 267  
      * @param command the command object bound for this MultiActionController
 268  
      * @return a view ModelAndView
 269  
      * @throws ServletException if an error occurs during disapproval
 270  
      */
 271  
     public ModelAndView disapprove(HttpServletRequest request, HttpServletResponse response, AdministerNotificationRequestCommand command) throws ServletException {
 272  0
         administerEventNotificationMessage(request, response, command, "disapprove");
 273  0
         Map<String, Object> model = new HashMap<String, Object>();
 274  0
         model.put("workflowActionTaken", "Disapproved");
 275  0
         model.put(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW, request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW));
 276  0
         return new ModelAndView("SendNotificationRequestActionTakenWindow", model);
 277  
     }
 278  
 
 279  
     /**
 280  
      * Acknowledge action that acknowledges a notification request disapproval
 281  
      * @param request the HttpServletRequest
 282  
      * @param response the HttpServletResponse
 283  
      * @param command the command object bound for this MultiActionController
 284  
      * @return a view ModelAndView
 285  
      * @throws ServletException if an error occurs during acknowledgement
 286  
      */
 287  
     public ModelAndView acknowledge(HttpServletRequest request, HttpServletResponse response, AdministerNotificationRequestCommand command) throws ServletException {
 288  0
         administerEventNotificationMessage(request, response, command, "acknowledge");
 289  0
         Map<String, Object> model = new HashMap<String, Object>();
 290  0
         model.put(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW, request.getParameter(NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.STANDALONE_WINDOW));
 291  0
         model.put("workflowActionTaken", "Acknowledged");
 292  0
         return new ModelAndView("SendNotificationRequestActionTakenWindow", model);
 293  
     }
 294  
 
 295  
     /**
 296  
      * This method handles approval/disapproval/acknowledgement of the notification request
 297  
      * @param request the HttpServletRequest
 298  
      * @param response the HttpServletResponse
 299  
      * @param command the command object bound for this MultiActionController
 300  
      * @throws ServletException
 301  
      */
 302  
     private void administerEventNotificationMessage(HttpServletRequest request, HttpServletResponse response, AdministerNotificationRequestCommand command, String action) throws ServletException {
 303  0
         LOG.debug("remoteUser: " + request.getRemoteUser());
 304  
 
 305  0
         BindException bindException = new BindException(command, "command");
 306  0
         ValidationUtils.rejectIfEmpty(bindException, "docId", "Document id must be specified");
 307  0
         if (bindException.hasErrors()) {
 308  0
             throw new ServletRequestBindingException("Document id must be specified", bindException);
 309  
         }
 310  
 
 311  
         // obtain a workflow user object first
 312  
         //WorkflowIdDTO user = new WorkflowIdDTO(request.getRemoteUser());
 313  0
         String userId = request.getRemoteUser();
 314  
 
 315  
         try {
 316  
             // now construct the workflow document, which will interact with workflow
 317  0
             WorkflowDocument document = NotificationWorkflowDocument.loadNotificationDocument(userId, command.getDocId());
 318  
 
 319  0
             Notification notification = retrieveNotificationForWorkflowDocument(document);
 320  
 
 321  0
             String initiatorPrincipalId = document.getInitiatorPrincipalId();
 322  0
             Person initiator = KimApiServiceLocator.getPersonService().getPerson(initiatorPrincipalId);
 323  0
             String notificationBlurb =  notification.getContentType().getName() + " notification submitted by " + initiator.getName() + " for channel " + notification.getChannel().getName();
 324  0
             if ("disapprove".equals(action)) {
 325  0
                 document.disapprove("User " + userId + " disapproving " + notificationBlurb);
 326  0
             } else if ("approve".equals(action)) {
 327  0
                 document.approve("User " + userId + " approving " + notificationBlurb);
 328  0
             } else if ("acknowledge".equals(action)) {
 329  0
                 document.acknowledge("User " + userId + " acknowledging " + notificationBlurb);
 330  
             }
 331  0
         } catch (Exception e) {
 332  0
             LOG.error("Exception occurred taking action on notification request", e);
 333  0
             throw new ServletException("Exception occurred taking action on notification request", e);
 334  0
         }
 335  0
     }
 336  
 }