Coverage Report - org.kuali.rice.kns.web.spring.UifViewBeanWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
UifViewBeanWrapper
0%
0/59
0%
0/32
3.25
 
 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.PropertyEditor;
 19  
 import java.util.HashSet;
 20  
 import java.util.Set;
 21  
 
 22  
 import org.kuali.rice.core.web.format.Formatter;
 23  
 import org.kuali.rice.kns.uif.field.AttributeField;
 24  
 import org.kuali.rice.kns.web.spring.form.UifFormBase;
 25  
 import org.springframework.beans.BeanWrapperImpl;
 26  
 import org.springframework.beans.BeansException;
 27  
 import org.springframework.beans.PropertyValue;
 28  
 
 29  
 /**
 30  
  * This class is a top level BeanWrapper for a UIF View (form).  It will call the
 31  
  * view service to find formatters and check if fields are encrypted. 
 32  
  * 
 33  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 34  
  *
 35  
  */
 36  
 public class UifViewBeanWrapper extends BeanWrapperImpl {
 37  0
     private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UifViewBeanWrapper.class);
 38  
     
 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  
     
 49  
     public UifViewBeanWrapper(Object object) {
 50  0
         super(object);
 51  
         
 52  0
         form = (UifFormBase)object;
 53  0
         processedProperties = new HashSet<String>();
 54  0
     }
 55  
     
 56  
     
 57  
     protected void callViewService(String propertyName) {
 58  0
         if (LOG.isDebugEnabled()) {
 59  0
             LOG.debug("Attempting view service call for property '" + propertyName + "'");
 60  
         }
 61  0
         Class<? extends Formatter> formatterClass = null;
 62  
         
 63  
         // viewId should be determined in UifAnnotationMethodHandlerAdapter so
 64  
         // nothing we can do without one here
 65  0
         if(form.getView() == null) {
 66  0
             return;
 67  
         }
 68  
         
 69  
         // check if we already processed this property for this BeanWrapper instance
 70  0
         if(processedProperties.contains(propertyName)) {
 71  0
             return;
 72  
         }
 73  
         
 74  0
         AttributeField af = form.getView().getViewIndex().getAttributeFieldByPath(propertyName);
 75  0
         boolean requiresEncryption = false;
 76  0
         if(af != null) {
 77  0
                 if (af.getAttributeSecurity() != null) {
 78  0
                         if (af.getAttributeSecurity().hasRestrictionThatRemovesValueFromUI()) {
 79  0
                                 requiresEncryption = true;
 80  
                         }
 81  
                 }
 82  
             
 83  0
             Formatter formatter = af.getFormatter();
 84  0
             if(formatter != null) {
 85  0
                 formatterClass = formatter.getClass();
 86  
             }
 87  
         }
 88  
         
 89  
         // really these should be PropertyEditors after we evaluate how many are
 90  
         // needed vs how many spring provides
 91  0
         if(formatterClass != null) {
 92  0
                 if (LOG.isDebugEnabled()) {
 93  0
                         LOG.debug("Registering custom editor for property path '" + propertyName + "' and formatter class '" + formatterClass.getName() + "'");
 94  
                 }
 95  0
                 PropertyEditor customEditor = new UifKnsFormatterPropertyEditor(formatterClass);
 96  0
                 if (requiresEncryption) {
 97  0
                     if (LOG.isDebugEnabled()) {
 98  0
                             LOG.debug("Enabling encryption for custom editor '" + propertyName + "' and formatter class '" + 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 + "' but property does require encryption");
 107  
                 }
 108  0
                 this.registerCustomEditor(null, propertyName, new UifEncryptionPropertyEditorWrapper(findEditorForPropertyName(propertyName)));
 109  
         }
 110  
         
 111  0
         processedProperties.add(propertyName);
 112  
         
 113  0
     }
 114  
 
 115  
     protected PropertyEditor findEditorForPropertyName(String propertyName) {
 116  0
             Class<?> clazz = getPropertyType(propertyName);
 117  0
             if (LOG.isDebugEnabled()) {
 118  0
                     LOG.debug("Attempting retrieval of property editor using class '" + clazz + "' and property path '" + propertyName + "'");
 119  
             }
 120  0
                 PropertyEditor editor = findCustomEditor(clazz, propertyName);
 121  0
                 if (editor == null) {
 122  0
                     if (LOG.isDebugEnabled()) {
 123  0
                             LOG.debug("No custom property editor found using class '" + clazz + "' and property path '" + propertyName + "'. Attempting to find default property editor class.");
 124  
                     }
 125  0
                         editor = getDefaultEditor(clazz);
 126  
                 }
 127  0
                 return editor;
 128  
     }
 129  
 
 130  
     @Override
 131  
     public Object getPropertyValue(String propertyName) throws BeansException {
 132  0
         callViewService(propertyName);
 133  0
         return super.getPropertyValue(propertyName);
 134  
     }
 135  
 
 136  
     @Override
 137  
     public void setPropertyValue(PropertyValue pv) throws BeansException {
 138  0
         callViewService(pv.getName());
 139  0
         super.setPropertyValue(pv);
 140  0
     }
 141  
 
 142  
     @Override
 143  
     public void setPropertyValue(String propertyName, Object value) throws BeansException {
 144  0
         callViewService(propertyName);
 145  0
         super.setPropertyValue(propertyName, value);
 146  0
     }
 147  
 
 148  
     @Override
 149  
     public void setWrappedInstance(Object object, String nestedPath, Object rootObject) {
 150  
         //TODO clear cache?
 151  0
         form = (UifFormBase)object;
 152  0
         super.setWrappedInstance(object, nestedPath, rootObject);
 153  0
     }
 154  
 
 155  
     @Override
 156  
     public void setWrappedInstance(Object object) {
 157  
         //TODO clear cache?
 158  0
         form = (UifFormBase)object;
 159  0
         super.setWrappedInstance(object);
 160  0
     }
 161  
 
 162  
 }