1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
33 | |
|
34 | |
|
35 | |
|
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 | |
|
41 | |
|
42 | |
private UifFormBase form; |
43 | |
|
44 | |
|
45 | |
|
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 | |
|
62 | |
|
63 | 0 | if (form.getView() == null) { |
64 | 0 | return; |
65 | |
} |
66 | |
|
67 | |
|
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 | |
|
88 | |
|
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 | |
|
142 | 0 | Object value = super.getPropertyValue(propertyName); |
143 | 0 | if (value != null) { |
144 | 0 | return value.getClass(); |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | 0 | Class<?> editorType = guessPropertyTypeFromEditors(propertyName); |
150 | 0 | if (editorType != null) { |
151 | 0 | return editorType; |
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 | |
} |