Coverage Report - org.kuali.rice.kns.web.spring.UifViewBeanWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
UifViewBeanWrapper
0%
0/71
0%
0/38
3.778
 
 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.beans.PropertyDescriptor;
 19  
 import java.beans.PropertyEditor;
 20  
 import java.util.HashSet;
 21  
 import java.util.Set;
 22  
 
 23  
 import org.kuali.rice.core.web.format.Formatter;
 24  
 import org.kuali.rice.kns.uif.field.AttributeField;
 25  
 import org.kuali.rice.kns.web.spring.form.UifFormBase;
 26  
 import org.springframework.beans.BeanWrapperImpl;
 27  
 import org.springframework.beans.BeansException;
 28  
 import org.springframework.beans.InvalidPropertyException;
 29  
 import org.springframework.beans.PropertyValue;
 30  
 
 31  
 /**
 32  
  * This class is a top level BeanWrapper for a UIF View (form).  It will call the
 33  
  * view service to find formatters and check if fields are encrypted. 
 34  
  * 
 35  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 36  
  *
 37  
  */
 38  
 public class UifViewBeanWrapper extends BeanWrapperImpl {
 39  0
     private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UifViewBeanWrapper.class);
 40  
     
 41  
     
 42  
     // this is a handle to the target object so we don't
 43  
     // have to cast so often
 44  
     private UifFormBase form;
 45  
     
 46  
     // this stores all properties this wrapper has already checked
 47  
     // with the view so the service isn't called again
 48  
     private Set<String> processedProperties;
 49  
     
 50  
     
 51  
     public UifViewBeanWrapper(Object object) {
 52  0
         super(object);
 53  
         
 54  0
         form = (UifFormBase)object;
 55  0
         processedProperties = new HashSet<String>();
 56  0
     }
 57  
     
 58  
     
 59  
     protected void callViewService(String propertyName) {
 60  0
         if (LOG.isDebugEnabled()) {
 61  0
             LOG.debug("Attempting view service call for property '" + propertyName + "'");
 62  
         }
 63  0
         Class<? extends Formatter> formatterClass = null;
 64  
         
 65  
         // viewId should be determined in UifAnnotationMethodHandlerAdapter so
 66  
         // nothing we can do without one here
 67  0
         if(form.getView() == null) {
 68  0
             return;
 69  
         }
 70  
         
 71  
         // check if we already processed this property for this BeanWrapper instance
 72  0
         if(processedProperties.contains(propertyName)) {
 73  0
             return;
 74  
         }
 75  
         
 76  0
         AttributeField af = form.getView().getViewIndex().getAttributeFieldByPath(propertyName);
 77  0
         boolean requiresEncryption = false;
 78  0
         if(af != null) {
 79  0
                 if (af.getAttributeSecurity() != null) {
 80  0
                         if (af.getAttributeSecurity().hasRestrictionThatRemovesValueFromUI()) {
 81  0
                                 requiresEncryption = true;
 82  
                         }
 83  
                 }
 84  
             
 85  0
             Formatter formatter = af.getFormatter();
 86  0
             if(formatter != null) {
 87  0
                 formatterClass = formatter.getClass();
 88  
             }
 89  
         }
 90  
         
 91  
         // really these should be PropertyEditors after we evaluate how many are
 92  
         // needed vs how many spring provides
 93  0
         if(formatterClass != null) {
 94  0
                 if (LOG.isDebugEnabled()) {
 95  0
                         LOG.debug("Registering custom editor for property path '" + propertyName + "' and formatter class '" + formatterClass.getName() + "'");
 96  
                 }
 97  0
                 PropertyEditor customEditor = new UifKnsFormatterPropertyEditor(formatterClass);
 98  0
                 if (requiresEncryption) {
 99  0
                     if (LOG.isDebugEnabled()) {
 100  0
                             LOG.debug("Enabling encryption for custom editor '" + propertyName + "' and formatter class '" + formatterClass.getName() + "'");
 101  
                     }
 102  0
                         this.registerCustomEditor(null, propertyName, new UifEncryptionPropertyEditorWrapper(customEditor));
 103  
                 } else {
 104  0
                         this.registerCustomEditor(null, propertyName, customEditor);
 105  
                 }
 106  0
         } else if (requiresEncryption) {
 107  0
                 if (LOG.isDebugEnabled()) {
 108  0
                         LOG.debug("No custom formatter for property path '" + propertyName + "' but property does require encryption");
 109  
                 }
 110  0
                 this.registerCustomEditor(null, propertyName, new UifEncryptionPropertyEditorWrapper(findEditorForPropertyName(propertyName)));
 111  
         }
 112  
         
 113  0
         processedProperties.add(propertyName);
 114  
         
 115  0
     }
 116  
 
 117  
     protected PropertyEditor findEditorForPropertyName(String propertyName) {
 118  0
             Class<?> clazz = getPropertyType(propertyName);
 119  0
             if (LOG.isDebugEnabled()) {
 120  0
                     LOG.debug("Attempting retrieval of property editor using class '" + clazz + "' and property path '" + propertyName + "'");
 121  
             }
 122  0
                 PropertyEditor editor = findCustomEditor(clazz, propertyName);
 123  0
                 if (editor == null) {
 124  0
                     if (LOG.isDebugEnabled()) {
 125  0
                             LOG.debug("No custom property editor found using class '" + clazz + "' and property path '" + propertyName + "'. Attempting to find default property editor class.");
 126  
                     }
 127  0
                         editor = getDefaultEditor(clazz);
 128  
                 }
 129  0
                 return editor;
 130  
     }
 131  
     
 132  
     @Override
 133  
     public Class<?> getPropertyType(String propertyName) throws BeansException {
 134  
         try {
 135  0
             PropertyDescriptor pd = getPropertyDescriptorInternal(propertyName);
 136  0
             if (pd != null) {
 137  0
                 return pd.getPropertyType();
 138  
             }
 139  
             
 140  
             // Maybe an indexed/mapped property...
 141  0
             Object value = super.getPropertyValue(propertyName);
 142  0
             if (value != null) {
 143  0
                 return value.getClass();
 144  
             }
 145  
             
 146  
             // Check to see if there is a custom editor,
 147  
             // which might give an indication on the desired target type.
 148  0
             Class<?> editorType = guessPropertyTypeFromEditors(propertyName);
 149  0
             if (editorType != null) {
 150  0
                 return editorType;
 151  
             }
 152  
         }
 153  0
         catch (InvalidPropertyException ex) {
 154  
             // Consider as not determinable.
 155  0
         }
 156  
 
 157  0
         return null;
 158  
     }
 159  
 
 160  
     @Override
 161  
     public Object getPropertyValue(String propertyName) throws BeansException {
 162  0
         callViewService(propertyName);
 163  0
         return super.getPropertyValue(propertyName);
 164  
     }
 165  
 
 166  
     @Override
 167  
     public void setPropertyValue(PropertyValue pv) throws BeansException {
 168  0
         callViewService(pv.getName());
 169  0
         super.setPropertyValue(pv);
 170  0
     }
 171  
 
 172  
     @Override
 173  
     public void setPropertyValue(String propertyName, Object value) throws BeansException {
 174  0
         callViewService(propertyName);
 175  0
         super.setPropertyValue(propertyName, value);
 176  0
     }
 177  
 
 178  
     @Override
 179  
     public void setWrappedInstance(Object object, String nestedPath, Object rootObject) {
 180  
         //TODO clear cache?
 181  0
         form = (UifFormBase)object;
 182  0
         super.setWrappedInstance(object, nestedPath, rootObject);
 183  0
     }
 184  
 
 185  
     @Override
 186  
     public void setWrappedInstance(Object object) {
 187  
         //TODO clear cache?
 188  0
         form = (UifFormBase)object;
 189  0
         super.setWrappedInstance(object);
 190  0
     }
 191  
 
 192  
 }