This section describes how a process run definition can be implemented.
What does this section cover?
In order to implement a process run definition, you need to:
- Create a new data object class extending com.crm.process.ProcessRunDefinition class.
- Create five new summary pages.
- Create a new data entry page.
- Create a new user interface class extending com.crm.process.processrun.CRMUIProcessRunDefinitionBean class.
- Create a new XML utility class extending com.crm.businessobject.processrun.ProcessRunDefinitionXMLUtilBean class, to save and load any criteria, action inputs and/or scheduling settings.
- Create the process class needed to execute the process run definition.
- Define the process in modules metadata file.
In the example below, we will implement a Rewards Settlements Run Definition which is used to evaluate customer events and spend transactions in order to debit or credit the related Organizational Unit's Accounts Receivable. The accounts receivable to be debited and/or credited are retrieved based on a set of criteria defined by the user. Rewards Settlements Run Definition, just like any other process run definition, can be executed ad hoc or on a recurring basis, based on the scheduling settings.
1. Create Data Object Class
The new data object class must extend com.crm.process.ProcessRunDefinition class and must override both getViewMandatoryFields() and getAssociatedObjects() methods. The associated objects will be used by setMandatoryFields() method of com.crm.dataobject.ViewObject for mandatory field calculation. This method calculates the associated objects' mandatory fields and along with the ones defined in getViewMandatoryFields() method it constructs the new data object's defaultMandatoryFields linked hash map.
A com.crm.process.utilities.SchedulerTask object must be defined as well, in order for the process run definition to be handled by the scheduler mechanism.
public class RewardSettlementRunDefinition extends ProcessRunDefinition{
private SchedulerTask schedulerTask;
//input settings
private RewardSettlementType rewardSettlementType;
//criteria
private Set<RewardSchemeCriterion> schemeCriteria;
private Integer daysAgo;
public SchedulerTask getSchedulerTask() {
return schedulerTask;
}
public void setSchedulerTask(SchedulerTask schedulerTask) {
this.schedulerTask = schedulerTask;
}
public RewardSettlementType getRewardSettlementType() {
return rewardSettlementType;
}
public void setRewardSettlementType(RewardSettlementType rewardSettlementType) {
this.rewardSettlementType = rewardSettlementType;
}
public Set<RewardSchemeCriterion> getSchemeCriteria() {
return schemeCriteria;
}
public void setSchemeCriteria(Set<RewardSchemeCriterion> schemeCriteria) {
this.schemeCriteria = schemeCriteria;
}
public Integer getDaysAgo() {
return daysAgo;
}
public void setDaysAgo(Integer daysAgo) {
this.daysAgo = daysAgo;
}
@Override
protected LinkedHashMap<String, CRMDO> getAssociatedDataObjects() {
LinkedHashMap<String, CRMDO> dataObjects=new LinkedHashMap<String, CRMDO>();
dataObjects.put("processRunDefinition", getProcessRunDefinition());
return dataObjects;
}
@Override
public LinkedHashMap<String, String> getViewMandatoryFields() {
LinkedHashMap<String, String> viewMandatoryFields = new LinkedHashMap<String, String>();
viewMandatoryFields.put( "rewardSettlementType", "key_reward_settlement_type");
return viewMandatoryFields;
}
public class RewardSchemeCriterion extends CRMDO {
/**
*
*/
private static final long serialVersionUID = 1L;
private CRMDORewardScheme scheme;
private String schemeID;
public CRMDORewardScheme getScheme() {
return scheme;
}
public void setScheme(CRMDORewardScheme scheme) {
this.scheme = scheme;
}
public String getSchemeID() {
return schemeID;
}
public void setSchemeID(String schemeID) {
this.schemeID = schemeID;
}
}
2. Create Summary Pages
Five new summary pages need to be implemented in order to give the user all the necessary information about the process run definition, its runs, and their results.
2.a. Process Run Definition Summary Page
The first summary page to create is the process run definition summary page, which is the main summary page that will display all the process run definition records.
As you can see, two drill-down summary pages must be created and included in the main summary page: process runs and previous version pages.
2.a.i. Process Runs Drilldown Summary Page
Process runs summary page shows all the process runs created by a specific process run definition.
As you can see, two drill-down summary pages must be created and included in process runs summary page: processed successfully and processed with errors pages.
2.a.i.1. Processed Successfully Summary Page
Processed successfully summary page shows all the entities that were processed successfully by a specific process run.
Processed with errors summary page shows all the entities that their processing, by a specific process run, failed.
2.b. Previous Version Summary Page
Previous version summary page shows the previous version of a specific process run.
3. Create Data Entry Page
A new data entry page must be implemented to give the user to create, view, modify, delete or apply other operations on a process run definition.
Note that it is recommended that you use the available global components for the process run:
- Name
- Alternative Code
- Description
- Life Cycle State
- Version
- Log Information
- Scheduling settings
Error rendering macro 'ui-expand' : Page loading failed
4. Create User Interface Class
The new user interface class must extend com.crm.process.processrun.CRMUIProcessRunDefinitionBean class.
Note that:
- When creating a new process run definition, the process and module attributes to set on the new data objects must be the same as the ones defined in modules metadata file.
- setMandatoryFields() method calculates the associated objects' mandatory fields and along with the ones defined in getViewMandatoryFields() method it constructs the new data object's defaultMandatoryFields linked hash map.
- To submit the process run definition to the scheduler for execution, use submit(CRMDOProcessRunLog processRunLog) method of com.crm.process.processrun.CRMUIProcessRunDefinitionBean class. This method uses process run definition's module and process attributes to retrieve the EJB and method name to call, from modules metadata file. For more information on how to schedule a process run, go to Schedule a Process Run.
@Stateless
@LocalBean
public class CRMUIRewardSettlementRunDefinitionBean extends CRMUIProcessRunDefinitionBean {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final String PROCESS_RUN_LOG_ENTRIES_EXIST = "PROCESS_RUN_LOG_ENTRIES_EXIST";
private static final String IS_PROCESS_RUN_DEF_LATEST_VERSION = "IS_PROCESS_RUN_DEF_LATEST_VERSION";
private static final String LATEST_PROCESS_RUN_DEF_ID = "LATEST_PROCESS_RUN_DEF_ID";
private static final String PROCESS ="REWARD_SETTLEMENT_PROCESSING";
private static final String MODULE ="REWARDS";
@EJB private CRMBOProcessRunDefinitionBean processRunDefinitionBean;
@EJB private RewardSettlementProcessRunDefinitionXMLUtilBean rewardSettlementProcessRunDefinitionXMLUtilBean;
@EJB private CRMBOProcessRunLogBean processRunLogBean;
@EJB private CRMBORewardSchemeBean rewardSchemeBean;
@EJB private CRMValidatorBean validatorBean;
@EJB private CRMBORewardDefinitionBean rewardDefinitionBean;
@EJB private RewardDefinitionXMLUtilBean rewardDefinitionXMLUtilBean;
/**
* @see CRMUIProcessRunDefinitionBean#CRMUIProcessRunDefinitionBean()
*/
public CRMUIRewardSettlementRunDefinitionBean() {
//do nothing
}
/**
* Loads a reward settlement run definition form.
*
* @param id - the reward settlement run definition id to load
* @return the loaded form
* @throws Exception
*/
public RewardSettlementRunDefinition loadForm(String id) throws Exception {
//Load the process run definition from db
CRMDOProcessRunDefinition definition = (CRMDOProcessRunDefinition)processRunDefinitionBean.load(id);
//Set process run definition additional information
definition = processRunDefinitionBean.loadAdditionalInformation(definition);
//Create new RewardSettlementRunDefinition instance and set process run definition object
RewardSettlementRunDefinition rewardSettlementRunDefinition = new RewardSettlementRunDefinition();
rewardSettlementRunDefinition.setProcessRunDefinition(definition);
//Load criteria input and scheduling settings from XML
rewardSettlementRunDefinition = rewardSettlementProcessRunDefinitionXMLUtilBean.loadFromXML(rewardSettlementRunDefinition);
//Initialise criteria
rewardSettlementRunDefinition = rewardSettlementProcessRunDefinitionXMLUtilBean.initialize(rewardSettlementRunDefinition);
//Set aliases
if (processRunLogBean.exists(rewardSettlementRunDefinition.getProcessRunDefinition()))
{
rewardSettlementRunDefinition.setAlias(PROCESS_RUN_LOG_ENTRIES_EXIST, "1");
}
else
{
rewardSettlementRunDefinition.setAlias(PROCESS_RUN_LOG_ENTRIES_EXIST, "0");
}
String latestVersionID = getLatestVersionID(rewardSettlementRunDefinition.getProcessRunDefinition());
if (latestVersionID==null || ( latestVersionID!=null && latestVersionID.equals(id)))
{
rewardSettlementRunDefinition.setAlias(IS_PROCESS_RUN_DEF_LATEST_VERSION, "1");
rewardSettlementRunDefinition.setAlias(LATEST_PROCESS_RUN_DEF_ID, latestVersionID);
}
else
{
rewardSettlementRunDefinition.setAlias(IS_PROCESS_RUN_DEF_LATEST_VERSION, "0");
rewardSettlementRunDefinition.setAlias(LATEST_PROCESS_RUN_DEF_ID, latestVersionID);
}
//Set mandatory fields
rewardSettlementRunDefinition.setMandatoryFields();
return rewardSettlementRunDefinition;
}
/**
* Creates a rewards settlement run definition.
*
* @return a rewards settlement run definition
* @throws Exception
*/
public RewardSettlementRunDefinition createButton() throws Exception {
RewardSettlementRunDefinition rewardSettlementRunDefinition = new RewardSettlementRunDefinition();
//Initialise attributes
rewardSettlementRunDefinition.setProcessRunDefinition((CRMDOProcessRunDefinition)processRunDefinitionBean.construct());
rewardSettlementRunDefinition.setSchedulerTask(new SchedulerTask());
rewardSettlementRunDefinition.setSchemeCriteria(constructRewardSchemeCriterion());
//Set process = process id as defined in modules metadata file
rewardSettlementRunDefinition.getProcessRunDefinition().setProcess(PROCESS);
//Set module = module id as defined in modules metadata file
rewardSettlementRunDefinition.getProcessRunDefinition().setModule(MODULE);
//Set type default value
rewardSettlementRunDefinition.setRewardSettlementType(RewardSettlementType.BOTH);
//Set the definition's mandatory fields
rewardSettlementRunDefinition.setMandatoryFields();
return rewardSettlementRunDefinition;
}
private Set<RewardSchemeCriterion> constructRewardSchemeCriterion() throws Exception {
Set<RewardSchemeCriterion> schemeCriteria = new HashSet<RewardSchemeCriterion>();
//schemeCriteria.add(new RewardSchemeCriterion());
return schemeCriteria;
}
/**
* Saves a rewards settlement run definition.
* @param rewardSettlementRunDefinition - the rewards settlement run definition to save
* @return the saved rewards settlement run definition
* @throws Exception
*/
public RewardSettlementRunDefinition saveButton(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception {
//Set and validate the definition's mandatory fields
rewardSettlementRunDefinition.setMandatoryFields();
validateMandatoryFields(rewardSettlementRunDefinition);
//Convert criteria, input settings and scheduling settings to XML and set them to process run definition data object.
rewardSettlementRunDefinition = rewardSettlementProcessRunDefinitionXMLUtilBean.constructXML(rewardSettlementRunDefinition);
CRMDOProcessRunDefinition processRunDefinition = rewardSettlementRunDefinition.getProcessRunDefinition();
//Validate process run definition
if(processRunDefinition.getCode()==null)
{
processRunDefinition.setCode(processRunDefinitionBean.getNextSequenceNumber(SequenceNumber.PROCESSRUNDEFINITIONCODES));
}
validateCriteria(rewardSettlementRunDefinition);
validateActionInput(rewardSettlementRunDefinition);
validateSchedulingSettings(rewardSettlementRunDefinition);
CRMDORewardDefinition rewardDefinition= (CRMDORewardDefinition)rewardDefinitionBean.loadActiveDefinition();
rewardDefinition = rewardDefinitionXMLUtilBean.initialize(rewardDefinition);
if(rewardDefinition.getSettlementAutomation()==null || rewardDefinition.getSettlementAutomation().getInvoiceType()==null || rewardDefinition.getSettlementAutomation().getInvoiceProduct() == null)
{
throw new CannotExecuteSettlementNoConfigurationInRewardDefinitionException(getCRMSession());
}
processRunDefinitionBean.validateAndSave( rewardSettlementRunDefinition.getProcessRunDefinition() );
String latestVersionID = getLatestVersionID(rewardSettlementRunDefinition.getProcessRunDefinition());
if (latestVersionID==null || ( latestVersionID!=null && latestVersionID.equals(rewardSettlementRunDefinition.getProcessRunDefinition().getId())))
{
rewardSettlementRunDefinition.setAlias(IS_PROCESS_RUN_DEF_LATEST_VERSION, "1");
}
else
{
rewardSettlementRunDefinition.setAlias(IS_PROCESS_RUN_DEF_LATEST_VERSION, "0");
}
return rewardSettlementRunDefinition;
}
/**
* Validate the criteria of the rewards settlement run definition
*
* @param notificationRunDefinition - the rewards settlement run definition to validate the criteria of
* @throws Exception
*/
protected void validateCriteria(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception{
if (ServiceUtil.isInitialized(rewardSettlementRunDefinition.getSchemeCriteria()) )
{
validatorBean.validateUniqueRecord(
"Scheme",
new ArrayList<CRMDO>(rewardSettlementRunDefinition.getSchemeCriteria()),
new String[]{"scheme.name"},
new String[]{"Name"});
}
}
/**
* Validate the scheduling settings of the rewards settlement run definition
*
* @param rewardSettlementRunDefinition - the rewards settlement run definition to validate the scheduling settings of
* @throws Exception
*/
protected void validateSchedulingSettings(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception{
}
/**
* Loads a reward run definition form.
*
* @param rewardSettlementRunDefinition - the rewards settlement run definition to load
* @return the loaded form
* @throws Exception
* @throws CannotModifyException
*/
public RewardSettlementRunDefinition editButton(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception, CannotModifyException {
CRMDOProcessRunDefinition definition = (CRMDOProcessRunDefinition)processRunDefinitionBean.load(rewardSettlementRunDefinition.getProcessRunDefinition().getId());
if(definition==null)
{
definition = processRunDefinitionBean.loadLatest(rewardSettlementRunDefinition.getProcessRunDefinition().getCode());
}
rewardSettlementRunDefinition = loadForm(definition.getId());
if(rewardSettlementRunDefinition.getAlias(PROCESS_RUN_LOG_ENTRIES_EXIST).equals("1"))
{
rewardSettlementRunDefinition = amendButton(rewardSettlementRunDefinition);
}
return rewardSettlementRunDefinition;
}
/**
* Amend a reward run definition.
*
* @param rewardSettlementRunDefinition - the rewards settlement run definition to amend
* @return the amended rewards settlement run definition
* @throws Exception
* @throws CannotModifyException
*/
public RewardSettlementRunDefinition amendButton(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception {
CRMDOProcessRunDefinition processRunDefinition = rewardSettlementRunDefinition.getProcessRunDefinition();
CRMDOProcessRunDefinition newProcessRunDefinition = (CRMDOProcessRunDefinition) processRunDefinition.clone();
newProcessRunDefinition.setVersion(new Integer(processRunDefinition.getVersion().intValue()+1));
rewardSettlementRunDefinition.setProcessRunDefinition(newProcessRunDefinition);
rewardSettlementRunDefinition.setAlias(PROCESS_RUN_LOG_ENTRIES_EXIST, "0" );
rewardSettlementRunDefinition.setAlias(IS_PROCESS_RUN_DEF_LATEST_VERSION, "1");
return rewardSettlementRunDefinition;
}
/**
* Submit a reward run definition.
*
* @param rewardSettlementRunDefinition - the rewards settlement run definition to submit
* @return the submitted form
* @throws Exception
* @throws CannotModifyException
*/
public RewardSettlementRunDefinition submitButton(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception {
CRMDORewardDefinition rewardDefinition= (CRMDORewardDefinition)rewardDefinitionBean.loadActiveDefinition();
rewardDefinition = rewardDefinitionXMLUtilBean.initialize(rewardDefinition);
if(rewardDefinition.getSettlementAutomation()==null || rewardDefinition.getSettlementAutomation().getInvoiceType()==null || rewardDefinition.getSettlementAutomation().getInvoiceProduct() == null)
{
throw new CannotExecuteSettlementNoConfigurationInRewardDefinitionException(getCRMSession());
}
//Create a process run log object and set the process run definition
CRMDOProcessRunLog processRunLog = (CRMDOProcessRunLog) processRunLogBean.construct(rewardSettlementRunDefinition.getProcessRunDefinition());
//submit the process run log for execution
submit(processRunLog);
return rewardSettlementRunDefinition;
}
/**
* Validate the action input of the rewards settlement run definition
*
* @param definition - the rewards settlement run definition to validate the action input of
* @throws Exception
*/
protected void validateActionInput(RewardSettlementRunDefinition definition) throws Exception{
if (definition.getRewardSettlementType()==null)
{
throw new MandatoryFieldException(getCRMSession(),"key_reward_settlement_type");
}
}
/**
* Delete a rewards settlement run definition.
*
* @param rewardSettlementRunDefinition - the rewards settlement run definition to delete
* @return the deleted form
* @throws Exception
* @throws CannotModifyException
*/
public RewardSettlementRunDefinition deleteButton(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception {
processRunDefinitionBean.validateAndDelete(rewardSettlementRunDefinition.getProcessRunDefinition());
return rewardSettlementRunDefinition;
}
/**
* Adds a list of reward schemes to the rewards settlement definition run.
*
* @param entityIDs - the ids of the reward schemes
* @return the list of reward schemes
* @throws Exception
*/
public ArrayList<CRMDO> addRewardSchemeCriterionButton(ArrayList<String> entityIDs) throws Exception {
ArrayList<CRMDO> dtos = new ArrayList<CRMDO>();
if (entityIDs!=null && entityIDs.size() > 0)
{
for (int i=0; i<entityIDs.size(); i++)
{
CRMDORewardScheme scheme = (CRMDORewardScheme)rewardSchemeBean.load(entityIDs.get(i));
RewardSchemeCriterion schemeCriterion=new RewardSchemeCriterion();
schemeCriterion.setScheme(scheme);
dtos.add(schemeCriterion);
}
}
return dtos;
}
/**
* Removes a list of reward scheme criteria from a rewards settlement run definition.
*
* @param dtoList - a list of reward scheme criteria to delete
* @return the updated list
* @throws Exception
*/
public ArrayList<CRMDO> removeRewardSchemeCriterionButton(ArrayList<CRMDO> dtoList) throws Exception {
return setDeleted(dtoList);
}
/**
* Loads the rewards settlement type options.
*
* @return the rewards settlement type options
* @throws Exception
*/
public LookupBuilder getRewardSettlementTypeOptions() throws Exception {
return getRewardSettlementTypeOptions(null);
}
/**
* Loads the rewards settlement type options.
*
* @param emptyValue - an empty value for a rewards settlement type
* @return the rewards settlement type options
* @throws Exception
*/
public LookupBuilder getRewardSettlementTypeOptions(String emptyValue) throws Exception {
LookupBuilder builder = new LookupBuilder();
if (emptyValue!=null)
{
builder.build("", emptyValue);
}
RewardSettlementType[] rewardSettlementTypes = RewardSettlementType.values();
for (int i=0; i<rewardSettlementTypes.length; i++)
{
RewardSettlementType rewardSettlementType = rewardSettlementTypes[i];
builder.build(rewardSettlementType.toString(), rewardSettlementType.getLabel());
}
return builder;
}
}
5. Create XML Utility Class
The new XML utility class must extend com.crm.businessobject.processrun.ProcessRunDefinitionXMLUtilBean class and will be used to save and load any criteria, action inputs, and scheduling settings.
/**
* Session Bean implementation class RewardSettlementProcessRunDefinitionXMLUtilBean
*/
@Stateless
@LocalBean
public class RewardSettlementProcessRunDefinitionXMLUtilBean extends ProcessRunDefinitionXMLUtilBean {
/**
*
*/
private static final long serialVersionUID = 1L;
private final String XML_CRITERIA_NODE = "criteria";
private final String XML_INPUT_SETTINGS_NODE = "inputsettings";
private final String XML_SCHEME_CRITERIA_NODE = "schemes";
private final String XML_SCHEME_NODE = "scheme";
private final String XML_SCHEMEID_NODE = "schemeid";
private final String XML_DAYS_AGO_NODE = "daysago";
private final String XML_SETTLEMENT_TYPE_NODE = "settlementtype";
@EJB private CRMBORewardSchemeBean schemeBean;
@EJB private CRMBOProductBean productBean;
@EJB private CRMBOFinancialTransactionTypeBean financialTransactionTypeBean;
/**
* @see ProcessRunDefinitionXMLUtilBean#ProcessRunDefinitionXMLUtilBean()
*/
public RewardSettlementProcessRunDefinitionXMLUtilBean() {
}
public RewardSettlementRunDefinition initialize(RewardSettlementRunDefinition definition) throws Exception {
// Initialises the rewards schemes criteria
definition.setSchemeCriteria(initializeSchemeCriteria(definition.getSchemeCriteria()));
return definition;
}
public Set<RewardSchemeCriterion> initializeSchemeCriteria(Set<RewardSchemeCriterion> schemeCriteria) throws ParserConfigurationException, SAXException, IOException,Exception {
//Create a set of rewards scheme criteria
Set<RewardSchemeCriterion> schemes = new HashSet<RewardSchemeCriterion>();
if(schemeCriteria!=null && schemeCriteria.size() > 0)
{
Iterator<RewardSchemeCriterion> iter = schemeCriteria.iterator();
while (iter.hasNext())
{
RewardSchemeCriterion schemeCriterion = iter.next();
if (schemeCriterion.getSchemeID()!=null && schemeCriterion.getScheme() == null)
{
//Given its ID, load the rewards scheme for the db and set it to the scheme criterion
schemeCriterion.setScheme((CRMDORewardScheme)schemeBean.load(schemeCriterion.getSchemeID()));
}
//Add the initialised scheme criterion to the scheme criteria set
schemes.add(schemeCriterion);
}
}
return schemes;
}
public RewardSettlementRunDefinition loadFromXML(RewardSettlementRunDefinition definition) throws Exception {
definition = loadCriteriaFromXML(definition);
definition = loadInputSettingsFromXML(definition);
definition = loadSchedulingSettingsFromXML(definition);
return definition;
}
protected RewardSettlementRunDefinition loadInputSettingsFromXML(RewardSettlementRunDefinition definition) throws Exception {
//load the rewards settlement type from an XML formatted string field
definition.setRewardSettlementType(loadRewardSettlementTypeFromXML(definition.getProcessRunDefinition().getActionInputXML()));
return definition;
}
protected RewardSettlementRunDefinition loadCriteriaFromXML(RewardSettlementRunDefinition definition) throws Exception {
//load the rewards scheme criteria set from an XML formatted string field
definition.setSchemeCriteria(loadSchemeCriteria(definition.getProcessRunDefinition().getCriteriaXML()));
//load the days ago integer field from an XML formatted string field
definition.setDaysAgo(loadNumberFromXML(definition.getProcessRunDefinition().getCriteriaXML()));
return definition;
}
public Integer loadNumberFromXML(String xmlString) throws Exception {
Integer number = null;
if (xmlString!=null)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xmlString));
Document doc = (Document) builder.parse(is);
Element parent = (Element)doc.getFirstChild();
for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling())
{
if (child.getTextContent().length()>0)
{
if (child instanceof Element && child.getNodeName().equals(XML_DAYS_AGO_NODE))
{
if (child.getTextContent()!=null && child.getTextContent().length()>0)
{
number = new Integer(Integer.parseInt(child.getTextContent()));
}
}
}
}
}
return number;
}
public RewardSettlementType loadRewardSettlementTypeFromXML(String xmlString) throws Exception {
RewardSettlementType type = null;
if (xmlString!=null)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xmlString));
Document doc = (Document) builder.parse(is);
Element parent = (Element)doc.getFirstChild();
for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling())
{
if (child.getTextContent().length()>0)
{
if (child instanceof Element && child.getNodeName().equals(XML_SETTLEMENT_TYPE_NODE))
{
if (child.getTextContent()!=null && child.getTextContent().length()>0)
{
type=UnitOfTime.valueOf(RewardSettlementType.class,child.getTextContent());
break;
}
}
}
}
}
return type;
}
public Set<RewardSchemeCriterion> loadSchemeCriteria(String xmlString) throws ParserConfigurationException, SAXException, IOException {
Set<RewardSchemeCriterion> schemeCriteria =new HashSet<RewardSchemeCriterion>();
if(xmlString!=null)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xmlString));
Document doc = (Document) builder.parse(is);
Element parent = (Element)doc.getFirstChild();
for(Node child = parent.getFirstChild(); child != null; child = child.getNextSibling())
{
if(child.getTextContent().length()>0)
{
if(child instanceof Element && child.getNodeName().equals(XML_SCHEME_CRITERIA_NODE))
{
NodeList schemes = child.getChildNodes();
if (schemes!=null && schemes.getLength() > 0)
{
for (int i=0;i<schemes.getLength();i++)
{
Node schemeNode = schemes.item(i);
if(schemeNode instanceof Element && schemeNode.getNodeName().equals(XML_SCHEME_NODE))
{
NodeList schemeValueNodes = schemeNode.getChildNodes();
RewardSchemeCriterion rewardSchemeCriterion = new RewardSchemeCriterion();
if (schemeValueNodes!=null && schemeValueNodes.getLength() > 0)
{
for (int j=0;j<schemeValueNodes.getLength();j++)
{
Node schemeValueNode = schemeValueNodes.item(j);
if(schemeValueNode instanceof Element && schemeValueNode.getNodeName().equals(XML_SCHEMEID_NODE))
{
rewardSchemeCriterion.setSchemeID(schemeValueNode.getTextContent());
}
}
}
schemeCriteria.add(rewardSchemeCriterion);
}
}
}
}
}
}
}
return schemeCriteria;
}
protected RewardSettlementRunDefinition loadSchedulingSettingsFromXML(RewardSettlementRunDefinition definition) throws ParserConfigurationException, SAXException, IOException, ParseException {
CRMDOProcessRunDefinition processRunDefinition = definition.getProcessRunDefinition();
if (processRunDefinition!=null)
{
//Use an XML formatted string field to load the scheduling settings object and set it to the process run definition
String xmlString = processRunDefinition.getSchSettingXML();
SchedulerTask schedulerTask = loadScheduleSettings(xmlString);
definition.setSchedulerTask(schedulerTask);
}
return definition;
}
public RewardSettlementRunDefinition constructXML(RewardSettlementRunDefinition definition) throws Exception {
definition = constructCriteriaXML(definition);
definition = constructInputSettingsXML(definition);
definition = constructSchedulingSettingsXML(definition);
return definition;
}
protected RewardSettlementRunDefinition constructInputSettingsXML(RewardSettlementRunDefinition definition) {
//Create input settings node
DOMElement inputSettingsNode = new DOMElement(XML_INPUT_SETTINGS_NODE);
//construct the rewards settlement type XML
DOMElement rewardSettlementTypeElement = constructRewardSettlementTypeXML(definition.getRewardSettlementType());
//add the rewards settlement type XML to the input settings node
inputSettingsNode.add(rewardSettlementTypeElement);
CRMDOProcessRunDefinition processRunDefinition = definition.getProcessRunDefinition();
processRunDefinition.setActionInputXML(inputSettingsNode.asXML());
return definition;
}
public DOMElement constructRewardSettlementTypeXML(RewardSettlementType type) {
//Create a new settlement type node and add the rewards settlement type to it
DOMElement typeElement = new DOMElement(XML_SETTLEMENT_TYPE_NODE);
if (type!=null)
{
typeElement.setText(type.toString());
}
return typeElement;
}
protected RewardSettlementRunDefinition constructCriteriaXML(RewardSettlementRunDefinition definition) {
//Create a new criteria node
DOMElement criteriaNode = new DOMElement(XML_CRITERIA_NODE);
//Construct the rewards scheme criteria XML
DOMElement schemeCriteriaElement = constructSchemeCriteriaXML(definition.getSchemeCriteria());
//Construct the days ago XML
DOMElement daysAgoElement = constructNumber(definition.getDaysAgo());
//Add the two XMLs to the criteria node
criteriaNode.add(schemeCriteriaElement);
criteriaNode.add(daysAgoElement);
CRMDOProcessRunDefinition processRunDefinition = definition.getProcessRunDefinition();
processRunDefinition.setCriteriaXML(criteriaNode.asXML() );
return definition;
}
public DOMElement constructNumber(Integer number) {
DOMElement daysAgoElement = new DOMElement(XML_DAYS_AGO_NODE);
if (number!=null)
{
daysAgoElement.setText(number.toString());
}
return daysAgoElement;
}
public DOMElement constructSchemeCriteriaXML(Set<RewardSchemeCriterion> schemeCriteria) {
DOMElement schemeCriteriaElement = new DOMElement(XML_SCHEME_CRITERIA_NODE);
if (schemeCriteria!=null && schemeCriteria.size() > 0)
{
Iterator<RewardSchemeCriterion> iter = schemeCriteria.iterator();
while (iter.hasNext())
{
RewardSchemeCriterion schemeCriterion = iter.next();
DOMElement schemeElement = new DOMElement(XML_SCHEME_NODE);
DOMElement schemeValueElement = new DOMElement(XML_SCHEMEID_NODE);
if (schemeCriterion.getScheme()!=null)
{
schemeValueElement.setText(schemeCriterion.getScheme().getId());
}
else if(schemeCriterion.getSchemeID()!=null)
{
schemeValueElement.setText(schemeCriterion.getSchemeID());
}
schemeElement.add(schemeValueElement);
schemeCriteriaElement.add(schemeElement);
}
}
return schemeCriteriaElement;
}
protected RewardSettlementRunDefinition constructSchedulingSettingsXML(RewardSettlementRunDefinition definition) {
DOMElement schedulerTaskNode = constructSchedulingSettingsXML(definition.getSchedulerTask() );
definition.getProcessRunDefinition().setSchSettingXML(schedulerTaskNode.asXML());
return definition;
}
}
6. Create Process Class
The new process class must extend com.crm.process.CRMProcessRunBean class.
Note that:
- A method having only one input parameter, the process run log ID, must be created. This method will be used to implement any business functionality required after the process run definition submission.
- If a multithreaded process is needed, please follow the instructions on how to Implement a multithreaded process.
/**
* Session Bean implementation class CRMProcessRewardSettlementBean
*/
@Stateless
@LocalBean
public class CRMProcessRewardSettlementBean extends CRMProcessRunBean {
/**
*
*/
private static final long serialVersionUID = 1L;
@EJB private RewardSettlementProcessRunDefinitionXMLUtilBean rewardSettlementProcessRunDefinitionXMLUtilBean;
@EJB private RewardDefinitionXMLUtilBean rewardDefinitionXMLUtilBean;
@EJB private CRMBOInvoiceBean invoiceBean;
@EJB private CRMBOCreditNoteBean creditNoteBean;
@EJB private CRMBOAccountReceivableBean accountReceivableBean;
@EJB private CRMBOUnitBean unitBean;
@EJB private CRMBORewardAwardTransactionBean rewardAwardTransactionBean;
@EJB private CRMBORewardSpendTransactionBean rewardSpendTransactionBean;
@EJB private CRMProcessFinTransactionBean processFinTransactionBean;
@EJB private CRMBORewardsParticipatingMerchantBean rewardsParticipatingMerchantBean;
@EJB private CRMBOAwardEventBean awardEventBean;
@EJB private CRMBORewardsParticipatingMerchantGroupBean rewardsParticipatingMerchantGroupBean;
@EJB private CRMBOUnitGroupBean unitGroupBean;
@EJB private CRMBORewardDefinitionBean rewardDefinitionBean;
/**
* @see CRMProcessRunBean#CRMProcessRunBean()
*/
public CRMProcessRewardSettlementBean() {
//do nothing
}
public void execute(String processRunLogID) throws Exception {
CRMDOProcessRunLog processRun = (CRMDOProcessRunLog)processRunLogBean.load(processRunLogID);
execute(processRun);
}
public void execute(CRMDOProcessRunLog processRun) throws Exception {
CRMDOProcessRunDefinition processRunDefinition = processRun.getProcessRunDefinition();
RewardSettlementRunDefinition rewardSettlementRunDefinition = new RewardSettlementRunDefinition();
rewardSettlementRunDefinition.setProcessRunDefinition(processRunDefinition);
rewardSettlementRunDefinition = rewardSettlementProcessRunDefinitionXMLUtilBean.loadFromXML(rewardSettlementRunDefinition);
rewardSettlementRunDefinition = rewardSettlementProcessRunDefinitionXMLUtilBean.initialize(rewardSettlementRunDefinition);
CRMDORewardDefinition rewardDefinition=(CRMDORewardDefinition)rewardDefinitionBean.loadActiveDefinition();
rewardDefinition=rewardDefinitionXMLUtilBean.initialize(rewardDefinition);
processRun.setStatus(ProcessRunLogStatus.IN_PROGRESS);
processRun.setStartDate(getCurrentDate());
processRunLogBean.save(processRun);
Boolean processComplete = new Boolean(false);
try
{
if(rewardSettlementRunDefinition!=null && rewardSettlementRunDefinition.getRewardSettlementType()!=null
&& !rewardSettlementRunDefinition.getRewardSettlementType().equals(RewardSettlementType.CREDIT) )
{
createInvoices(processRun, rewardSettlementRunDefinition,rewardDefinition);
createCreditNotesForPurchaseCustEvent(processRun, rewardSettlementRunDefinition,rewardDefinition);
processComplete = new Boolean(true);
}
if(rewardSettlementRunDefinition!=null && rewardSettlementRunDefinition.getRewardSettlementType()!=null
&& !rewardSettlementRunDefinition.getRewardSettlementType().equals(RewardSettlementType.DEBIT) )
{
createCreditNotes(processRun, rewardSettlementRunDefinition,rewardDefinition);
createInvoicesForSpendCustEvent(processRun, rewardSettlementRunDefinition,rewardDefinition);
processComplete = new Boolean(true);
}
}
catch(Exception e)
{
saveEntityLogRecord(processRun,"", "",ProcessRunLogEntityStatus.FAILED,e.getClass().getSimpleName(),ExceptionUtil.getStackTrace(e));
processRun.setStatus(ProcessRunLogStatus.FAILED);
processRunLogBean.save(processRun);
processRun = saveNextProcessRun(processRun);
}
if (processComplete)
{
processRun.setStatus(ProcessRunLogStatus.COMPLETED);
processRun.setEndDate(getCurrentDate());
processRun = saveNextProcessRun(processRun);
processRunLogBean.save(processRun);
}
}
...
}
To define the process in the modules metadata file:
- Create a new process entry under the right module (module id must be equal to the process run definition's module attribute) and give your process the right id (process id must be equal to the process run definition's process attribute).
- Set the value of <ejbname> to the name of the process class you created (step 4). Use the name defined in EJB binding file, after prefix 'ejb/' (ibm-ejb-jar-bnd.xml).
- Set the value of <methodname> to the name of the method used for executing the process run definition (step 4.1)
...
<module>
<moduleid>REWARDS</moduleid>
...
<feature>
...
<additionalprocesses>
<process>
<id>REWARD_SETTLEMENT_PROCESSING</id>
<name>key_execute_reward_settlement_run_definition</name>
<description>key_execute_reward_settlement_run_defintion</description>
<methods>
<method>
<ejbname>CRMProcessRewardSettlement</ejbname>
<methodname>execute</methodname>
<scheduled>true</scheduled>
</method>
</methods>
<batch>true</batch>
</process>
...
</additionalprocesses>
</feature>
</module>
...
<ejb-jar-bnd>
...
<session name="CRMProcessRewardSettlementBean" simple-binding-name="ejb/CRMProcessRewardSettlement" />
...
</ejb-jar-bnd>