Coverage Report - org.kuali.rice.krad.web.bind.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.krad.web.bind;
 17  
 
 18  
 import org.kuali.rice.core.web.format.Formatter;
 19  
 import org.kuali.rice.krad.uif.field.AttributeField;
 20  
 import org.kuali.rice.krad.web.form.UifFormBase;
 21  
 import org.springframework.beans.BeanWrapperImpl;
 22  
 import org.springframework.beans.BeansException;
 23  
 import org.springframework.beans.InvalidPropertyException;
 24  
 import org.springframework.beans.PropertyValue;
 25  
 
 26  
 import java.beans.PropertyDescriptor;
 27  
 import java.beans.PropertyEditor;
 28  
 import java.util.HashSet;
 29  
 import java.util.Set;
 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  
 public class UifViewBeanWrapper extends BeanWrapperImpl {
 38  0
     private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UifViewBeanWrapper.class);
 39  
 
 40  
     // this is a handle to the target object so we don't
 41  
     // have to cast so often
 42  
     private UifFormBase form;
 43  
 
 44  
     // this stores all properties this wrapper has already checked
 45  
     // with the view so the service isn't called again
 46  
     private Set<String> processedProperties;
 47  
 
 48  
     public UifViewBeanWrapper(Object object) {
 49  0
         super(object);
 50  
 
 51  0
         form = (UifFormBase) object;
 52  0
         processedProperties = new HashSet<String>();
 53  0
     }
 54  
 
 55  
     protected void callViewService(String propertyName) {
 56  0
         if (LOG.isDebugEnabled()) {
 57  0
             LOG.debug("Attempting view service call for property '" + propertyName + "'");
 58  
         }
 59  0
         Class<? extends Formatter> formatterClass = null;
 60  
 
 61  
         // viewId should be determined in UifAnnotationMethodHandlerAdapter so
 62  
         // nothing we can do without one here
 63  0
         if (form.getView() == null) {
 64  0
             return;
 65  
         }
 66  
 
 67  
         // check if we already processed this property for this BeanWrapper instance
 68  0
         if (processedProperties.contains(propertyName)) {
 69  0
             return;
 70  
         }
 71  
 
 72  0
         AttributeField af = form.getView().getViewIndex().getAttributeFieldByPath(propertyName);
 73  0
         boolean requiresEncryption = false;
 74  0
         if (af != null) {
 75  0
             if (af.getAttributeSecurity() != null) {
 76  0
                 if (af.getAttributeSecurity().hasRestrictionThatRemovesValueFromUI()) {
 77  0
                     requiresEncryption = true;
 78  
                 }
 79  
             }
 80  
 
 81  0
             Formatter formatter = af.getFormatter();
 82  0
             if (formatter != null) {
 83  0
                 formatterClass = formatter.getClass();
 84  
             }
 85  
         }
 86  
 
 87  
         // really these should be PropertyEditors after we evaluate how many are
 88  
         // needed vs how many spring provides
 89  0
         if (formatterClass != null) {
 90  0
             if (LOG.isDebugEnabled()) {
 91  0
                 LOG.debug("Registering custom editor for property path '" + propertyName + "' and formatter class '" +
 92  
                         formatterClass.getName() + "'");
 93  
             }
 94  0
             PropertyEditor customEditor = new UifKnsFormatterPropertyEditor(formatterClass);
 95  0
             if (requiresEncryption) {
 96  0
                 if (LOG.isDebugEnabled()) {
 97  0
                     LOG.debug("Enabling encryption for custom editor '" + propertyName + "' and formatter class '" +
 98  
                             formatterClass.getName() + "'");
 99  
                 }
 100  0
                 this.registerCustomEditor(null, propertyName, new UifEncryptionPropertyEditorWrapper(customEditor));
 101  
             } else {
 102  0
                 this.registerCustomEditor(null, propertyName, customEditor);
 103  
             }
 104  0
         } else if (requiresEncryption) {
 105  0
             if (LOG.isDebugEnabled()) {
 106  0
                 LOG.debug("No custom formatter for property path '" + propertyName +
 107  
                         "' but property does require encryption");
 108  
             }
 109  0
             this.registerCustomEditor(null, propertyName,
 110  
                     new UifEncryptionPropertyEditorWrapper(findEditorForPropertyName(propertyName)));
 111  
         }
 112  
 
 113  0
         processedProperties.add(propertyName);
 114  0
     }
 115  
 
 116  
     protected PropertyEditor findEditorForPropertyName(String propertyName) {
 117  0
         Class<?> clazz = getPropertyType(propertyName);
 118  0
         if (LOG.isDebugEnabled()) {
 119  0
             LOG.debug("Attempting retrieval of property editor using class '" + clazz + "' and property path '" +
 120  
                     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 '" +
 126  
                         propertyName + "'. Attempting to find default property editor class.");
 127  
             }
 128  0
             editor = getDefaultEditor(clazz);
 129  
         }
 130  0
         return editor;
 131  
     }
 132  
 
 133  
     @Override
 134  
     public Class<?> getPropertyType(String propertyName) throws BeansException {
 135  
         try {
 136  0
             PropertyDescriptor pd = getPropertyDescriptorInternal(propertyName);
 137  0
             if (pd != null) {
 138  0
                 return pd.getPropertyType();
 139  
             }
 140  
 
 141  
             // Maybe an indexed/mapped property...
 142  0
             Object value = super.getPropertyValue(propertyName);
 143  0
             if (value != null) {
 144  0
                 return value.getClass();
 145  
             }
 146  
 
 147  
             // Check to see if there is a custom editor,
 148  
             // which might give an indication on the desired target type.
 149  0
             Class<?> editorType = guessPropertyTypeFromEditors(propertyName);
 150  0
             if (editorType != null) {
 151  0
                 return editorType;
 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  
 }