Coverage Report - org.kuali.rice.kns.web.spring.UifServletRequestDataBinder
 
Classes in this File Line Coverage Branch Coverage Complexity
UifServletRequestDataBinder
0%
0/63
0%
0/28
3.111
 
 1  
 /*
 2  
  * Copyright 2007 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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.kns.web.spring;
 17  
 
 18  
 import java.util.HashMap;
 19  
 import java.util.Map;
 20  
 
 21  
 import javax.servlet.ServletRequest;
 22  
 import javax.servlet.http.HttpServletRequest;
 23  
 
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 import org.kuali.rice.kns.datadictionary.DataDictionaryException;
 26  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 27  
 import org.kuali.rice.kns.uif.UifConstants;
 28  
 import org.kuali.rice.kns.uif.UifParameters;
 29  
 import org.kuali.rice.kns.uif.container.View;
 30  
 import org.kuali.rice.kns.uif.service.ViewService;
 31  
 import org.kuali.rice.kns.util.WebUtils;
 32  
 import org.kuali.rice.kns.web.spring.form.UifFormBase;
 33  
 import org.springframework.core.convert.ConversionService;
 34  
 import org.springframework.util.Assert;
 35  
 import org.springframework.validation.AbstractPropertyBindingResult;
 36  
 import org.springframework.web.bind.ServletRequestDataBinder;
 37  
 
 38  
 /**
 39  
  * Override of ServletRequestDataBinder in order to hook in the UifBeanPropertyBindingResult
 40  
  * which instantiates a custom BeanWrapperImpl. 
 41  
  * 
 42  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 43  
  */
 44  
 public class UifServletRequestDataBinder extends ServletRequestDataBinder {
 45  0
     protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UifServletRequestDataBinder.class);
 46  
 
 47  
         private UifBeanPropertyBindingResult bindingResult;
 48  
         private ConversionService conversionService;
 49  
         
 50  
         protected ViewService viewService;
 51  
 
 52  
 
 53  
         public UifServletRequestDataBinder(Object target) {
 54  0
         super(target);
 55  0
         setBindingErrorProcessor(new UifBindingErrorProcessor());
 56  0
     }
 57  
         
 58  
         public UifServletRequestDataBinder(Object target, String name) {
 59  0
         super(target, name);
 60  0
         setBindingErrorProcessor(new UifBindingErrorProcessor());
 61  0
     }
 62  
         
 63  
     /**
 64  
      * Allows for a custom binding result class.
 65  
      * 
 66  
      * @see org.springframework.validation.DataBinder#initBeanPropertyAccess()
 67  
      */
 68  
     @Override
 69  
         public void initBeanPropertyAccess() {
 70  0
                 Assert.state(this.bindingResult == null,
 71  
                                 "DataBinder is already initialized - call initBeanPropertyAccess before other configuration methods");
 72  0
                 this.bindingResult = new UifBeanPropertyBindingResult(getTarget(), getObjectName(), isAutoGrowNestedPaths());
 73  0
                 if (this.conversionService != null) {
 74  0
                         this.bindingResult.initConversion(this.conversionService);
 75  
                 }
 76  0
         }
 77  
 
 78  
     /**
 79  
      * Allows for the setting attributes to use to find the data dictionary data from Kuali
 80  
      * 
 81  
      * @see org.springframework.validation.DataBinder#getInternalBindingResult()
 82  
      */
 83  
     @Override
 84  
         protected AbstractPropertyBindingResult getInternalBindingResult() {
 85  0
                 if (this.bindingResult == null) {
 86  0
                         initBeanPropertyAccess();
 87  
                 }
 88  0
                 return this.bindingResult;
 89  
         }
 90  
 
 91  
         /**
 92  
      * Disallows direct field access for Kuali
 93  
      * 
 94  
      * @see org.springframework.validation.DataBinder#initDirectFieldAccess()
 95  
      */
 96  
     @Override
 97  
         public void initDirectFieldAccess() {
 98  0
             LOG.error("Direct Field access is not allowed in UifServletRequestDataBinder.");
 99  0
                 throw new RuntimeException("Direct Field access is not allowed in Kuali");
 100  
         }
 101  
 
 102  
         @Override
 103  
         @SuppressWarnings("unchecked")
 104  
     public void bind(ServletRequest request) {
 105  0
         super.bind(request);
 106  0
         UifFormBase form = (UifFormBase) this.getTarget();
 107  
         
 108  
         // check for request param that indicates to skip view initialize
 109  0
         Boolean skipViewInit = WebUtils.getRequestParameterAsBoolean(request, UifParameters.SKIP_VIEW_INIT);
 110  0
         if ((skipViewInit != null) && skipViewInit.booleanValue()) {
 111  
             // just invoke post bind on form and return
 112  0
             form.postBind((HttpServletRequest) request);
 113  0
             return;
 114  
         }
 115  
 
 116  
         // back up previous view instance
 117  0
         View previousView = form.getView();
 118  0
         form.setPreviousView(previousView);
 119  
 
 120  0
         Map<String, String> viewRequestParameters = new HashMap<String, String>();
 121  
         // TODO: this should get from form not previous view, test
 122  0
         if (previousView != null) {
 123  0
             viewRequestParameters = previousView.getViewRequestParameters();
 124  
         }
 125  
 
 126  
         // initialize new view for request
 127  0
         View view = null;
 128  
 
 129  
         // we are going to need this no matter how we get the view, so do it
 130  
         // once
 131  0
         Map<String, String> parameterMap = WebUtils.translateRequestParameterMap(request.getParameterMap());
 132  
 
 133  
         // determine whether full view should be rendered or just page
 134  
         // if not specified on the request and previous is not null, default to
 135  
         // just page
 136  
         // TODO: revisit and see if we can have a general pattern
 137  
         // if ((previousView != null) &&
 138  
         // !parameterMap.containsKey(UifParameters.RENDER_FULL_VIEW)) {
 139  
         // form.setRenderFullView(false);
 140  
         // }
 141  
 
 142  
         // add/override view request parameters
 143  0
         parameterMap.putAll(viewRequestParameters);
 144  
 
 145  0
         String viewId = request.getParameter(UifParameters.VIEW_ID);
 146  0
         if (viewId != null) {
 147  0
             view = getViewService().getView(viewId, parameterMap);
 148  
         } else {
 149  0
             String viewTypeName = request.getParameter(UifParameters.VIEW_TYPE_NAME);
 150  0
             if (viewTypeName == null) {
 151  0
                 viewTypeName = form.getViewTypeName();
 152  
             }
 153  
 
 154  0
             if (StringUtils.isBlank(viewTypeName)) {
 155  0
                 view = getViewFromPreviousModel(form, parameterMap);
 156  0
                 if (view == null) {
 157  0
                     throw new RuntimeException(
 158  
                             "Could not find enough information to fetch the required view. " +
 159  
                              " Checked the model retrieved from session for both viewTypeName and viewId");
 160  
                 }
 161  
             } else {
 162  
                 try {
 163  0
                     view = getViewService().getViewByType(viewTypeName, parameterMap);
 164  0
                 } catch (DataDictionaryException ddex) {
 165  0
                     view = getViewFromPreviousModel(form, parameterMap);
 166  
                     // if we didn't find one, just re-throw
 167  0
                     if (view == null) {
 168  0
                         throw ddex;
 169  
                     }
 170  0
                     LOG.warn("Obtained viewId from cached form, this may not be safe!");
 171  0
                 }
 172  
             }
 173  
         }
 174  
 
 175  
         // apply default values to form if needed
 176  0
         if (!form.isDefaultsApplied()) {
 177  0
             view.getViewHelperService().applyDefaultValues(view, form);
 178  
         }
 179  
 
 180  0
         form.setViewRequestParameters(view.getViewRequestParameters());
 181  0
         form.setViewId(view.getId());
 182  0
         form.setView(view);
 183  0
         form.postBind((HttpServletRequest) request);
 184  0
     }
 185  
 
 186  
         protected View getViewFromPreviousModel(UifFormBase form, Map<String, String> parameterMap) {
 187  
         // maybe we have a view id from the session form
 188  0
         if(form.getViewId() != null) {
 189  0
             return getViewService().getView(form.getViewId(), parameterMap);
 190  
         }
 191  0
         return null;
 192  
         }
 193  
 
 194  
         public ViewService getViewService() {
 195  0
         if(viewService == null) {
 196  0
             viewService = KNSServiceLocatorWeb.getViewService();
 197  
         }
 198  0
         return this.viewService;
 199  
     }
 200  
 
 201  
     public void setViewService(ViewService viewService) {
 202  0
         this.viewService = viewService;
 203  0
     }
 204  
 
 205  
 }