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