package com.acme.intg.processes;

import com.bea.control.ServiceBrokerControl;
import com.bea.jpd.JpdContext;
import com.bea.data.RawData;
import com.bea.wli.control.broker.MessageBroker;
import com.bea.wli.jpd.Callback;
import com.bea.wli.jpd.CallbackInterface;
import com.bea.xml.XmlObjectList;
import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.controls.api.events.EventHandler;
import org.apache.xmlbeans.XmlObject;
import com.acme.logging.AppLogger;
import com.acme.businessOps.MessagePersistenceLogger;

/**
 * @author Selvan V. Rajan
 * @version 1.0 
 * 
 * @jpd:process process::
 * <process name="process">
 *   <clientRequest name="Client Request with Return" method="clientRequestwithReturn" returnMethod="clientReturn">
 *     <perform name="PersistMessage" method="persistMessage"/>
 *     <controlSend name="NonXML to XML Conversion" method="callNonXMLToXMLClientRequest">
 *       <onException name="OnException">
 *         <perform name="Conversion Error" method="logErrorMsg"/>
 *       </onException>
 *     </controlSend>
 *     <controlSend name="Credit Card Validation Process" method="validateCCClientRequest"/>
 *     <controlSend name="XML to Non-XML Conversion" method="outXMLToNonXMLConvertXMLtoNonXML"/>
 *     <perform name="Persist Out Message" method="persistOutMessage"/>
 *   </clientRequest>
 * </process>::
 */
@com.bea.wli.jpd.Process(process = "<process name=\"process\"> " +
                                   "  <clientRequest name=\"Client Request with Return\" method=\"clientRequestwithReturn\" returnMethod=\"clientReturn\"> " +
                                   "    <perform name=\"PersistMessage\" method=\"persistMessage\"/> " +
                                   "    <controlSend name=\"NonXML to XML Conversion\" method=\"callNonXMLToXMLClientRequest\"> " +
                                   "      <onException name=\"OnException\"> " +
                                   "        <perform name=\"Conversion Error\" method=\"logErrorMsg\"/> " +
                                   "      </onException> " +
                                   "    </controlSend> " +
                                   "    <controlSend name=\"Credit Card Validation Process\" method=\"validateCCClientRequest\"/> " +
                                   "    <controlSend name=\"XML to Non-XML Conversion\" method=\"outXMLToNonXMLConvertXMLtoNonXML\"/> " +
                                   "    <perform name=\"Persist Out Message\" method=\"persistOutMessage\"/> " +
                                   "  </clientRequest> " +
                                   "</process>")
public class CreditCardProcess implements com.bea.jpd.ProcessDefinition
{
    public com.bea.data.RawData outRawMessage;

    public com.acme.validationService.ValidationMessageDocument outboundXMLDoc;

    public com.acme.authorizationService.AuthorizationMessageDocument inboundXMLDoc;

    public com.bea.data.RawData rawMessage;

    static final long serialVersionUID = 1L;
    
    @com.bea.wli.jpd.Callback()
    public Callback callback;
    
    /*
     * The area above this comment contains Variable Declarations created from the Design View.
     * The area below this comment contains Control Declarations created from the Design View.
     * 
     * Code in these areas is generated automatically.
     * 
     * 2-Way editing is fully supported in these areas of code.
     * Warning: changing Process Variables and Control Declarations already in use by your application
     * may generate errors in your application if you do not update these declarations
     * in all locations where they are used.
     */
    
    /**
     * @common:control
     */
    @Control()
    private com.acme.intg.controls.ConvertNonXMLToXMLPControl callNonXMLToXML;

    /**
     * @common:control
     */
    @Control()
    private com.acme.intg.controls.CreditCardValidationProcessPControl validateCC;

    /**
     * @jpd:transform
     */
    @com.bea.wli.jpd.Transform()
    private com.acme.intg.transformations.OutboundXMLToNonXML outXMLToNonXML;

    /**
     * @common:context
     */
    @com.bea.wli.jpd.Context()
    JpdContext context; //(..control insertion marker - do not modify this line)
    
    /*
     * The area below this comment contains java methods referenced by communication nodes in the Process Language.
     *
     * Code in this area is generated automatically.
     *
     * 2-Way editing is partially supported in this area of code.
     * Warning: you can safely add code inside a method body but it must be outside of any PROTECTED SECTION
     * block comments. Modifications to code within these comments will prevent you from viewing information
     * in the Design View builder that generated this code.
     */


    public void clientRequestwithReturn(com.bea.data.RawData x0)
    {
        //#START: CODE GENERATED - PROTECTED SECTION - you can safely add code above this comment in this method. #//
        // input transform
        // parameter assignment
        this.rawMessage = x0;
        //#END  : CODE GENERATED - PROTECTED SECTION - you can safely add code below this comment in this method. #//
    }





    public com.bea.data.RawData clientReturn()
    {
        //#START: CODE GENERATED - PROTECTED SECTION - you can safely add code above this comment in this method. #//
        // return
        return outRawMessage;
        //#END  : CODE GENERATED - PROTECTED SECTION - you can safely add code below this comment in this method. #//
    }




    public void callNonXMLToXMLClientRequest() throws Exception
    {
        //#START: CODE GENERATED - PROTECTED SECTION - you can safely add code above this comment in this method. #//
        // input transform
        // return method call
        this.inboundXMLDoc = callNonXMLToXML.clientRequest(this.rawMessage);
        // output transform
        // output assignments
        //#END  : CODE GENERATED - PROTECTED SECTION - you can safely add code below this comment in this method. #//
    }

    public void validateCCClientRequest() throws Exception
    {
        //#START: CODE GENERATED - PROTECTED SECTION - you can safely add code above this comment in this method. #//
        // input transform
        // return method call
        this.outboundXMLDoc = validateCC.clientRequest(this.inboundXMLDoc);
        // output transform
        // output assignments
        //#END  : CODE GENERATED - PROTECTED SECTION - you can safely add code below this comment in this method. #//
    }

    public void outXMLToNonXMLConvertXMLtoNonXML() throws Exception
    {
        //#START: CODE GENERATED - PROTECTED SECTION - you can safely add code above this comment in this method. #//
        // input transform
        // return method call
        this.outRawMessage = outXMLToNonXML.convertXMLtoNonXML(this.outboundXMLDoc);
        // output transform
        // output assignments
        //#END  : CODE GENERATED - PROTECTED SECTION - you can safely add code below this comment in this method. #//
    }

	@CallbackInterface()
    public interface Callback extends ServiceBrokerControl //(..region end marker - do not modify this line)
    {
    }
    
    /*
     * Code inserted below this comment is for methods corresponding to Perform or Condition nodes
     * created in the Design View.
     *
     * Feel free to make modifications or add new code here.
     */
    
    public static AppLogger logger = new AppLogger("Credit Card Processing");

    public void persistMessage() throws Exception
    {

      logger.info("CreditCardProcess.persistLogMessage - Begin");
      try
      {
        MessagePersistenceLogger.writeMessage( rawMessage, logger );
      }
      catch (Exception io)
      {
        //Do nothing as the error has already been logged in the MessagePersistenceLogger
        //The catch is needed to avoid null pointer exception
      }
      logger.info("CreditCardProcess.persistLogMessage - End");
        
    
    
    }

    public void logErrorMsg() throws Exception
    {
        logger.error("Conversion error: "+rawMessage);
    }

    public void persistOutMessage() throws Exception
    {
      logger.info("CreditCardProcess.persistOutMessage - Begin");
      try
      {
        MessagePersistenceLogger.writeMessage( outRawMessage, logger );
      }
      catch (Exception io)
      {
        //Do nothing as the error has already been logged in the MessagePersistenceLogger
        //The catch is needed to avoid null pointer exception
      }
      logger.info("CreditCardProcess.persistOutMessage - End");
        

    }
} 
