package 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 javax.naming.*;

/**
 * @jpd:process process::
 * <process name="TaskCreation" persistent="always">
 *   <clientRequest name="Task Creation And Assignment" method="recieveTaskCreationRequest" returnMethod="sendTaskCreationConfirmation">
 *     <controlSend name="createAndAssignTask" method="taskCtrlCreateTask"/>
 *   </clientRequest>
 *   <eventChoice name="Event Choice">
 *     <messageEvent name="Message Event">
 *       <controlReceive name="onTaskCompleted" method="taskCtrl_onTaskCompleted"/>
 *     </messageEvent>
 *     <messageEvent name="Message Event">
 *       <controlReceive name="onTaskOverdue" method="taskCtrl_onTaskOverdue"/>
 *       <perform name="startAndStopTask" method="startAndStopTask"/>
 *     </messageEvent>
 *     <messageEvent name="Message Event">
 *       <controlReceive name="onTaskAborted" method="taskCtrl_onTaskAborted"/>
 *     </messageEvent>
 *   </eventChoice>
 * </process>::
 */
@com.bea.wli.jpd.Process(process = "<process name=\"TaskCreation\" persistent=\"always\"> " +
                                   "  <clientRequest name=\"Task Creation And Assignment\" method=\"recieveTaskCreationRequest\" returnMethod=\"sendTaskCreationConfirmation\"> " +
                                   "    <controlSend name=\"createAndAssignTask\" method=\"taskCtrlCreateTask\"/> " +
                                   "  </clientRequest> " +
                                   "  <eventChoice name=\"Event Choice\"> " +
                                   "    <messageEvent name=\"Message Event\"> " +
                                   "      <controlReceive name=\"onTaskCompleted\" method=\"taskCtrl_onTaskCompleted\"/> " +
                                   "    </messageEvent> " +
                                   "    <messageEvent name=\"Message Event\"> " +
                                   "      <controlReceive name=\"onTaskOverdue\" method=\"taskCtrl_onTaskOverdue\"/> " +
                                   "      <perform name=\"startAndStopTask\" method=\"startAndStopTask\"/> " +
                                   "    </messageEvent> " +
                                   "    <messageEvent name=\"Message Event\"> " +
                                   "      <controlReceive name=\"onTaskAborted\" method=\"taskCtrl_onTaskAborted\"/> " +
                                   "    </messageEvent> " +
                                   "  </eventChoice> " +
                                   "</process>")
public class TaskCreationSync implements com.bea.jpd.ProcessDefinition
{
    public com.bea.worklistSample.inputData.InputDataDocument inputDataXML;

    @com.bea.wli.jpd.Callback()
    public Callback callback;

    public java.lang.String taskId;

    static final long serialVersionUID = 1L;

    /*
     * 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
     * @jc:advanced completion-due-business-date="1min"
     */
    @Control()
    @com.bea.control.TaskAnnotations.TaskCreate(taskPlanId = @com.bea.control.TaskAnnotations.TaskPlanID
												(path = "/Worklist/Compatibility 8.1.x",
	                                             version = 9.0f,
	                                             worklistHostApplicationId = "worklist-ejbs-81x"),completionDueDate = @com.bea.control.TaskControl.DateTimeSpec(businessTime = @com.bea.control.TaskControl.BusinessTimeAnno(duration = "1min")))
	private processes.MyTaskCtrl taskCtrl;


    /**
     * @common:control
     */
    @Control()
    private processes.MyTaskWorkerCtrl taskWorkerCtrl;


    /**
     * This is a control instance, which will be instantiated automatically at
     * runtime.
     * @jpd:transform
     */
    @com.bea.wli.jpd.Transform()
    private processes.TaskCreationTransformation transformations;

    /**
     * @common:context
     */
    @com.bea.wli.jpd.Context()
    JpdContext context; //(..control insertion marker - do not modify this line)


    private long startTime;


    /*
     * 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.
     */


    @EventHandler(field = "taskCtrl",
                  eventSet = MyTaskCtrl.CallbackIntf.class,
                  eventName = "onTaskCompleted")
    public void taskCtrl_onTaskCompleted(XmlObject response)
    {
        //#START: CODE GENERATED - PROTECTED SECTION - you can safely add code above this comment in this method. #//
        // input transform
        // parameter assignment
        //#END  : CODE GENERATED - PROTECTED SECTION - you can safely add code below this comment in this method. #//
    }


    @EventHandler(field = "taskCtrl",
                  eventSet = MyTaskCtrl.CallbackIntf.class,
                  eventName = "onTaskAborted")
    public void taskCtrl_onTaskAborted(XmlObject response)
    {
        //#START: CODE GENERATED - PROTECTED SECTION - you can safely add code above this comment in this method. #//
        // input transform
        // parameter assignment
        //#END  : CODE GENERATED - PROTECTED SECTION - you can safely add code below this comment in this method. #//
    }


    @EventHandler(field = "taskCtrl",
                  eventSet = MyTaskCtrl.CallbackIntf.class,
                  eventName = "onTaskOverdue")
    public void taskCtrl_onTaskOverdue(java.util.Date time)
    {
        //#START: CODE GENERATED - PROTECTED SECTION - you can safely add code above this comment in this method. #//
        // input transform
        // parameter assignment
        //#END  : CODE GENERATED - PROTECTED SECTION - you can safely add code below this comment in this method. #//
    }












    /*
     * 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 void startAndStopTask() throws Exception
    {
        taskWorkerCtrl.startTask(this.taskId);
        taskWorkerCtrl.setTaskComment("Task processed. Overdue event handled.", this.taskId);
        taskWorkerCtrl.stopTask(this.taskId);

    }




    public void taskCtrlCreateTask() throws Exception
    {
        //#START: CODE GENERATED - PROTECTED SECTION - you can safely add code above this comment in this method. #//
        // input transform
        com.bea.wli.worklist.xml.TaskCreationXMLDocument obj = transformations.taskCtrlcreateTask(this.inputDataXML);
        //com.bea.wli.worklist.xml.Data f = com.bea.wli.worklist.xml.Data.Factory.parse(this.inputDataXML.toString());
        //obj.getTaskCreationXML().getRequest().setMessage(f);
        // return method call
        this.taskId = taskCtrl.createTask(obj);
        // output transform
        // output assignments
        //#END  : CODE GENERATED - PROTECTED SECTION - you can safely add code below this comment in this method. #//
    }



    public void recieveTaskCreationRequest(com.bea.worklistSample.inputData.InputDataDocument inputDataXML)
    {
        //#START: CODE GENERATED - PROTECTED SECTION - you can safely add code above this comment in this method. #//
        // input transform
        // parameter assignment
        this.inputDataXML = inputDataXML;
        //#END  : CODE GENERATED - PROTECTED SECTION - you can safely add code below this comment in this method. #//
    }




    public void sendTaskCreationConfirmation()
    {
        //#START: CODE GENERATED - PROTECTED SECTION - you can safely add code above this comment in this method. #//
        // return
        //#END  : CODE GENERATED - PROTECTED SECTION - you can safely add code below this comment in this method. #//
    }


	@CallbackInterface()
    public interface Callback extends ServiceBrokerControl
    {
    }




  private void printAndResetTimer(String msg)
  {
    long endTime = System.currentTimeMillis();
    System.out.println(msg + " = " + (endTime-startTime) + " ms");
    startTime = System.currentTimeMillis();
  }


}
