Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. This Developer Center contains the guides for Commerce 6.13.0 through 7.4.1.Visit new site

This version of Elastic Path Commerce is no longer supported or maintained. To upgrade to the latest version, contact your Elastic Path representative.

Creating conditional expressions

Creating conditional expressions

A condition is a rule consisting of a tag name, an operator, and a value. A conditional expression consists of one or more conditions joined by a concatenation operator (AND, OR, and NOT).

Usually, conditional expressions are built by application users through the Using the Condition Builder UI widget in the Commerce Manager UI. Conditional expressions can also be built programmatically. The following code uses the ConditionHandler service to create two conditions.

import com.elasticpath.tags.domain.Condition;
import com.elasticpath.tags.domain.LogicalOperator;
import com.elasticpath.cmclient.conditionbuilder.wizard.conditions.handlers.ConditionBuilder;
import com.elasticpath.tags.service.ConditionDSLBuilder;
...


ConditionHandler conditionHandler = new ConditionHandler();

// Create the female shopper condition
String tagName1 = "CUSTOMER_GENDER";
String op1 = "equalTo";
String value1 = "F";
Condition condition1 = conditionHandler.buildCondition(tagName1, op1, value1);

// Create the age over 65 condition
String tagName2 = "CUSTOMER_AGE_YEARS";
String op2 = "greaterThan";
Integer value2 = new Integer(65);
Condition condition2 = conditionHandler.buildCondition(tagName2, op2, value2);

The buildCondition method creates a Condition object. The first parameter must be a valid Tag definitions name. An InvalidArgumentException is thrown if the name does not match a tag definition in the TTAGDEFINITION table. The second parameter must be an operator that is valid for the specified tag's Tag value types. For example, the greaterThan operator is not valid for the TARGET_URL tag.

Note:

The ConditionalOperatorLookupService can be used to retrieve the list of operators that are valid for a given tag.

The third parameter is the value you want to compare to the value in the tag set. The type of the value must match the data type for that tag's tag value type as specified in the TTAGVALUETYPE table.

After the condition or conditions have been created, they need to be joined and added to a conditional expression. This is required even when there is only one condition. The following code builds a conditional expression by joining the two conditions created in the previous example using the AND operator.

// Create an AND operator to join the two conditions
LogicalOperator logicalOp = new LogicalOperator(LogicalOperatorType.AND);
logicalOp.addCondition(condition1);
logicalOp.addCondition(condition2);

// Create a string containing the AND-ed conditions
ConditionDSLBuilder dslBuilder = (ConditionDSLBuilder)
		getBeanByName(ContextIdNames.TAG_CONDITION_DSL_BUILDER);
String exprString = dslBuilder.getConditionalDSLString(logicalOp);

// Create the conditional expression from the expression string
ConditionalExpression condExpr = new ConditionalExpression();
condExpr.setConditionString(exprStr);
condExpr.setName("Female shoppers over 65");

The conditional expression can then be used by the ConditionEvaluatorService to Evaluating conditional expressions.