HCL Commerce Enterprise

Price rule element task commands

Each condition and action has an associated task command called a price rule element task command. The task command performs the work associated with the price rule element. The price rule element template definition must specify which task command to use.

Task commands serve several functions, as described in the following table:

Functions of task commands for price rule elements
Task command function Method called in the task command Applies to conditions? Applies to actions?
Validate the price rule element's parameters when the price rule is saved. validateParameters Yes, if needed. Yes, if needed.
Perform the work for the element in the price rule. performExecute Yes, mandatory. Yes, mandatory.

Format of a task command

A price rule element task command must be a class that implements the com.ibm.commerce.price.rule.commands.element.PriceRuleElementCmd interface.

To understand the format of a task command, review the following code samples. The samples are for an example customized condition. The purpose of the condition is to determine whether the customer registered on, before, or after a specific date, and is therefore entitled to special prices.

This is an example of the task command interface class that extends from the PriceRuleElementCmd interface:

package com.mycompany.commerce.price.rule.commands.element;
import com.ibm.commerce.price.rule.commands.element.PriceRuleElementCmd;
public interface RegistrationTimeConditionElementCmd extends PriceRuleElementCmd {
	public final static String defaultCommandClassName =
		RegistrationTimeConditionElementCmdImpl.class.getName();
}

This is an example of a task command implementation that extends from the com.ibm.commerce.price.rule.commands.element.PriceRuleElementCmdImpl class. The snippets with numbers to the left are explained in more detail after the sample:

package com.mycompany.commerce.price.rule.commands.element;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import com.ibm.commerce.context.base.BaseContext;
import com.ibm.commerce.foundation.client.util.oagis.SelectionCriteriaHelper;
import com.ibm.commerce.foundation.common.exception.ApplicationError;
import com.ibm.commerce.foundation.common.util.logging.LoggingHelper;
import com.ibm.commerce.foundation.server.services.businesscontext.ContextService;
import com.ibm.commerce.foundation.server.services.businesscontext.ContextServiceFactory;
import com.ibm.commerce.member.facade.client.MemberFacadeClient;
import com.ibm.commerce.member.facade.datatypes.PersonalProfileType;
import com.ibm.commerce.price.rule.commands.element.PriceRuleElementCmdImpl;
import com.ibm.commerce.price.rule.runtime.util.PriceRuleConstants;

/*
 * This class is an implementation of PriceRuleElementCmdImpl for a custom Registration Time Condition in a price rule.
 */
public class RegistrationTimeConditionElementCmdImpl extends PriceRuleElementCmdImpl implements RegistrationTimeConditionElementCmd {

private static final String CLASSNAME = RegistrationTimeConditionElementCmdImpl.class
.getName();
private final String NameofRegistrationTimeParameter = "registrationTime";
private final String NameofRegistrationTimeOperatorParameter = "registrationTimeOperator";
/*
* Logger used for logging.
*/
private static final java.util.logging.Logger LOGGER = LoggingHelper
.getLogger(RegistrationTimeConditionElementCmdImpl.class);	
1
/*
* Validate that the input date and comparison operation are not null and ""
*/

public List validateParameters(Map elementParameters) {

final String METHODNAME = "validateParameters";
if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
LOGGER.entering(CLASSNAME, METHODNAME);
}
List validationErrors = new ArrayList();
if (elementParameters != null) {
String inputDate = (String) elementParameters
.get(NameofRegistrationTimeParameter);
String registrationTimeOperator = (String) elementParameters
.get(NameofRegistrationTimeOperatorParameter);
if (inputDate == null || inputDate.length() == 0) {
ApplicationError validationError = new ApplicationError(
(short) 0, "No input date", null, LOGGER
.getResourceBundleName());
validationErrors.add(validationError);
}
if (registrationTimeOperator == null
|| registrationTimeOperator.length() == 0) {
ApplicationError validationError = new ApplicationError(
(short) 0, "No comparison operator", null, LOGGER
.getResourceBundleName());
validationErrors.add(validationError);
}
if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
LOGGER.exiting(CLASSNAME, METHODNAME);
}
}
return validationErrors;

}
2
/*
* This method is to compare the registration date and input date by the operator, set the
* comparison result for returned object. Get registration date, input date
* and operator. Compare the two dates. Set result.
*/
public void performExecute() {
try {
final String METHODNAME = "performExecute";
if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
LOGGER.entering(CLASSNAME, METHODNAME);
}
Boolean conditionReturnValue = Boolean.FALSE;;
2a
// get person info from context
ContextService bcs = ContextServiceFactory.getContextService();
BaseContext baseContext = (BaseContext) bcs
.findContext(BaseContext.CONTEXT_NAME);
long userId = baseContext.getRunAsId();
Integer interg = baseContext.getStoreId();
MemberFacadeClient mfc = new MemberFacadeClient();
String xpathPerson = "{_wcf.ap=IBM_All}/Person[PersonIdentifier[(UniqueID="	+ userId + ")]]";
List personL = mfc.getPerson(
SelectionCriteriaHelper.STR_XPATH_LANG, xpathPerson);

if (personL != null && personL.size()> 0) {
PersonalProfileType personal = ((com.ibm.commerce.member.facade.datatypes.PersonType) personL
.get(0)).getPersonalProfile();
if (personal != null
&& personal.getRegistrationDateTime() != null) {
2b
// get actual registration time of the user
String registrationTime = personal
.getRegistrationDateTime().toString();
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date registrationdate = formatter.parse(registrationTime
.split("T")[0]);
2c
//  get the parameters set in the PRELEMENTATTR table
Map elementMap = this.getElementParameters();
if (elementMap != null) { 
String elementRegistrationTime = (String) elementMap
.get(NameofRegistrationTimeParameter);
Date inputdate = formatter
.parse(elementRegistrationTime.split("T")[0]);
String elementOperation = (String) elementMap
.get(NameofRegistrationTimeOperatorParameter);
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.logp(Level.FINER, CLASSNAME, METHODNAME,
"inputdate:" + inputdate
+ " registrationTime:"
+ registrationTime + " Operation"
+ elementOperation);
}	
2d
//determine if the condition evaluation is true or false
if (elementOperation.equals(">")
&& registrationdate.after(inputdate)) {
conditionReturnValue = Boolean.TRUE;
}
if (elementOperation.equals("<")
&& registrationdate.before(inputdate)) {
conditionReturnValue = Boolean.TRUE;
}
if (elementOperation.equals("=")
&& registrationdate.equals(inputdate)) {
conditionReturnValue = Boolean.TRUE;
}
}
}
}
2e
//  return true or false
this.getReturnedObject().put(
PriceRuleConstants.RETURNED_OBJECT_CONDITION,
conditionReturnValue);
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.logp(Level.FINER, CLASSNAME, METHODNAME,
" conditionReturnValue: " + conditionReturnValue);
}
if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
LOGGER.exiting(CLASSNAME, METHODNAME);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
  • 1 This snippet defines the validateParameters method to validate the condition parameters and return any errors.
  • 2 This snippet defines the performExecute method, which performs the evaluation work for the condition and returns true or false:
    • 2a This snippet gets the business context parameters for the customer.
    • 2b This snippet gets the actual registration time of the customer.
    • 2c This snippet calls the getElementParameters method to get the parameters that the task command uses to perform the evaluation. The parameters are defined as arguments in the price rule element task command.
    • 2d This snippet evaluates whether the condition should return true or false. For example, if the condition is customers who registered in February, then the condition should return true if the customer being evaluated registered in February.
    • 2e This snippet informs the pricing services whether the condition returned true or false.

About the methods used in a price rule element task command

This table provides more details about the typical methods used in price rule element task commands for conditions and actions. For a full list and description of available methods for task commands, see the com.ibm.commerce.price.rule.commands.element.PriceRuleElementCmdImpl class.

Price rule element Typical methods used
Condition
  • performExecute: This method evaluates whether the condition is met, for example, whether the catalog entry is from the Laptop Computer category. If so, the performExecute method returns true, and the path containing the condition is used to set pricing. If the performExecute method returns false, then WebSphere Commerce ignores the path and moves to the next path in the price rule. You can use other useful methods that extend from the PriceRuleElementCmdImpl parent class in the performExecute method to determine the condition (for example, getPriceRule method, getCatentryId method).
  • validateParameters: This method is optional; it validates that the business user has set all the required name-value pairs for the price rule element in the user interface, and can return error messages to the business user.
Action
  • performExecute: This method performs the action in the price rule. For example, the action might be to get a price from a price list or to calculate a price using a price equation.
  • validateParameters: This method is optional; it validates that the business user has set all the required name-value pairs for the price rule element in the user interface, and can return error messages to the business user.