public class MvelHelper extends Object
Modifier and Type | Field and Description |
---|---|
static String |
BLC_RULE_MAP_PARAM |
static String |
REQUEST_DTO |
Constructor and Description |
---|
MvelHelper() |
Modifier and Type | Method and Description |
---|---|
static Map<String,Object> |
buildMvelParameters()
Builds parameters using time, request, customer, and cart.
|
static Object |
convertField(String type,
String fieldValue)
Converts a field to the specified type.
|
static String |
currentTime() |
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[] |
getRuleAttributeMaps()
Returns an array of attribute map field names that we need to do replacements for in
modifyExpression(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.
|
static void |
setTestMode(boolean testMode)
When true, LOG.info statement will be suppressed.
|
static String |
subtractFromCurrentTime(int days) |
static Object |
toUpperCase(String value) |
public static final String BLC_RULE_MAP_PARAM
public static final String REQUEST_DTO
public static Object convertField(String type, String fieldValue)
type
- fieldValue
- public static String currentTime()
public static String subtractFromCurrentTime(int days)
public static boolean evaluateRule(String rule, Map<String,Object> ruleParameters)
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.rule
- ruleParameters
- public static boolean evaluateRule(String rule, Map<String,Object> ruleParameters, Map<String,Serializable> expressionCache)
rule
- ruleParameters
- public static boolean evaluateRule(String rule, Map<String,Object> ruleParameters, Map<String,Serializable> expressionCache, Map<String,Class<?>> additionalContextImports)
rule
- ruleParameters
- expressionCache
- additionalContextImports
- additional imports to give to the ParserContext
besides "MVEL" (MVEL
and
"MvelHelper" (MvelHelper
) since they are automatically addedprotected 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)"
rule
- the rule to replace#getRuleAttributeMaps()}
protected static String[] getRuleAttributeMaps()
modifyExpression(String, Map, ParserContext)
public static void setTestMode(boolean testMode)
testMode
- Copyright © 2022. All rights reserved.