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