View Javadoc

1   /**
2    * Copyright 2005-2013 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.krad.web.controller;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.krad.UserSession;
21  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
22  import org.kuali.rice.krad.uif.UifConstants;
23  import org.kuali.rice.krad.uif.UifParameters;
24  import org.kuali.rice.krad.uif.view.History;
25  import org.kuali.rice.krad.uif.view.HistoryEntry;
26  import org.kuali.rice.krad.uif.service.ViewService;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  import org.kuali.rice.krad.util.KRADConstants;
29  import org.kuali.rice.krad.util.KRADUtils;
30  import org.kuali.rice.krad.web.form.DocumentFormBase;
31  import org.kuali.rice.krad.web.form.IncidentReportForm;
32  import org.kuali.rice.krad.web.form.UifFormBase;
33  import org.springframework.web.servlet.ModelAndView;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  /**
39   * Spring Exception intercepter
40   *
41   * <p>
42   * Gets the data needed for the incident report from the request and builds the
43   * model and view for the incident report. This resolver intercepts any unhandled
44   * exception.
45   * </p>
46   *
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  public class UifHandlerExceptionResolver implements org.springframework.web.servlet.HandlerExceptionResolver {
50      private static final Logger LOG = Logger.getLogger(UifHandlerExceptionResolver.class);
51  
52      /**
53       * Builds the incident report model and view from the request that threw the exception
54       *
55       * @param request -
56       *            the request
57       * @param response -
58       *            the response
59       * @param handler -
60       *            the current handler when the exception occurred
61       * @param ex -
62       *            the exception
63       * @return the incident report model and view
64       * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest,
65       *      javax.servlet.http.HttpServletResponse, java.lang.Object,
66       *      java.lang.Exception)
67       */
68      @Override
69      public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
70              Exception ex) {
71          LOG.error("The following error was caught by the UifHandlerExceptionResolver : ", ex);
72  
73          // log exception
74          LOG.error(ex.getMessage(), ex);
75  
76          String incidentDocId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
77          String incidentViewId = "";
78  
79          UifFormBase form = (UifFormBase)request.getAttribute(UifConstants.REQUEST_FORM);
80          if (form instanceof DocumentFormBase) {
81              if (((DocumentFormBase) form).getDocument() != null) {
82                  incidentDocId = ((DocumentFormBase) form).getDocument().getDocumentNumber();
83              }
84              incidentViewId = ((DocumentFormBase) form).getViewId();
85          }
86          GlobalVariables.getUifFormManager().removeSessionForm(form);
87  
88          UserSession userSession = (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
89          IncidentReportForm incidentReportForm = new IncidentReportForm();
90  
91          // Set the post url map to the incident report controller and not 
92          // the one the exception occurred on
93          String postUrl = request.getRequestURL().toString();
94          postUrl = postUrl.substring(0, postUrl.lastIndexOf("/")) + "/incidentReport";
95          incidentReportForm.setFormPostUrl(postUrl);
96  
97          incidentReportForm.setException(ex);
98          incidentReportForm.setIncidentDocId(incidentDocId);
99          incidentReportForm.setIncidentViewId(incidentViewId);
100         incidentReportForm.setController(handler.getClass().toString());
101         incidentReportForm.setUserId(userSession.getPrincipalId());
102         incidentReportForm.setUserName(userSession.getPrincipalName());
103         incidentReportForm.setUserEmail(userSession.getPerson().getEmailAddress());
104         incidentReportForm.setDevMode(!KRADUtils.isProductionEnvironment());
105         incidentReportForm.setViewId("Uif-IncidentReportView");
106 
107         if (form != null) {
108             incidentReportForm.setAjaxRequest(form.isAjaxRequest());
109         } else {
110             String ajaxRequestParm = request.getParameter(UifParameters.AJAX_REQUEST);
111             if (StringUtils.isNotBlank(ajaxRequestParm)) {
112                 incidentReportForm.setAjaxRequest(Boolean.parseBoolean(ajaxRequestParm));
113             }
114         }
115 
116         // Set the view object
117         incidentReportForm.setView(getViewService().getViewById("Uif-IncidentReportView"));
118 
119         // Add a new History entry to avoid errors in the postHandle
120         History history = new History();
121         HistoryEntry entry = new HistoryEntry("", "", "Incident Report", "", "");
122         history.setCurrent(entry);
123         incidentReportForm.setFormHistory(history);
124 
125         // Set the ajax return type
126         incidentReportForm.setAjaxReturnType(UifConstants.AjaxReturnTypes.UPDATEVIEW.getKey());
127 
128         ModelAndView modelAndView = UifControllerHelper.getUIFModelAndView(incidentReportForm, "");
129         try {
130             UifControllerHelper.postControllerHandle(request, response, handler, modelAndView);
131         } catch (Exception e) {
132             LOG.error("An error stopped the incident form from loading", e);
133         }
134 
135         return modelAndView;
136     }
137 
138     protected ViewService getViewService() {
139         return KRADServiceLocatorWeb.getViewService();
140     }
141 
142 }