View Javadoc

1   /**
2    * Copyright 2005-2011 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.uif.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.core.api.exception.RiceRuntimeException;
21  import org.kuali.rice.krad.UserSession;
22  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
23  import org.kuali.rice.krad.service.SessionDocumentService;
24  import org.kuali.rice.krad.uif.UifConstants;
25  import org.kuali.rice.krad.uif.UifParameters;
26  import org.kuali.rice.krad.uif.view.History;
27  import org.kuali.rice.krad.uif.view.View;
28  import org.kuali.rice.krad.uif.component.Component;
29  import org.kuali.rice.krad.uif.service.ViewService;
30  import org.kuali.rice.krad.util.KRADConstants;
31  import org.kuali.rice.krad.util.KRADUtils;
32  import org.kuali.rice.krad.web.controller.UifControllerBase;
33  import org.kuali.rice.krad.web.form.DocumentFormBase;
34  import org.kuali.rice.krad.web.form.UifFormBase;
35  import org.kuali.rice.krad.uif.UifConstants.ViewType;
36  import org.springframework.web.servlet.ModelAndView;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  import java.util.Map;
41  
42  /**
43   * Provides helper methods that will be used during the request lifecycle
44   *
45   * <p>
46   * Created to avoid duplication of the methods used by the UifHandlerExceptionResolver
47   * </p>
48   *
49   * @author Kuali Rice Team (rice.collab@kuali.org)
50   */
51  public class UifWebUtils {
52      private static final Logger LOG = Logger.getLogger(UifWebUtils.class);
53  
54      /**
55       * Gets the form from the request
56       *
57       * <p>
58       * Looks for the form on the session by using the form key. If the form is not
59       * on the session it will attempt to get it from the database.
60       * </p>
61       *
62       * @param request the http request
63       * @return the form from request
64       */
65      public static UifFormBase getFormFromRequest(HttpServletRequest request) {
66          UifFormBase form = null;
67  
68          String formKeyParam = request.getParameter(UifParameters.FORM_KEY);
69          if (StringUtils.isBlank(formKeyParam)) {
70              formKeyParam = (String) request.getAttribute(UifParameters.FORM_KEY);
71          }
72  
73          String docId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
74          if (StringUtils.isNotBlank(formKeyParam)) {
75              form = (UifFormBase) request.getSession().getAttribute(formKeyParam);
76              // retrieve from db if form not in session
77              if (form == null) {
78                  UserSession userSession = (UserSession) request.getSession().getAttribute(
79                          KRADConstants.USER_SESSION_KEY);
80                  form = KRADServiceLocatorWeb.getSessionDocumentService().getDocumentForm(docId, formKeyParam,
81                          userSession, request.getRemoteAddr());
82              }
83          }
84  
85          return form;
86      }
87  
88      /**
89       * Configures the <code>ModelAndView</code> instance containing the form
90       * data and pointing to the UIF generic spring view
91       *
92       * @param form - Form instance containing the model data
93       * @param pageId - Id of the page within the view that should be rendered, can
94       * be left blank in which the current or default page is rendered
95       * @return ModelAndView object with the contained form
96       */
97      public static ModelAndView getUIFModelAndView(UifFormBase form, String pageId) {
98          if (StringUtils.isNotBlank(pageId)) {
99              form.setPageId(pageId);
100         }
101 
102         // create the spring return object pointing to View.jsp
103         ModelAndView modelAndView = new ModelAndView();
104         modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, form);
105         modelAndView.setViewName(UifConstants.SPRING_VIEW_ID);
106 
107         return modelAndView;
108     }
109 
110     public static ModelAndView getComponentModelAndView(Component component, Object model) {
111         ModelAndView modelAndView = new ModelAndView();
112         modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, model);
113         modelAndView.addObject("Component", component);
114         modelAndView.setViewName("ComponentUpdate");
115 
116         return modelAndView;
117     }
118 
119     /**
120      * After the controller logic is executed, the form is placed into session
121      * and the corresponding view is prepared for rendering
122      */
123     public static void postControllerHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
124             ModelAndView modelAndView) throws Exception {
125         if (handler instanceof UifControllerBase && (modelAndView != null)) {
126             UifControllerBase controller = (UifControllerBase) handler;
127             UifFormBase form = null;
128 
129             // check to see if this is a full view request
130             if (modelAndView.getViewName().equals(UifConstants.SPRING_VIEW_ID)) {
131                 Object model = modelAndView.getModelMap().get(UifConstants.DEFAULT_MODEL_NAME);
132                 if (model instanceof UifFormBase) {
133                     form = (UifFormBase) model;
134 
135                     form.setPreviousView(null);
136 
137                     // persist document form to db
138                     if (form instanceof DocumentFormBase) {
139                         UserSession userSession = (UserSession) request.getSession().getAttribute(
140                                 KRADConstants.USER_SESSION_KEY);
141                         getSessionDocumentService().setDocumentForm((DocumentFormBase) form, userSession,
142                                 request.getRemoteAddr());
143                     }
144 
145                     // perform authorization of controller method
146                     checkMethodToCallAuthorization(request, controller, form);
147 
148                     // prepare view instance
149                     prepareViewForRendering(request, form);
150 
151                     // update history for view
152                     prepareHistory(request, form);
153                 }
154             }
155         }
156     }
157 
158     /**
159      * Verify the user is authorized to invoke the controller method according
160      * to the module that owns the functionality. This is done post handle to be
161      * able to access the form and whatever processing was done TODO: should
162      * this be throwing some exception?
163      *
164      * @param request - current HTTP request containing method to call parameter
165      * @param controller - controller that was invoked
166      * @param form - form instance containing the data
167      */
168     public static void checkMethodToCallAuthorization(HttpServletRequest request, UifControllerBase controller,
169             UifFormBase form) {
170         // currently methodToCall must be a regularly parseable request
171         // parameter, so just get from request
172         String methodToCall = request.getParameter(KRADConstants.DISPATCH_REQUEST_PARAMETER);
173 
174         if (!controller.getMethodToCallsToNotCheckAuthorization().contains(methodToCall)) {
175             if (LOG.isDebugEnabled()) {
176                 LOG.debug("'" + methodToCall + "' not in set of excempt methods: " + controller
177                         .getMethodToCallsToNotCheckAuthorization());
178             }
179 
180             controller.checkAuthorization(form, methodToCall);
181         } else {
182             if (LOG.isDebugEnabled()) {
183                 LOG.debug("'" + methodToCall + "' is exempt from auth checks.");
184             }
185         }
186     }
187 
188     /**
189      * Updates the history object (or constructs a new History) for the view we are getting ready
190      * to render
191      *
192      * @param request - Http request object containing the request parameters
193      * @param form - object containing the view data
194      */
195     public static void prepareHistory(HttpServletRequest request, UifFormBase form) {
196         View view = form.getView();
197 
198         // main history/breadcrumb tracking support
199         History history = form.getFormHistory();
200         if (history == null || request.getMethod().equals("GET")) {
201             history = new History();
202             history.setHomewardPath(view.getBreadcrumbs().getHomewardPathList());
203             history.setAppendHomewardPath(view.getBreadcrumbs().isDisplayHomewardPath());
204             history.setAppendPassedHistory(view.getBreadcrumbs().isDisplayPassedHistory());
205 
206             // passed settings ALWAYS override the defaults
207             if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HOME))) {
208                 history.setAppendHomewardPath(Boolean.parseBoolean(request.getParameter(
209                         UifConstants.UrlParams.SHOW_HOME)));
210             }
211 
212             if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HISTORY))) {
213                 history.setAppendPassedHistory(Boolean.parseBoolean(request.getParameter(
214                         UifConstants.UrlParams.SHOW_HISTORY)));
215             }
216 
217             history.setCurrent(form, request);
218             history.buildHistoryFromParameterString(request.getParameter(UifConstants.UrlParams.HISTORY));
219             form.setFormHistory(history);
220         }
221     }
222 
223     /**
224      * Prepares the <code>View</code> instance contained on the form for
225      * rendering
226      *
227      * <p>
228      * First
229      * </p>
230      *
231      * @param form - form instance containing the data and view instance
232      */
233     public static void prepareViewForRendering(HttpServletRequest request, UifFormBase form) {
234         View view = form.getView();
235 
236         // set view page to page requested on form
237         if (StringUtils.isNotBlank(form.getPageId())) {
238             view.setCurrentPageId(form.getPageId());
239         }
240 
241         Map<String, String> parameterMap = KRADUtils.translateRequestParameterMap(request.getParameterMap());
242         parameterMap.putAll(form.getViewRequestParameters());
243 
244         // build view which will prepare for rendering
245         getViewService().buildView(view, form, parameterMap);
246 
247         // set dirty flag
248         form.setValidateDirty(view.isValidateDirty());
249     }
250 
251     protected static SessionDocumentService getSessionDocumentService() {
252         return KRADServiceLocatorWeb.getSessionDocumentService();
253     }
254 
255     protected static ViewService getViewService() {
256         return KRADServiceLocatorWeb.getViewService();
257     }
258 }