|  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.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 |     | 
     | 
  |  51 |     | 
     | 
  |  52 |     | 
     | 
  |  53 |     | 
     | 
  |  54 |    0 |    public class AdministerNotificationRequestController extends MultiActionController { | 
  |  55 |    0 |        private static final Logger LOG = Logger.getLogger(AdministerNotificationRequestController.class);  | 
  |  56 |     | 
     | 
  |  57 |     | 
         | 
  |  58 |     | 
     | 
  |  59 |     | 
     | 
  |  60 |    0 |        public static class AdministerNotificationRequestCommand { | 
  |  61 |     | 
             | 
  |  62 |     | 
           private String docId;  | 
  |  63 |     | 
     | 
  |  64 |     | 
             | 
  |  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 |     | 
     | 
  |  115 |     | 
     | 
  |  116 |     | 
     | 
  |  117 |     | 
       public void setMessageContentService(  | 
  |  118 |     | 
               NotificationMessageContentService notificationMessageContentService) { | 
  |  119 |    0 |            this.messageContentService = notificationMessageContentService;  | 
  |  120 |    0 |        }  | 
  |  121 |     | 
     | 
  |  122 |     | 
         | 
  |  123 |     | 
     | 
  |  124 |     | 
     | 
  |  125 |     | 
     | 
  |  126 |     | 
       public void setWorkflowDocumentService(  | 
  |  127 |     | 
               NotificationWorkflowDocumentService notificationWorkflowDocumentService) { | 
  |  128 |    0 |            this.workflowDocumentService = notificationWorkflowDocumentService;  | 
  |  129 |    0 |        }  | 
  |  130 |     | 
     | 
  |  131 |     | 
         | 
  |  132 |     | 
     | 
  |  133 |     | 
     | 
  |  134 |     | 
     | 
  |  135 |     | 
       public void setRecipientService(  | 
  |  136 |     | 
               NotificationRecipientService notificationRecipientService) { | 
  |  137 |    0 |            this.recipientService = notificationRecipientService;  | 
  |  138 |    0 |        }  | 
  |  139 |     | 
     | 
  |  140 |     | 
         | 
  |  141 |     | 
     | 
  |  142 |     | 
     | 
  |  143 |     | 
     | 
  |  144 |     | 
     | 
  |  145 |     | 
     | 
  |  146 |     | 
     | 
  |  147 |     | 
       private Notification retrieveNotificationForWorkflowDocument(WorkflowDocument document) throws Exception { | 
  |  148 |    0 |            String notificationAsXml = document.getApplicationContent();  | 
  |  149 |     | 
     | 
  |  150 |     | 
             | 
  |  151 |    0 |            Notification notification = messageContentService.parseSerializedNotificationXml(notificationAsXml.getBytes());  | 
  |  152 |     | 
     | 
  |  153 |    0 |            return notification;  | 
  |  154 |     | 
       }  | 
  |  155 |     | 
     | 
  |  156 |     | 
         | 
  |  157 |     | 
     | 
  |  158 |     | 
     | 
  |  159 |     | 
     | 
  |  160 |     | 
     | 
  |  161 |     | 
     | 
  |  162 |     | 
     | 
  |  163 |     | 
       public ModelAndView view(HttpServletRequest request, HttpServletResponse response, AdministerNotificationRequestCommand command) { | 
  |  164 |     | 
             | 
  |  165 |    0 |            String initiatorId = request.getRemoteUser();  | 
  |  166 |     | 
     | 
  |  167 |     | 
             | 
  |  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 |     | 
             | 
  |  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 |     | 
             | 
  |  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 |     | 
                 | 
  |  189 |    0 |                command.setDocument(document);  | 
  |  190 |    0 |                command.setNotification(notification);  | 
  |  191 |     | 
                 | 
  |  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 |     | 
                     | 
  |  202 |     | 
     | 
  |  203 |     | 
     | 
  |  204 |     | 
                     | 
  |  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 |     | 
                                 | 
  |  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 |     | 
                         | 
  |  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 |     | 
     | 
  |  249 |     | 
     | 
  |  250 |     | 
     | 
  |  251 |     | 
     | 
  |  252 |     | 
     | 
  |  253 |     | 
     | 
  |  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 |     | 
     | 
  |  265 |     | 
     | 
  |  266 |     | 
     | 
  |  267 |     | 
     | 
  |  268 |     | 
     | 
  |  269 |     | 
     | 
  |  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 |     | 
     | 
  |  281 |     | 
     | 
  |  282 |     | 
     | 
  |  283 |     | 
     | 
  |  284 |     | 
     | 
  |  285 |     | 
     | 
  |  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 |     | 
     | 
  |  297 |     | 
     | 
  |  298 |     | 
     | 
  |  299 |     | 
     | 
  |  300 |     | 
     | 
  |  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 |     | 
             | 
  |  312 |     | 
             | 
  |  313 |    0 |            String userId = request.getRemoteUser();  | 
  |  314 |     | 
     | 
  |  315 |     | 
           try { | 
  |  316 |     | 
                 | 
  |  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 |     | 
   }  |