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