View Javadoc
1   /**
2    * Copyright 2005-2014 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 org.apache.log4j.Logger;
19  import org.kuali.rice.ken.bo.NotificationBo;
20  import org.kuali.rice.ken.bo.NotificationContentTypeBo;
21  import org.kuali.rice.ken.exception.ErrorList;
22  import org.kuali.rice.ken.util.NotificationConstants;
23  import org.kuali.rice.ken.util.Util;
24  import org.springframework.web.servlet.ModelAndView;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import java.io.IOException;
30  import java.util.Map;
31  
32  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
33  
34  /**
35   * This class is the controller for sending Simple notification messages via an end user interface.
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public class SendNotificationMessageController extends BaseSendNotificationController {
40  
41      private static final Logger LOG = Logger.getLogger(SendNotificationMessageController.class);
42  
43      /**
44       * Handles the display of the form for sending a simple notification message.
45       *
46       * @param request : a servlet request
47       * @param response the servlet response
48       *
49       * @return the next view to move to
50       * @throws ServletException
51       * @throws IOException
52       */
53      public ModelAndView sendSimpleNotificationMessage(HttpServletRequest request, HttpServletResponse response)
54              throws ServletException, IOException {
55          String view = "SendSimpleNotificationMessage";
56  
57          LOG.debug("remoteUser: " + request.getRemoteUser());
58  
59          Map<String, Object> model = setupModelForSendNotification(request);
60          model.put("errors", new ErrorList()); // need an empty one so we don't have an NPE
61  
62          return new ModelAndView(view, model);
63      }
64  
65      /**
66       * Handles submitting the actual simple notification message.
67       *
68       * @param request the servlet request
69       * @param response the servlet response
70       *
71       * @return the next view to move to
72       * @throws ServletException
73       * @throws IOException
74       */
75      public ModelAndView submitSimpleNotificationMessage(HttpServletRequest request, HttpServletResponse response)
76              throws ServletException, IOException {
77          String routeMessage = "This message was submitted via the simple notification message submission form by user ";
78          String viewName = "SendSimpleNotificationMessage";
79  
80          return submitNotificationMessage(request, routeMessage, viewName);
81      }
82  
83      /**
84       * {@inheritDoc}
85       *
86       * Overrides to set the content type to "simple".
87       */
88      @Override
89      protected NotificationBo createNotification(HttpServletRequest request, Map<String, Object> model,
90              ErrorList errors) throws ErrorList {
91          NotificationBo notification = super.createNotification(request, model, errors);
92  
93          String message = getParameter(request, "message", model, errors, "You must fill in a message.");
94  
95          // stop processing if there are errors
96          if (!errors.getErrors().isEmpty()) {
97              throw errors;
98          }
99  
100         NotificationContentTypeBo contentType = Util.retrieveFieldReference("contentType", "name",
101                 NotificationConstants.CONTENT_TYPES.SIMPLE_CONTENT_TYPE, NotificationContentTypeBo.class,
102                 dataObjectService, Boolean.TRUE);
103         notification.setContentType(contentType);
104 
105         notification.setContent(NotificationConstants.XML_MESSAGE_CONSTANTS.CONTENT_SIMPLE_OPEN
106                 + NotificationConstants.XML_MESSAGE_CONSTANTS.MESSAGE_OPEN
107                 + message
108                 + NotificationConstants.XML_MESSAGE_CONSTANTS.MESSAGE_CLOSE
109                 + NotificationConstants.XML_MESSAGE_CONSTANTS.CONTENT_CLOSE);
110 
111         return notification;
112     }
113 }