001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ken.web.spring;
017
018import org.apache.log4j.Logger;
019import org.kuali.rice.ken.bo.NotificationBo;
020import org.kuali.rice.ken.bo.NotificationContentTypeBo;
021import org.kuali.rice.ken.exception.ErrorList;
022import org.kuali.rice.ken.util.NotificationConstants;
023import org.kuali.rice.ken.util.Util;
024import org.springframework.web.servlet.ModelAndView;
025
026import javax.servlet.ServletException;
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029import java.io.IOException;
030import java.util.Date;
031import java.util.Map;
032
033/**
034 * This class is the controller for sending Event notification messages via an end user interface.
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038public class SendEventNotificationMessageController extends BaseSendNotificationController {
039
040    private static final Logger LOG = Logger.getLogger(SendEventNotificationMessageController.class);
041
042    /**
043     * Handles the display of the form for sending a event notification message.
044     *
045     * @param request : a servlet request
046     * @param response the servlet response
047     *
048     * @return the next view to move to
049     * @throws ServletException
050     * @throws IOException
051     */
052    public ModelAndView sendEventNotificationMessage(HttpServletRequest request,
053            HttpServletResponse response) throws ServletException, IOException {
054        String view = "SendEventNotificationMessage";
055
056        LOG.debug("remoteUser: " + request.getRemoteUser());
057
058        Map<String, Object> model = setupModelForSendNotification(request);
059        model.put("errors", new ErrorList()); // need an empty one so we don't have an NPE
060
061        return new ModelAndView(view, model);
062    }
063
064    /**
065     * Handles submitting the actual event notification message.
066     *
067     * @param request the servlet request
068     * @param response the servlet response
069     *
070     * @return the next view to move to
071     * @throws ServletException
072     * @throws IOException
073     */
074    public ModelAndView submitEventNotificationMessage(HttpServletRequest request,
075            HttpServletResponse response) throws ServletException, IOException {
076        String routeMessage = "This message was submitted via the event notification message submission form by user ";
077        String viewName = "SendEventNotificationMessage";
078
079        return submitNotificationMessage(request, routeMessage, viewName);
080    }
081
082    /**
083     * {@inheritDoc}
084     *
085     * Populates values pertaining to an event notification message.
086     */
087    @Override
088    protected Map<String, Object> setupModelForSendNotification(HttpServletRequest request) {
089        Map<String, Object> model = super.setupModelForSendNotification(request);
090
091        model.put("summary", request.getParameter("summary"));
092        model.put("description", request.getParameter("description"));
093        model.put("location", request.getParameter("location"));
094        model.put("startDateTime", request.getParameter("startDateTime"));
095        model.put("stopDateTime", request.getParameter("stopDateTime"));
096
097        return model;
098    }
099
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}