Class MvelHelper
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionBuilds parameters using time, request, customer, and cart.static Object
convertField
(String type, String fieldValue) Converts a field to the specified type.static String
static boolean
evaluateRule
(String rule, Map<String, Object> ruleParameters) Returns true if the passed in rule passes based on the passed in ruleParameters.static boolean
evaluateRule
(String rule, Map<String, Object> ruleParameters, Map<String, Serializable> expressionCache) Evaluates the passed in rule given the passed in parameters.static boolean
evaluateRule
(String rule, Map<String, Object> ruleParameters, Map<String, Serializable> expressionCache, Map<String, Class<?>> additionalContextImports) protected static String[]
Returns an array of attribute map field names that we need to do replacements for inmodifyExpression(String, Map, ParserContext)
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.protected static void
static void
setTestMode
(boolean testMode) When true, LOG.info statement will be suppressed.static String
subtractFromCurrentTime
(int days) static Object
toUpperCase
(String value) protected static String
unescapeUnicode
(String input) Decodes unicode escapes like " into actual characters.
-
Field Details
-
BLC_RULE_MAP_PARAM
- See Also:
-
REQUEST_DTO
- See Also:
-
-
Constructor Details
-
MvelHelper
public MvelHelper()
-
-
Method Details
-
convertField
Converts a field to the specified type. Useful when- Parameters:
type
-fieldValue
-- Returns:
-
toUpperCase
-
currentTime
-
subtractFromCurrentTime
-
evaluateRule
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 theParserContext
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
Decodes unicode escapes like " into actual characters. -
getRuleAttributeMaps
Returns an array of attribute map field names that we need to do replacements for inmodifyExpression(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
Builds parameters using time, request, customer, and cart.Should be called from within a valid web request.
- Returns:
-
populateParamsFromMap
-