Coverage Report - org.kuali.rice.krad.uif.util.UifWebUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
UifWebUtils
0%
0/72
0%
0/34
2.889
 
 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  0
 public class UifWebUtils {
 52  0
     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  0
         UifFormBase form = null;
 67  
 
 68  0
         String formKeyParam = request.getParameter(UifParameters.FORM_KEY);
 69  0
         if (StringUtils.isBlank(formKeyParam)) {
 70  0
             formKeyParam = (String) request.getAttribute(UifParameters.FORM_KEY);
 71  
         }
 72  
 
 73  0
         String docId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
 74  0
         if (StringUtils.isNotBlank(formKeyParam)) {
 75  0
             form = (UifFormBase) request.getSession().getAttribute(formKeyParam);
 76  
             // retrieve from db if form not in session
 77  0
             if (form == null) {
 78  0
                 UserSession userSession = (UserSession) request.getSession().getAttribute(
 79  
                         KRADConstants.USER_SESSION_KEY);
 80  0
                 form = KRADServiceLocatorWeb.getSessionDocumentService().getDocumentForm(docId, formKeyParam,
 81  
                         userSession, request.getRemoteAddr());
 82  
             }
 83  
         }
 84  
 
 85  0
         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  0
         if (StringUtils.isNotBlank(pageId)) {
 99  0
             form.setPageId(pageId);
 100  
         }
 101  
 
 102  
         // create the spring return object pointing to View.jsp
 103  0
         ModelAndView modelAndView = new ModelAndView();
 104  0
         modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, form);
 105  0
         modelAndView.setViewName(UifConstants.SPRING_VIEW_ID);
 106  
 
 107  0
         return modelAndView;
 108  
     }
 109  
 
 110  
     public static ModelAndView getComponentModelAndView(Component component, Object model) {
 111  0
         ModelAndView modelAndView = new ModelAndView();
 112  0
         modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, model);
 113  0
         modelAndView.addObject("Component", component);
 114  0
         modelAndView.setViewName("ComponentUpdate");
 115  
 
 116  0
         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  0
         if (handler instanceof UifControllerBase && (modelAndView != null)) {
 126  0
             UifControllerBase controller = (UifControllerBase) handler;
 127  0
             UifFormBase form = null;
 128  
 
 129  
             // check to see if this is a full view request
 130  0
             if (modelAndView.getViewName().equals(UifConstants.SPRING_VIEW_ID)) {
 131  0
                 Object model = modelAndView.getModelMap().get(UifConstants.DEFAULT_MODEL_NAME);
 132  0
                 if (model instanceof UifFormBase) {
 133  0
                     form = (UifFormBase) model;
 134  
 
 135  0
                     form.setPreviousView(null);
 136  
 
 137  
                     // persist document form to db
 138  0
                     if (form instanceof DocumentFormBase) {
 139  0
                         UserSession userSession = (UserSession) request.getSession().getAttribute(
 140  
                                 KRADConstants.USER_SESSION_KEY);
 141  0
                         getSessionDocumentService().setDocumentForm((DocumentFormBase) form, userSession,
 142  
                                 request.getRemoteAddr());
 143  
                     }
 144  
 
 145  
                     // perform authorization of controller method
 146  0
                     checkMethodToCallAuthorization(request, controller, form);
 147  
 
 148  
                     // prepare view instance
 149  0
                     prepareViewForRendering(request, form);
 150  
 
 151  
                     // update history for view
 152  0
                     prepareHistory(request, form);
 153  
                 }
 154  
             }
 155  
         }
 156  0
     }
 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  0
         String methodToCall = request.getParameter(KRADConstants.DISPATCH_REQUEST_PARAMETER);
 173  
 
 174  0
         if (!controller.getMethodToCallsToNotCheckAuthorization().contains(methodToCall)) {
 175  0
             if (LOG.isDebugEnabled()) {
 176  0
                 LOG.debug("'" + methodToCall + "' not in set of excempt methods: " + controller
 177  
                         .getMethodToCallsToNotCheckAuthorization());
 178  
             }
 179  
 
 180  0
             controller.checkAuthorization(form, methodToCall);
 181  
         } else {
 182  0
             if (LOG.isDebugEnabled()) {
 183  0
                 LOG.debug("'" + methodToCall + "' is exempt from auth checks.");
 184  
             }
 185  
         }
 186  0
     }
 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  0
         View view = form.getView();
 197  
 
 198  
         // main history/breadcrumb tracking support
 199  0
         History history = form.getFormHistory();
 200  0
         if (history == null || request.getMethod().equals("GET")) {
 201  0
             history = new History();
 202  0
             history.setHomewardPath(view.getBreadcrumbs().getHomewardPathList());
 203  0
             history.setAppendHomewardPath(view.getBreadcrumbs().isDisplayHomewardPath());
 204  0
             history.setAppendPassedHistory(view.getBreadcrumbs().isDisplayPassedHistory());
 205  
 
 206  
             // passed settings ALWAYS override the defaults
 207  0
             if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HOME))) {
 208  0
                 history.setAppendHomewardPath(Boolean.parseBoolean(request.getParameter(
 209  
                         UifConstants.UrlParams.SHOW_HOME)));
 210  
             }
 211  
 
 212  0
             if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HISTORY))) {
 213  0
                 history.setAppendPassedHistory(Boolean.parseBoolean(request.getParameter(
 214  
                         UifConstants.UrlParams.SHOW_HISTORY)));
 215  
             }
 216  
 
 217  0
             history.setCurrent(form, request);
 218  0
             history.buildHistoryFromParameterString(request.getParameter(UifConstants.UrlParams.HISTORY));
 219  0
             form.setFormHistory(history);
 220  
         }
 221  0
     }
 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  0
         View view = form.getView();
 235  
 
 236  
         // set view page to page requested on form
 237  0
         if (StringUtils.isNotBlank(form.getPageId())) {
 238  0
             view.setCurrentPageId(form.getPageId());
 239  
         }
 240  
 
 241  0
         Map<String, String> parameterMap = KRADUtils.translateRequestParameterMap(request.getParameterMap());
 242  0
         parameterMap.putAll(form.getViewRequestParameters());
 243  
 
 244  
         // build view which will prepare for rendering
 245  0
         getViewService().buildView(view, form, parameterMap);
 246  
 
 247  
         // set dirty flag
 248  0
         form.setValidateDirty(view.isValidateDirty());
 249  0
     }
 250  
 
 251  
     protected static SessionDocumentService getSessionDocumentService() {
 252  0
         return KRADServiceLocatorWeb.getSessionDocumentService();
 253  
     }
 254  
 
 255  
     protected static ViewService getViewService() {
 256  0
         return KRADServiceLocatorWeb.getViewService();
 257  
     }
 258  
 }