| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
package org.kuali.rice.kns.uif.service.impl; |
| 12 | |
|
| 13 | |
import java.beans.PropertyDescriptor; |
| 14 | |
import java.util.List; |
| 15 | |
import java.util.Map; |
| 16 | |
import java.util.Map.Entry; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.kuali.rice.kns.uif.UifConstants; |
| 20 | |
import org.kuali.rice.kns.uif.core.Component; |
| 21 | |
import org.kuali.rice.kns.uif.core.PropertyReplacer; |
| 22 | |
import org.kuali.rice.kns.uif.layout.LayoutManager; |
| 23 | |
import org.kuali.rice.kns.uif.service.ExpressionEvaluatorService; |
| 24 | |
import org.kuali.rice.kns.uif.util.ExpressionFunctions; |
| 25 | |
import org.kuali.rice.kns.uif.util.ObjectPropertyUtils; |
| 26 | |
import org.springframework.expression.EvaluationException; |
| 27 | |
import org.springframework.expression.Expression; |
| 28 | |
import org.springframework.expression.ExpressionParser; |
| 29 | |
import org.springframework.expression.common.TemplateParserContext; |
| 30 | |
import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 31 | |
import org.springframework.expression.spel.support.StandardEvaluationContext; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | public class ExpressionEvaluatorServiceImpl implements ExpressionEvaluatorService { |
| 40 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger |
| 41 | |
.getLogger(ExpressionEvaluatorServiceImpl.class); |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public void evaluateObjectProperties(Object object, Object contextObject, Map<String, Object> evaluationParameters) { |
| 48 | 0 | if (object instanceof Component) { |
| 49 | 0 | evaluatePropertyReplacers(object, contextObject, evaluationParameters); |
| 50 | |
} |
| 51 | 0 | visitPropertiesAndEvaluateExpressions(object, contextObject, evaluationParameters); |
| 52 | 0 | } |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public String evaluateExpressionTemplate(Object contextObject, Map<String, Object> evaluationParameters, |
| 59 | |
String expressionTemplate) { |
| 60 | 0 | StandardEvaluationContext context = new StandardEvaluationContext(contextObject); |
| 61 | 0 | context.setVariables(evaluationParameters); |
| 62 | 0 | addCustomFunctions(context); |
| 63 | |
|
| 64 | 0 | ExpressionParser parser = new SpelExpressionParser(); |
| 65 | |
|
| 66 | 0 | String result = null; |
| 67 | |
try { |
| 68 | 0 | Expression expression = null; |
| 69 | 0 | if (StringUtils.contains(expressionTemplate, UifConstants.EL_PLACEHOLDER_PREFIX)) { |
| 70 | 0 | expression = parser.parseExpression(expressionTemplate, new TemplateParserContext( |
| 71 | |
UifConstants.EL_PLACEHOLDER_PREFIX, UifConstants.EL_PLACEHOLDER_SUFFIX)); |
| 72 | |
} else { |
| 73 | 0 | expression = parser.parseExpression(expressionTemplate); |
| 74 | |
} |
| 75 | |
|
| 76 | 0 | result = expression.getValue(context, String.class); |
| 77 | 0 | } catch (Exception e) { |
| 78 | 0 | LOG.error("Exception evaluating expression: " + expressionTemplate); |
| 79 | 0 | throw new RuntimeException("Exception evaluating expression: " + expressionTemplate, e); |
| 80 | 0 | } |
| 81 | |
|
| 82 | 0 | return result; |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public Object evaluateExpression(Object contextObject, Map<String, Object> evaluationParameters, |
| 90 | |
String expressionStr) { |
| 91 | 0 | StandardEvaluationContext context = new StandardEvaluationContext(contextObject); |
| 92 | 0 | context.setVariables(evaluationParameters); |
| 93 | 0 | addCustomFunctions(context); |
| 94 | |
|
| 95 | 0 | ExpressionParser parser = new SpelExpressionParser(); |
| 96 | 0 | Expression expression = parser.parseExpression(expressionStr); |
| 97 | |
|
| 98 | 0 | Object result = null; |
| 99 | |
try { |
| 100 | 0 | result = expression.getValue(context); |
| 101 | |
} |
| 102 | 0 | catch (EvaluationException e) { |
| 103 | 0 | LOG.error("Exception evaluating expression: " + expressionStr); |
| 104 | 0 | throw new RuntimeException("Exception evaluating expression: " + expressionStr, e); |
| 105 | 0 | } |
| 106 | |
|
| 107 | 0 | return result; |
| 108 | |
} |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
protected void addCustomFunctions(StandardEvaluationContext context) { |
| 116 | |
try { |
| 117 | 0 | context.registerFunction("isAssignableFrom", ExpressionFunctions.class |
| 118 | |
.getDeclaredMethod("isAssignableFrom", new Class[]{Class.class, Class.class})); |
| 119 | 0 | } catch (NoSuchMethodException e) { |
| 120 | 0 | LOG.error("Custom function for el expressions not found: " + e.getMessage()); |
| 121 | 0 | throw new RuntimeException("Custom function for el expressions not found: " + e.getMessage(), e); |
| 122 | 0 | } |
| 123 | 0 | } |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
protected void evaluatePropertyReplacers(Object object, Object contextObject, |
| 135 | |
Map<String, Object> evaluationParameters) { |
| 136 | 0 | List<PropertyReplacer> replacers = null; |
| 137 | 0 | if (Component.class.isAssignableFrom(object.getClass())) { |
| 138 | 0 | replacers = ((Component) object).getPropertyReplacers(); |
| 139 | 0 | } else if (LayoutManager.class.isAssignableFrom(object.getClass())) { |
| 140 | 0 | replacers = ((LayoutManager) object).getPropertyReplacers(); |
| 141 | |
} |
| 142 | |
|
| 143 | 0 | for (PropertyReplacer propertyReplacer : replacers) { |
| 144 | 0 | String conditionEvaluation = |
| 145 | |
evaluateExpressionTemplate(contextObject, evaluationParameters, propertyReplacer.getCondition()); |
| 146 | 0 | boolean conditionSuccess = Boolean.parseBoolean(conditionEvaluation); |
| 147 | 0 | if (conditionSuccess) { |
| 148 | 0 | ObjectPropertyUtils.setPropertyValue(object, propertyReplacer.getPropertyName(), |
| 149 | |
propertyReplacer.getReplacement()); |
| 150 | |
} |
| 151 | 0 | } |
| 152 | 0 | } |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
protected void visitPropertiesAndEvaluateExpressions(Object object, Object contextObject, |
| 174 | |
Map<String, Object> evaluationParameters) { |
| 175 | |
|
| 176 | 0 | PropertyDescriptor[] propertyDescriptors = ObjectPropertyUtils.getPropertyDescriptors(object); |
| 177 | 0 | for (int i = 0; i < propertyDescriptors.length; i++) { |
| 178 | 0 | PropertyDescriptor descriptor = propertyDescriptors[i]; |
| 179 | |
|
| 180 | 0 | if (descriptor.getWriteMethod() == null) { |
| 181 | 0 | continue; |
| 182 | |
} |
| 183 | |
|
| 184 | 0 | String propertyName = descriptor.getName(); |
| 185 | 0 | if (String.class.isAssignableFrom(descriptor.getPropertyType())) { |
| 186 | 0 | String propertyValue = ObjectPropertyUtils.getPropertyValue(object, propertyName); |
| 187 | |
|
| 188 | 0 | if (StringUtils.isNotBlank(propertyValue) |
| 189 | |
&& (containsElPlaceholder(propertyValue) || StringUtils.startsWith(propertyName, |
| 190 | |
UifConstants.EL_CONDITIONAL_PROPERTY_PREFIX))) { |
| 191 | |
|
| 192 | |
|
| 193 | 0 | propertyValue = evaluateExpressionTemplate(contextObject, evaluationParameters, propertyValue); |
| 194 | |
|
| 195 | 0 | String propertyNameToSet = propertyName; |
| 196 | 0 | if (StringUtils.startsWith(propertyName, UifConstants.EL_CONDITIONAL_PROPERTY_PREFIX)) { |
| 197 | |
|
| 198 | 0 | propertyNameToSet = StringUtils.removeStart(propertyName, |
| 199 | |
UifConstants.EL_CONDITIONAL_PROPERTY_PREFIX); |
| 200 | 0 | propertyNameToSet = StringUtils.substring(propertyNameToSet, 0, 1).toLowerCase() |
| 201 | |
+ StringUtils.substring(propertyNameToSet, 1, propertyNameToSet.length()); |
| 202 | |
} |
| 203 | |
|
| 204 | 0 | ObjectPropertyUtils.setPropertyValue(object, propertyNameToSet, propertyValue); |
| 205 | |
} |
| 206 | 0 | } |
| 207 | 0 | else if (Map.class.isAssignableFrom(descriptor.getPropertyType())) { |
| 208 | 0 | Map<Object, Object> propertyValue = ObjectPropertyUtils.getPropertyValue(object, propertyName); |
| 209 | |
|
| 210 | 0 | if (propertyValue != null) { |
| 211 | 0 | for (Entry<Object, Object> entry : propertyValue.entrySet()) { |
| 212 | 0 | if ((entry.getValue() != null) && String.class.isAssignableFrom(entry.getValue().getClass()) |
| 213 | |
&& containsElPlaceholder((String) entry.getValue())) { |
| 214 | 0 | String entryValue = evaluateExpressionTemplate(contextObject, evaluationParameters, |
| 215 | |
(String) entry.getValue()); |
| 216 | 0 | propertyValue.put(entry.getKey(), entryValue); |
| 217 | 0 | } |
| 218 | |
} |
| 219 | |
} |
| 220 | |
} |
| 221 | |
} |
| 222 | 0 | } |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
public boolean containsElPlaceholder(String value) { |
| 228 | 0 | boolean containsElPlaceholder = false; |
| 229 | |
|
| 230 | 0 | String elPlaceholder = StringUtils.substringBetween(value, UifConstants.EL_PLACEHOLDER_PREFIX, |
| 231 | |
UifConstants.EL_PLACEHOLDER_SUFFIX); |
| 232 | 0 | if (elPlaceholder != null) { |
| 233 | 0 | containsElPlaceholder = true; |
| 234 | |
} |
| 235 | |
|
| 236 | 0 | return containsElPlaceholder; |
| 237 | |
} |
| 238 | |
|
| 239 | |
} |