View Javadoc
1   /**
2    * Copyright 2005-2016 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.Date;
31  import java.util.Map;
32  
33  /**
34   * This class is the controller for sending Event notification messages via an end user interface.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class SendEventNotificationMessageController extends BaseSendNotificationController {
39  
40      private static final Logger LOG = Logger.getLogger(SendEventNotificationMessageController.class);
41  
42      /**
43       * Handles the display of the form for sending a event notification message.
44       *
45       * @param request : a servlet request
46       * @param response the servlet response
47       *
48       * @return the next view to move to
49       * @throws ServletException
50       * @throws IOException
51       */
52      public ModelAndView sendEventNotificationMessage(HttpServletRequest request,
53              HttpServletResponse response) throws ServletException, IOException {
54          String view = "SendEventNotificationMessage";
55  
56          LOG.debug("remoteUser: " + request.getRemoteUser());
57  
58          Map<String, Object> model = setupModelForSendNotification(request);
59          model.put("errors", new ErrorList()); // need an empty one so we don't have an NPE
60  
61          return new ModelAndView(view, model);
62      }
63  
64      /**
65       * Handles submitting the actual event notification message.
66       *
67       * @param request the servlet request
68       * @param response the servlet response
69       *
70       * @return the next view to move to
71       * @throws ServletException
72       * @throws IOException
73       */
74      public ModelAndView submitEventNotificationMessage(HttpServletRequest request,
75              HttpServletResponse response) throws ServletException, IOException {
76          String routeMessage = "This message was submitted via the event notification message submission form by user ";
77          String viewName = "SendEventNotificationMessage";
78  
79          return submitNotificationMessage(request, routeMessage, viewName);
80      }
81  
82      /**
83       * {@inheritDoc}
84       *
85       * Populates values pertaining to an event notification message.
86       */
87      @Override
88      protected Map<String, Object> setupModelForSendNotification(HttpServletRequest request) {
89          Map<String, Object> model = super.setupModelForSendNotification(request);
90  
91          model.put("summary", request.getParameter("summary"));
92          model.put("description", request.getParameter("description"));
93          model.put("location", request.getParameter("location"));
94          model.put("startDateTime", request.getParameter("startDateTime"));
95          model.put("stopDateTime", request.getParameter("stopDateTime"));
96  
97          return model;
98      }
99  
100     /**
101      * {@inheritDoc}
102      *
103      * Overrides to set the content type to "event" and add extra attributes.
104      */
105     @Override
106     protected NotificationBo createNotification(HttpServletRequest request, Map<String, Object> model,
107             ErrorList errors) throws ErrorList {
108         NotificationBo notification = super.createNotification(request, model, errors);
109 
110         String message = getParameter(request, "message", model, errors, "You must fill in a message.");
111 
112         String summary = getParameter(request, "summary", model, errors, "You must fill in a summary.");
113         String description = getParameter(request, "description", model, errors, "You must fill in a description.");
114         String location = getParameter(request, "location", model, errors, "You must fill in a location.");
115 
116         String startDateTime = request.getParameter("startDateTime");
117         Date startDate = getDate(startDateTime, errors,
118                 "You specified an invalid start date and time.  Please use the calendar picker.");
119         if (startDate != null) {
120             model.put("startDateTime", startDateTime);
121         }
122 
123         String stopDateTime = request.getParameter("stopDateTime");
124         Date stopDate = getDate(stopDateTime, errors,
125                 "You specified an invalid start date and time.  Please use the calendar picker.");
126         if (stopDate != null) {
127             model.put("stopDateTime", stopDateTime);
128         } else {
129 
130         }
131 
132         if (stopDate != null && startDate != null) {
133             if (stopDate.before(startDate)) {
134                 errors.addError("Event Stop Date/Time cannot be before Event Start Date/Time.");
135             }
136         }
137 
138         // stop processing if there are errors
139         if (!errors.getErrors().isEmpty()) {
140             throw errors;
141         }
142 
143         NotificationContentTypeBo contentType = Util.retrieveFieldReference("contentType", "name",
144                 NotificationConstants.CONTENT_TYPES.EVENT_CONTENT_TYPE, NotificationContentTypeBo.class,
145                 dataObjectService, Boolean.TRUE);
146         notification.setContentType(contentType);
147 
148         notification.setContent(NotificationConstants.XML_MESSAGE_CONSTANTS.CONTENT_EVENT_OPEN
149                 + NotificationConstants.XML_MESSAGE_CONSTANTS.MESSAGE_OPEN
150                 + message
151                 + NotificationConstants.XML_MESSAGE_CONSTANTS.MESSAGE_CLOSE
152                 + "<event>\n"
153                 + "  <summary>"
154                 + summary
155                 + "</summary>\n"
156                 + "  <description>"
157                 + description
158                 + "</description>\n"
159                 + "  <location>"
160                 + location
161                 + "</location>\n"
162                 + "  <startDateTime>"
163                 + Util.toUIDateTimeString(startDate)
164                 + "</startDateTime>\n"
165                 + "  <stopDateTime>"
166                 + Util.toUIDateTimeString(stopDate)
167                 + "</stopDateTime>\n"
168                 + "</event>"
169                 + NotificationConstants.XML_MESSAGE_CONSTANTS.CONTENT_CLOSE);
170 
171         return notification;
172     }
173 }