Example: campaign element task command for a trigger (daily check type)

When you create a custom trigger for a marketing activity, you can refer to this sample when you develop the trigger's task command. This example trigger is a daily check trigger.

Sample

Here is the task command implementation code for the example trigger. This gift registry trigger finds all customers for whom the current date is a specified number of days before a gift registry event. Marketing Managers could use this trigger to communicate with customers who set up a store gift registry, for example, a bride-to-be. At a specified number of days before the registry event (for example, 30 days before the wedding), the Marketing Manager could use this trigger to start a Dialog activity that sends an update email to the bride-to-be.

package com.mycompany.commerce.marketing.commands.elements;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.ibm.commerce.base.objects.ServerJDBCHelperBean;
import com.ibm.commerce.emarketing.utils.EmailActivityStorePathHelper;
import com.ibm.commerce.foundation.common.exception.ApplicationError;
import com.ibm.commerce.foundation.common.util.logging.LoggingHelper;
import com.ibm.commerce.marketing.commands.elements.MarketingCampaignElementTaskCmdImpl;
import com.ibm.commerce.marketing.dialog.util.MarketingUtil;
import com.ibm.commerce.marketing.runtime.engine.Activity;
import com.mycompany.commerce.marketing.logging.CustomMarketingMessageKeys;

public class GiftRegistryTriggerTaskCmdImpl extends MarketingCampaignElementTaskCmdImpl implements GiftRegistryTriggerTaskCmd {

	/**
	 * The name of this class.
	 */
	public final static String CLASSNAME = GiftRegistryTriggerTaskCmdImpl.class.getName();
	private static final Logger LOGGER = LoggingHelper.getLogger(GiftRegistryTriggerTaskCmdImpl.class);

	private final static String PARAM_NUMBER_OF_DAYS = "numberOfDays";

	/**
	 * This method validates that all the required name-value pairs have been set for
	 * the campaign element. It checks if the numberOfDays parameter is set.  
	 * @param elementParameters: The name-value pairs for this element.
	 * @return: This method returns a list of ApplicationError exceptions
	 * that contains any validation errors. The list may be empty or be null.
	 */
	public List validateParameters(Map elementParameters) {
		final String METHOD_NAME = "validateParameters";
		
		if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
			LOGGER.entering(CLASSNAME, METHOD_NAME, elementParameters);
		}	
		
		List validationErrors = new ArrayList();
		
		Object numberOfDays = elementParameters.get(PARAM_NUMBER_OF_DAYS);
		if (numberOfDays == null || numberOfDays.toString().length() == 0) {
			ApplicationError validateError = new ApplicationError(
					ApplicationError.TYPE_GENERIC_ERROR,
					CustomMarketingMessageKeys._APP_ACTIVITY_GIFT_REGISTRY_MISSING_NUMBER_OF_DAYS,
					null, LOGGER.getResourceBundleName());
			validationErrors.add(validateError);
		}
		
		if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
			LOGGER.exiting(CLASSNAME, METHOD_NAME, validationErrors);
		}		
		return validationErrors;
	}
	
	/**
	 * This method finds all the customers for whom the current date is
	 * the specified number of days before the customer's gift registry event date.
	 * A trigger is sent to the marketing services for each customer so that
	 * he or she will participate in the applicable Dialog activity. 
	 * If the activity is defined in a storefront asset store, then
	 * the customers are found in each extended site that refers to the storefront 
	 * asset store with a 'com.ibm.commerce.campaigns' relationship.
	 */
	public void forwardTriggersForProcessing() {
		final String METHOD_NAME = "forwardTriggersForProcessing";
		if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
			LOGGER.entering(CLASSNAME, METHOD_NAME);
		}
		boolean traceEnabled = LoggingHelper.isTraceEnabled(LOGGER);

		Activity activity = getActivity();
		Integer elementId = getElementId();
		Integer storeId = activity.getStoreId();
		int numberOfDays = Integer.parseInt(MarketingUtil.findElementNVPByElementIdAndName(elementId.toString(), PARAM_NUMBER_OF_DAYS));

		if (traceEnabled) {
			LOGGER.logp(Level.FINE, CLASSNAME, METHOD_NAME, "activityId=" + activity.getId());
			LOGGER.logp(Level.FINE, CLASSNAME, METHOD_NAME, "elementId=" + elementId);
			LOGGER.logp(Level.FINE, CLASSNAME, METHOD_NAME, "storeId=" + storeId);
			LOGGER.logp(Level.FINE, CLASSNAME, METHOD_NAME, "numberOfDays=" + numberOfDays);
		}
		
		try {
			// handle the activity in the store in which it is defined
			processGiftRegistryTrigger(storeId, elementId, activity.getId(), numberOfDays);
			
			// if this activity is defined in an asset store, then find the customers in each child store
			Integer[] storeIds = EmailActivityStorePathHelper.getStoresByRelatedStoreAndStoreRelType(
									storeId, com.ibm.commerce.server.ECConstants.EC_STRELTYP_CAMPAIGNS);
			
			if (storeIds != null){
				for (int j = 0; j < storeIds.length; j++) {
					if (!storeIds[j].toString().equals(storeId.toString())) {
						if (traceEnabled) {
							LOGGER.logp(Level.FINE, CLASSNAME, METHOD_NAME, "child storeId=" + storeIds[j]);
						}
						processGiftRegistryTrigger(storeIds[j], elementId, activity.getId(), numberOfDays);
					}
				} //end of for each store
			}
			
		} catch (Exception e) {
			if (traceEnabled) {
				LOGGER.logp(Level.FINE, CLASSNAME, METHOD_NAME, "Exception: " + e);
			}
		}
		
		if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
			LOGGER.exiting(CLASSNAME, METHOD_NAME);
		}		
		setReturnValue(true);
	}
	
	/**
	 * This method finds all the customers for whom the current date is
	 * the specified number of days before the customer's gift registry event date.
	 * The gift registry is associated with the provided store.
	 * A trigger is sent to the marketing services for each customer so that
	 * he or she will participate in the applicable Dialog activity. 
	 * @param storeId: The identifier of the store in which to find the applicable gift registries.
	 * @param elementId: The identifier of the trigger element.
	 * @param activityId: The identifier of the marketing activity.
	 * @param numberOfDays: The number of days before the gift registry event date.
	 */
	public void processGiftRegistryTrigger(Integer storeId, Integer elementId, Integer activityId, int numberOfDays) throws Exception{
		final String METHODNAME = "processGiftRegistryTrigger";
		if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
			LOGGER.entering(CLASSNAME, METHODNAME, new Object [] {storeId, elementId, activityId});
		}
		
		/*
		 * The userList is a list with the customer information. Every element in 
		 * the list is a child list; in the child list, the first element is the customer's 
		 * member ID, and the second element is the customer's personalization ID.
		 * It is recommended to use the method ServerJDBCHelperBean().executeQuery()
		 * as this will return the list in the required format. The SQL used should select
		 * the member ID and personalization ID as the first two columns.
		 * (for example: SELECT USERDEMO.USERS_ID, USERS.PERSONALIZATIONID 
		 * FROM USERDEMO USERDEMO, USERS USERS AND USERDEMO.USERS_ID=USERS.USERS_ID AND ...) 
		 */
		String sql = ""/* SQL NOT PROVIDED */;
             List userList = SessionBeanHelper.lookupSessionBean(ServerJDBCHelperBean.class).executeQuery(sql);
		
		// use this method to have the marketing services send the trigger for each customer in the user list
		forwardTriggersForProcessing(userList, activityId, elementId, storeId);
		
		if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
			LOGGER.exiting(CLASSNAME, METHODNAME);
		}
	}	
	
}