View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation Licensed under the Educational Community
3    * License, Version 1.0 (the "License"); you may not use this file except in
4    * compliance with the License. You may obtain a copy of the License at
5    * http://www.opensource.org/licenses/ecl1.php Unless required by applicable law
6    * or agreed to in writing, software distributed under the License is
7    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8    * KIND, either express or implied. See the License for the specific language
9    * governing permissions and limitations under the License.
10   */
11  package org.kuali.rice.krad.web.bind;
12  
13  import org.apache.log4j.Logger;
14  import org.kuali.rice.krad.UserSession;
15  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
16  import org.kuali.rice.krad.service.SessionDocumentService;
17  import org.kuali.rice.krad.uif.view.History;
18  import org.kuali.rice.krad.uif.view.HistoryEntry;
19  import org.kuali.rice.krad.uif.service.ViewService;
20  import org.kuali.rice.krad.uif.util.UifWebUtils;
21  import org.kuali.rice.krad.util.KRADConstants;
22  import org.kuali.rice.krad.util.KRADUtils;
23  import org.kuali.rice.krad.web.form.DocumentFormBase;
24  import org.kuali.rice.krad.web.form.IncidentReportForm;
25  import org.kuali.rice.krad.web.form.UifFormBase;
26  import org.springframework.web.servlet.ModelAndView;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  /**
32   * Spring Exception intercepter
33   *
34   * <p>
35   * Gets the data needed for the incident report from the request and builds the
36   * model and view for the incident report. This resolver intercepts any unhandled 
37   * exception.
38   * </p>
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class UifHandlerExceptionResolver implements org.springframework.web.servlet.HandlerExceptionResolver {
43  
44      private static final Logger LOG = Logger.getLogger(UifHandlerExceptionResolver.class);
45  
46      /**
47       * Builds the incident report model and view from the
48       * request that threw the exception.
49       * 
50       * @param request
51       *            the request
52       * @param response
53       *            the response
54       * @param handler
55       *            the current handler when the exception occurred
56       * @param ex
57       *            the exception
58       * @return the incident report model and view
59       * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest,
60       *      javax.servlet.http.HttpServletResponse, java.lang.Object,
61       *      java.lang.Exception)
62       */
63      @Override
64      public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
65              Exception ex) {    
66          LOG.error("The following error was caught by the UifHandlerExceptionResolver : ", ex);        
67          String incidentDocId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
68          String incidentViewId = "";
69  
70          // log exception
71          LOG.error(ex.getMessage(), ex);
72  
73          UifFormBase form = UifWebUtils.getFormFromRequest(request);
74          if (form instanceof DocumentFormBase) {
75              incidentDocId = ((DocumentFormBase) form).getDocument().getDocumentNumber();
76              incidentViewId = ((DocumentFormBase) form).getViewId();
77          }
78  
79          UserSession userSession = (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
80          IncidentReportForm incidentReportForm = new IncidentReportForm();
81          // Set the post url map to the incident report controller and not 
82          // the one the exception occurred on
83          String postUrl = request.getRequestURL().toString();        
84          postUrl = postUrl.substring(0, postUrl.lastIndexOf("/")) + "/incidentReport";
85          incidentReportForm.setFormPostUrl(postUrl);
86          incidentReportForm.setException(ex);
87          incidentReportForm.setIncidentDocId(incidentDocId);
88          incidentReportForm.setIncidentViewId(incidentViewId);
89          incidentReportForm.setController(handler.getClass().toString());
90          incidentReportForm.setUserId(userSession.getPrincipalId());
91          incidentReportForm.setUserName(userSession.getPrincipalName());
92          incidentReportForm.setUserEmail(userSession.getPerson().getEmailAddress());
93          incidentReportForm.setDevMode(!KRADUtils.isProductionEnvironment());
94  
95          // Set the view object
96          incidentReportForm.setView(getViewService().getView("Incident-Report",
97                  incidentReportForm.getViewRequestParameters()));
98  
99          // Add a new History entry to avoid errors in the postHandle
100         History history = new History();
101         HistoryEntry entry = new HistoryEntry("", "", "Incident Report", "", "");
102         history.setCurrent(entry);
103         incidentReportForm.setFormHistory(history);
104 
105         // Set render full view to force full render
106         incidentReportForm.setRenderFullView(true);
107 
108         ModelAndView modelAndView = UifWebUtils.getUIFModelAndView(incidentReportForm, "Incident-Report", "");
109         try {
110             UifWebUtils.postControllerHandle(request, response, handler, modelAndView);
111         } catch (Exception e) {
112             LOG.error("An error stopped the incident form from loading", e);
113         }
114 
115         return modelAndView;
116     }
117 
118     protected ViewService getViewService() {
119         return KRADServiceLocatorWeb.getViewService();
120     }
121 
122     protected SessionDocumentService getSessionDocumentService() {
123         return KRADServiceLocatorWeb.getSessionDocumentService();
124     }
125 
126 }