View Javadoc

1   /**
2    * Copyright 2005-2012 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.krad.service.KRADServiceLocatorWeb;
21  import org.kuali.rice.krad.uif.UifConstants;
22  import org.kuali.rice.krad.uif.view.History;
23  import org.kuali.rice.krad.uif.view.View;
24  import org.kuali.rice.krad.uif.component.Component;
25  import org.kuali.rice.krad.uif.service.ViewService;
26  import org.kuali.rice.krad.util.KRADUtils;
27  import org.kuali.rice.krad.web.controller.UifControllerBase;
28  import org.kuali.rice.krad.web.form.UifFormBase;
29  import org.springframework.web.servlet.ModelAndView;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import java.util.Map;
34  
35  /**
36   * Provides helper methods that will be used during the request lifecycle
37   *
38   * <p>
39   * Created to avoid duplication of the methods used by the UifHandlerExceptionResolver
40   * </p>
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public class UifWebUtils {
45      private static final Logger LOG = Logger.getLogger(UifWebUtils.class);
46  
47      /**
48       * Configures the <code>ModelAndView</code> instance containing the form
49       * data and pointing to the UIF generic spring view
50       *
51       * @param form - Form instance containing the model data
52       * @param pageId - Id of the page within the view that should be rendered, can
53       * be left blank in which the current or default page is rendered
54       * @return ModelAndView object with the contained form
55       */
56      public static ModelAndView getUIFModelAndView(UifFormBase form, String pageId) {
57          if (StringUtils.isNotBlank(pageId)) {
58              form.setPageId(pageId);
59          }
60  
61          // create the spring return object pointing to View.jsp
62          ModelAndView modelAndView = new ModelAndView();
63          modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, form);
64          modelAndView.setViewName(UifConstants.SPRING_VIEW_ID);
65  
66          return modelAndView;
67      }
68  
69      public static ModelAndView getComponentModelAndView(Component component, Object model) {
70          ModelAndView modelAndView = new ModelAndView();
71          modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, model);
72          modelAndView.addObject("Component", component);
73          modelAndView.setViewName("ComponentUpdate");
74  
75          return modelAndView;
76      }
77  
78      /**
79       * After the controller logic is executed, the form is placed into session
80       * and the corresponding view is prepared for rendering
81       */
82      public static void postControllerHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
83              ModelAndView modelAndView) throws Exception {
84          if (handler instanceof UifControllerBase && (modelAndView != null)) {
85              UifControllerBase controller = (UifControllerBase) handler;
86              UifFormBase form = null;
87  
88              // check to see if this is a full view request
89              if (modelAndView.getViewName().equals(UifConstants.SPRING_VIEW_ID)) {
90                  Object model = modelAndView.getModelMap().get(UifConstants.DEFAULT_MODEL_NAME);
91                  if (model instanceof UifFormBase) {
92                      form = (UifFormBase) model;
93  
94                      // prepare view instance
95                      prepareViewForRendering(request, form);
96  
97                      // update history for view
98                      prepareHistory(request, form);
99                  }
100             }
101         }
102     }
103 
104     /**
105      * Updates the history object (or constructs a new History) for the view we are getting ready
106      * to render
107      *
108      * @param request - Http request object containing the request parameters
109      * @param form - object containing the view data
110      */
111     public static void prepareHistory(HttpServletRequest request, UifFormBase form) {
112         View view = form.getView();
113 
114         // main history/breadcrumb tracking support
115         History history = form.getFormHistory();
116         if (history == null || request.getMethod().equals("GET")) {
117             history = new History();
118             history.setHomewardPath(view.getBreadcrumbs().getHomewardPathList());
119             history.setAppendHomewardPath(view.getBreadcrumbs().isDisplayHomewardPath());
120             history.setAppendPassedHistory(view.getBreadcrumbs().isDisplayPassedHistory());
121 
122             // passed settings ALWAYS override the defaults
123             if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HOME))) {
124                 history.setAppendHomewardPath(Boolean.parseBoolean(request.getParameter(
125                         UifConstants.UrlParams.SHOW_HOME)));
126             }
127 
128             if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HISTORY))) {
129                 history.setAppendPassedHistory(Boolean.parseBoolean(request.getParameter(
130                         UifConstants.UrlParams.SHOW_HISTORY)));
131             }
132 
133             history.setCurrent(form, request);
134             history.buildHistoryFromParameterString(request.getParameter(UifConstants.UrlParams.HISTORY));
135             form.setFormHistory(history);
136         }
137     }
138 
139     /**
140      * Prepares the <code>View</code> instance contained on the form for
141      * rendering
142      *
143      * @param request - request object
144      * @param form - form instance containing the data and view instance
145      */
146     public static void prepareViewForRendering(HttpServletRequest request, UifFormBase form) {
147         View view = form.getView();
148 
149         // set view page to page requested on form
150         if (StringUtils.isNotBlank(form.getPageId())) {
151             view.setCurrentPageId(form.getPageId());
152         }
153 
154         Map<String, String> parameterMap = KRADUtils.translateRequestParameterMap(request.getParameterMap());
155         parameterMap.putAll(form.getViewRequestParameters());
156 
157         // build view which will prepare for rendering
158         getViewService().buildView(view, form, parameterMap);
159 
160         // set dirty flag
161         form.setValidateDirty(view.isValidateDirty());
162     }
163 
164     protected static ViewService getViewService() {
165         return KRADServiceLocatorWeb.getViewService();
166     }
167 }