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.krad.web.bind;
017
018import org.apache.log4j.Logger;
019import org.kuali.rice.krad.UserSession;
020import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
021import org.kuali.rice.krad.uif.view.History;
022import org.kuali.rice.krad.uif.view.HistoryEntry;
023import org.kuali.rice.krad.uif.service.ViewService;
024import org.kuali.rice.krad.uif.util.UifWebUtils;
025import org.kuali.rice.krad.util.GlobalVariables;
026import org.kuali.rice.krad.util.KRADConstants;
027import org.kuali.rice.krad.util.KRADUtils;
028import org.kuali.rice.krad.web.form.DocumentFormBase;
029import org.kuali.rice.krad.web.form.IncidentReportForm;
030import org.kuali.rice.krad.web.form.UifFormBase;
031import org.springframework.web.servlet.ModelAndView;
032
033import javax.servlet.http.HttpServletRequest;
034import javax.servlet.http.HttpServletResponse;
035
036/**
037 * Spring Exception intercepter
038 *
039 * <p>
040 * Gets the data needed for the incident report from the request and builds the
041 * model and view for the incident report. This resolver intercepts any unhandled 
042 * exception.
043 * </p>
044 * 
045 * @author Kuali Rice Team (rice.collab@kuali.org)
046 */
047public class UifHandlerExceptionResolver implements org.springframework.web.servlet.HandlerExceptionResolver {
048    private static final Logger LOG = Logger.getLogger(UifHandlerExceptionResolver.class);
049
050    /**
051     * Builds the incident report model and view from the request that threw the exception
052     * 
053     * @param request -
054     *            the request
055     * @param response -
056     *            the response
057     * @param handler -
058     *            the current handler when the exception occurred
059     * @param ex -
060     *            the exception
061     * @return the incident report model and view
062     * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest,
063     *      javax.servlet.http.HttpServletResponse, java.lang.Object,
064     *      java.lang.Exception)
065     */
066    @Override
067    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
068            Exception ex) {    
069        LOG.error("The following error was caught by the UifHandlerExceptionResolver : ", ex);
070
071        // log exception
072        LOG.error(ex.getMessage(), ex);
073
074        String incidentDocId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
075        String incidentViewId = "";
076
077        UifFormBase form = GlobalVariables.getUifFormManager().getCurrentForm();
078        if (form instanceof DocumentFormBase) {
079            if (((DocumentFormBase) form).getDocument() != null) {
080                incidentDocId = ((DocumentFormBase) form).getDocument().getDocumentNumber();
081            }
082            incidentViewId = ((DocumentFormBase) form).getViewId();
083        }
084        GlobalVariables.getUifFormManager().removeForm(form);
085
086        UserSession userSession = (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
087        IncidentReportForm incidentReportForm = new IncidentReportForm();
088
089        // Set the post url map to the incident report controller and not 
090        // the one the exception occurred on
091        String postUrl = request.getRequestURL().toString();        
092        postUrl = postUrl.substring(0, postUrl.lastIndexOf("/")) + "/incidentReport";
093        incidentReportForm.setFormPostUrl(postUrl);
094
095        incidentReportForm.setException(ex);
096        incidentReportForm.setIncidentDocId(incidentDocId);
097        incidentReportForm.setIncidentViewId(incidentViewId);
098        incidentReportForm.setController(handler.getClass().toString());
099        incidentReportForm.setUserId(userSession.getPrincipalId());
100        incidentReportForm.setUserName(userSession.getPrincipalName());
101        incidentReportForm.setUserEmail(userSession.getPerson().getEmailAddress());
102        incidentReportForm.setDevMode(!KRADUtils.isProductionEnvironment());
103        incidentReportForm.setViewId("Uif-IncidentReportView");
104
105        // Set the view object
106        incidentReportForm.setView(getViewService().getViewById("Uif-IncidentReportView"));
107
108        // Add a new History entry to avoid errors in the postHandle
109        History history = new History();
110        HistoryEntry entry = new HistoryEntry("", "", "Incident Report", "", "");
111        history.setCurrent(entry);
112        incidentReportForm.setFormHistory(history);
113
114        // Set render full view to force full render
115        incidentReportForm.setRenderFullView(true);
116
117        ModelAndView modelAndView = UifWebUtils.getUIFModelAndView(incidentReportForm, "");
118        try {
119            UifWebUtils.postControllerHandle(request, response, handler, modelAndView);
120        } catch (Exception e) {
121            LOG.error("An error stopped the incident form from loading", e);
122        }
123
124        GlobalVariables.getUifFormManager().setCurrentForm(incidentReportForm);
125
126        return modelAndView;
127    }
128
129    protected ViewService getViewService() {
130        return KRADServiceLocatorWeb.getViewService();
131    }
132
133}