Coverage Report - org.kuali.rice.kns.uif.service.impl.ExpressionEvaluatorServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionEvaluatorServiceImpl
0%
0/81
0%
0/40
4.714
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation Licensed under the Educational Community
 3  
  * License, Version 1.0 (the "License"); you may not use this file except in
 4  
  * compliance with the License. You may obtain a copy of the License at
 5  
  * http://www.opensource.org/licenses/ecl1.php Unless required by applicable law
 6  
  * or agreed to in writing, software distributed under the License is
 7  
  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 8  
  * KIND, either express or implied. See the License for the specific language
 9  
  * governing permissions and limitations under the License.
 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  
  * Evaluates expression language statements using the Spring EL engine TODO:
 35  
  * Look into using Rice KRMS for evaluation
 36  
  * 
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
      * @see org.kuali.rice.kns.uif.service.ExpressionEvaluatorService#evaluateObjectProperties(java.lang.Object,
 45  
      *      java.lang.Object, java.util.Map)
 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  
      * @see org.kuali.rice.kns.uif.service.ExpressionEvaluatorService#evaluateExpressionTemplate(java.lang.Object,
 56  
      *      java.util.Map, java.lang.String)
 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  
      * @see org.kuali.rice.kns.uif.service.ExpressionEvaluatorService#evaluateExpression(java.lang.Object,
 87  
      *      java.util.Map, java.lang.String)
 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  
      * Registers custom functions for el expressions with the given context
 112  
      *
 113  
      * @param context - context instance to register functions to
 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  
      * Iterates through any configured <code>PropertyReplacer</code> instances for the component and
 127  
      * evaluates the given condition. If the condition is met, the replacement value is set on the
 128  
      * corresponding property
 129  
      *
 130  
      * @param object - object instance with property replacers list, should be either a component or layout manager
 131  
      * @param contextObject - context for el evaluation
 132  
      * @param evaluationParameters - parameters for el evaluation
 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  
      * Iterates through the properties of the given object and checks for property values that contain
 156  
      * an el expression. If an expression is found it will be evaluated and the result of that evaluation
 157  
      * set back into the property value
 158  
      *
 159  
      * <p>
 160  
      *  If the property contains an el template (part static text and part expression), only the expression
 161  
      *  part will be replaced with the result. More than one expressions may be contained within the template
 162  
      * </p>
 163  
      *
 164  
      * <p>
 165  
      *  A special check is done for Map property types. When found the Map is iterated over and expressions
 166  
      *  contained in the map value are evaluated
 167  
      * </p>
 168  
      *
 169  
      * @param object - object to evaluate properties for
 170  
      * @param contextObject - context for el evaluation
 171  
      * @param evaluationParameters - parameters for el evaluation
 172  
      */
 173  
     protected void visitPropertiesAndEvaluateExpressions(Object object, Object contextObject,
 174  
             Map<String, Object> evaluationParameters) {
 175  
         // iterate through object properties and check for expressions
 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  
                     // evaluate any expressions and reset property value
 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  
                         // get the target property by convention
 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  
      * @see org.kuali.rice.kns.uif.service.ExpressionEvaluatorService#containsElPlaceholder(java.lang.String)
 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  
 }