001/**
002 * Copyright 2005-2017 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.Map;
031
032import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
033
034/**
035 * This class is the controller for sending Simple notification messages via an end user interface.
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039public class SendNotificationMessageController extends BaseSendNotificationController {
040
041    private static final Logger LOG = Logger.getLogger(SendNotificationMessageController.class);
042
043    /**
044     * Handles the display of the form for sending a simple notification message.
045     *
046     * @param request : a servlet request
047     * @param response the servlet response
048     *
049     * @return the next view to move to
050     * @throws ServletException
051     * @throws IOException
052     */
053    public ModelAndView sendSimpleNotificationMessage(HttpServletRequest request, HttpServletResponse response)
054            throws ServletException, IOException {
055        String view = "SendSimpleNotificationMessage";
056
057        LOG.debug("remoteUser: " + request.getRemoteUser());
058
059        Map<String, Object> model = setupModelForSendNotification(request);
060        model.put("errors", new ErrorList()); // need an empty one so we don't have an NPE
061
062        return new ModelAndView(view, model);
063    }
064
065    /**
066     * Handles submitting the actual simple notification message.
067     *
068     * @param request the servlet request
069     * @param response the servlet response
070     *
071     * @return the next view to move to
072     * @throws ServletException
073     * @throws IOException
074     */
075    public ModelAndView submitSimpleNotificationMessage(HttpServletRequest request, HttpServletResponse response)
076            throws ServletException, IOException {
077        String routeMessage = "This message was submitted via the simple notification message submission form by user ";
078        String viewName = "SendSimpleNotificationMessage";
079
080        return submitNotificationMessage(request, routeMessage, viewName);
081    }
082
083    /**
084     * {@inheritDoc}
085     *
086     * Overrides to set the content type to "simple".
087     */
088    @Override
089    protected NotificationBo createNotification(HttpServletRequest request, Map<String, Object> model,
090            ErrorList errors) throws ErrorList {
091        NotificationBo notification = super.createNotification(request, model, errors);
092
093        String message = getParameter(request, "message", model, errors, "You must fill in a message.");
094
095        // stop processing if there are errors
096        if (!errors.getErrors().isEmpty()) {
097            throw errors;
098        }
099
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}