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