| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 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 | |
|
| 43 | |
|
| 44 | |
private UifFormBase form; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 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 | |
|
| 66 | |
|
| 67 | 0 | if(form.getView() == null) { |
| 68 | 0 | return; |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 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 | |
|
| 92 | |
|
| 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 | |
|
| 141 | 0 | Object value = super.getPropertyValue(propertyName); |
| 142 | 0 | if (value != null) { |
| 143 | 0 | return value.getClass(); |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | 0 | Class<?> editorType = guessPropertyTypeFromEditors(propertyName); |
| 149 | 0 | if (editorType != null) { |
| 150 | 0 | return editorType; |
| 151 | |
} |
| 152 | |
} |
| 153 | 0 | catch (InvalidPropertyException ex) { |
| 154 | |
|
| 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 | |
|
| 181 | 0 | form = (UifFormBase)object; |
| 182 | 0 | super.setWrappedInstance(object, nestedPath, rootObject); |
| 183 | 0 | } |
| 184 | |
|
| 185 | |
@Override |
| 186 | |
public void setWrappedInstance(Object object) { |
| 187 | |
|
| 188 | 0 | form = (UifFormBase)object; |
| 189 | 0 | super.setWrappedInstance(object); |
| 190 | 0 | } |
| 191 | |
|
| 192 | |
} |