Class MvelHelper

java.lang.Object
org.broadleafcommerce.common.rule.MvelHelper

public class MvelHelper extends Object
Helper class for some common rule functions that can be called from mvel as well as utility functions to make calling MVEL rules within Broadleaf easier.

An instance of this class is available to the mvel runtime under the variable name MvelHelper with the following functions:

convertField(type, fieldValue) toUpperCase(value)

Author:
Jeff Fischer
  • Field Details

  • Constructor Details

    • MvelHelper

      public MvelHelper()
  • Method Details

    • convertField

      public static Object convertField(String type, String fieldValue)
      Converts a field to the specified type. Useful when
      Parameters:
      type -
      fieldValue -
      Returns:
    • toUpperCase

      public static Object toUpperCase(String value)
    • currentTime

      public static String currentTime()
    • subtractFromCurrentTime

      public static String subtractFromCurrentTime(int days)
    • evaluateRule

      public static boolean evaluateRule(String rule, Map<String,Object> ruleParameters)
      Returns true if the passed in rule passes based on the passed in ruleParameters.

      Also returns true if the rule is blank or null.

      Calls the evaluateRule(String, Map, Map) method passing in the DEFAULT_EXPRESSION_CACHE. For systems that need to cache a large number of rule expressions, an alternate cache can be passed in. The default cache is able to cache up to 1,000 rule expressions which should suffice for most systems.

      Parameters:
      rule -
      ruleParameters -
      Returns:
    • evaluateRule

      public static boolean evaluateRule(String rule, Map<String,Object> ruleParameters, Map<String,Serializable> expressionCache)
      Evaluates the passed in rule given the passed in parameters.
      Parameters:
      rule -
      ruleParameters -
      Returns:
    • evaluateRule

      public static boolean evaluateRule(String rule, Map<String,Object> ruleParameters, Map<String,Serializable> expressionCache, Map<String,Class<?>> additionalContextImports)
      Parameters:
      rule -
      ruleParameters -
      expressionCache -
      additionalContextImports - additional imports to give to the ParserContext besides "MVEL" (MVEL and "MvelHelper" (MvelHelper) since they are automatically added
      Returns:
    • modifyExpression

      protected static String modifyExpression(String rule, Map<String,Object> ruleParameters, org.mvel2.ParserContext context)

      Provides a hook point to modify the final expression before it's built. By default, this looks for attribute maps and replaces them such that it does string comparison.

      For example, given an expression like getProductAttributes()['somekey'] == 'someval', getProductAttributes()['somekey'] actually returns a ProductAttribute object, not a String, so the comparison is wrong. Instead, we actually want to do this: getProductAttributes().?get('somekey').?value == 'someval'. This function performs that replacement

      The modification regex will support both simple and complex expressions like: "(MvelHelper.convertField("INTEGER",orderItem.?product.?getProductAttributes()["myinteger"])>0&&MvelHelper.convertField("INTEGER",orderItem.?product.?getProductAttributes()["myinteger"])<10)"

      Parameters:
      rule - the rule to replace
      Returns:
      a modified version of rule
    • unescapeUnicode

      protected static String unescapeUnicode(String input)
      Decodes unicode escapes like " into actual characters.
    • getRuleAttributeMaps

      protected static String[] getRuleAttributeMaps()
      Returns an array of attribute map field names that we need to do replacements for in modifyExpression(String, Map, ParserContext)
    • setTestMode

      public static void setTestMode(boolean testMode)
      When true, LOG.info statement will be suppressed. Should only be set from within MvelHelperTest. Prevents an error from displaying during unit test runs.
      Parameters:
      testMode -
    • buildMvelParameters

      public static Map<String,Object> buildMvelParameters()
      Builds parameters using time, request, customer, and cart.

      Should be called from within a valid web request.

      Returns:
    • populateParamsFromMap

      protected static void populateParamsFromMap(Map<String,Object> blcRuleMap, Map<String,Object> mvelParameters)