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