Example Handlers and Steps Implementation Outline

The following code and pseudo code outline example implementations for fixed workflow handlers and variable workflow steps.

Protocol Processing Handler

An outline of a fixed inbound protocol processing handler implementation, in this case a handler to support CSV processing. Protocol specific code should be added by the developer.

public class MyCSVProtocolProcess implements
                                      BusinessProcessHandlerInterface {
   public boolean applies(BusinessDocumentInterface document) {
      //obtain from_protocol from BusinessDocument
       if (from_protocol.equals("CSV_PROTOCOL"))
          return true;
       return false;
      }
  public BusinessDocumentInterface process(
                                   BusinessDocumentInterface document) {
   try {
        //obtain the file contents in a String
        StringTokenizer tokenizer = new StringTokenizer(fileContents, ",");
        String fromBusinessId = tokenizer.nextToken();
        if (fromBusinessId == null) {
                                           EventInfo event = new EventInfo();
           BusinessProcessUtil.logEvent(eventInfo);
        }
        String toBusinessId = tokenizer.nextToken();
        String fromPackaging = tokenizer.nextToken();
        String customerId = tokenizer.nextToken();
        String customerName = tokenizer.nextToken();
        String documentType = tokenizer.nextToken();
        String documentVersion = tokenizer.nextToken();
         ... 
        //log obtained parameters
         ...
         document.setValue(DocumentConstant.FRBUSINESSID, fromBusinessId);
         document.setValue(DocumentConstant.TOBUSINESSID, toBusinessId);
         document.setValue(DocumentConstant.INITBUSINESSID, customerId);
         document.setValue(DocumentConstant.FRPROTOCOLNAME,
                                                "CSV_PROTOCOL ");
         document.setValue(DocumentConstant.FRPROCESS, documentType);
         document.setValue(DocumentConstant.FRPROCESSVER, documentVersion);
          ...
          document.setDocumentState(BusinessProcessUtil.BCG_DOC_IN_PROCESS);
      } catch (Exception e) {
                                EventInfo event = new EventInfo();
        document.addEvent(event);
        document.setDocumentState(BusinessProcessUtil.BCG_DOC_FAILED);
         } 
          return document;
      }
  }

Protocol Unpackaging Handler

An outline of a fixed inbound protocol unpackaging handler implementation, in this case a handler to support custom XML packaging from Websphere Commerce Business Edition. Protocol specific code should be added by the developer.

public class WCBEXMLProtocolUnpackagingHandler implements
                                    BusinessProcessHandlerInterface {
 
   private Context m_context = null;
   private rConfig m_config = null;
 
   public void init(Context context,Config config) {
              this.m_context = context;
      this.m_config = config;
      return;
    }
    public boolean applies() {
          //obtain schema for this XML
          if (schemaLocation.startsWith("http://www.ibm.com/WCBE/schemas/"))
                 return true;
           return false;
    }
 
    public BusinessDocumentInterface process(
                                    BusinessDocumentInterface document) {
          try {
                 document.setValue(DocumentConst.FRPACKAGING, "WCBEPackaging");
                 /*
                  * Parse the file and obtain the following information:
                  * 1. Recipient Name
                  * 2. Type of document - PurchaseOrder, RFQ
                  */
 
            //obtain receiver_id
            PartnerService partnerService = BusinessProcessUtil.getPartnerService();
            String receiver_id = partnerService.getBusinessId(recipientName,
                                    PartnerService.COMMUNITY_PARTICIPANT);
                 if (receiver_id == null) {
             EventInfo event = new EventInfo();
                     BusinessProcessUtil.logEvent(msg);
             document.setDocumentState(
                       BusinessProcessUtil.BCG_DOCSTATE_FAILED);
                 }
                document.setValue(DocumentConstant.TOBUSINESSID, receiver_id);
                document.setValue(DocumentConst.FRPROCESSCD, documentType);
 
    document.setDocumentState(BusinessProcessUtil.BCG_DOCSTATE_IN_PROCESS);
       }catch (Exception e) {
            EventInfo event = new EventInfo();
            document.addEvents(event);
document.setDocumentState(BusinessProcessUtil.BCG_DOCSTATE_FAILED);
 }
         return document;
     } 
}

Validation Step

An outline of a variable workflow step implementation, in this case a step to validate a WCBE purchase order document. It is in two parts. The first part implements the Factory interface, BusinessProcessFactoryInterface and the second part implements the Process interface, BusinessProcessInterface. Protocol specific code should be added by the developer.

The Factory class:

public class WCBEXMLValidationFactory implements
                                   BusinessProcessFactoryInterface {
 
    public BusinessDocumentInterface getBusinessProcess(Context context, 
                                                    Config config, 
                                           BusinessDocumentInterface bDoc)
{
 
// Can use any configuration values from config as necessary.  These
//are set via the Console.
 
         WCBEValidationBusinessProcess bp = new WCBEValidationBusinessProcess();
            // Set any items in this class as specific to the implementation
// between the factory and the business process class.
 
 
            return bp;
     }
 
public static void returnBusinessProcess(BusinessProcessInterface bp)
                                       throws BCGWorkflowException {
      // if not reusing Business Processes then do nothing.
 }
}

The Process class:

public class WCBEValidationBusinessProcess implements
                                        BusinessProcessInterface {
 
    public BusinessDocumentInterface process(BusinessDocumentInterface bDoc,
                                         Context context) {
           /*
            * Obtain document's contents.
            */
         File document = bDoc.getDocument();
   // Read in file.
 
 
           //obtain validation map
         MapService mapService = BusinessProcessUtil.getMapService();
         byte[] fromValidationMap = mapService.getFromValidationMap(bDoc, context);
 
   /* Obtain a validating XML parser instance.
    * Set the validation map location in the parser.
            * Validate the XML by parsing it.
            */ 
 
 
/*
         * Validate the PurchaseOrder:
 *
         * if document type is PurchaseOrder
         *     check if there is at least one orderitem
         *             if there is atleast one orderitem 
         *                    check if the quantity ordered is atleast one
         *                    if the quantity ordered is less than 1
         *                           set document status to DOC_FAILED
         *                           throw BCGInvalidDocumentException
         *             else
         *                     set document status to DOC_FAILED
         *                     throw BCGInvalidDocumentException
         * 
         */
 
 return bDoc
    }
 
    public boolean reset() {
        /* 
         * reset internal variables.
         */
     }
}

Transformation Step

An outline of a variable workflow step implementation, in this case a a step to transform a document from one format to another. This sample only includes code and pseudo code for the Process implementation, but a related Factory is also necessary. Protocol specific code should be added by the developer.

The Process class:

public class WCBETransformationBusinessProcess implements
                                                BusinessProcessInterface {
 
    public BusinessDocumentInterface process(BusinessDocumentInterface bDoc,
                                           Context context) {
            //obtain transformation map
             MapService mapService = BusinessProcessUtil.getMapService();
              byte[] transformationMap = mapService.getTransformationMap(
                                                       bDoc, context);
    // Transformer is for example only and is part of customer 
    // implementation.
     Transformer transformer = new Transformer(transformationMap);
 
  // Get the Business document file.
    File message = bDoc.getDocument();
  //get contents of File 
    byte[] transformOutput = transformer.transform(fileContents);
    File transformedFile = bDoc.createFile();
  //write transformedOutput to transformedFile
   bDoc.setDocument(transformedFile);
         return bDoc;
    } 
 
    public boolean reset() {
         /* 
          * reset internal variables.
          */
      }
}

Copyright IBM Corp. 2003, 2004