4 Replies Latest reply on Feb 12, 2008 11:19 AM by richard bradwell

    task-create event

    richard bradwell Newbie

      Hi,

      I have a start state with a task and some variables (see below). However I want to set the initial values of the variables. That's what I am try to do in the action called "SetupSuspiciousTransaction" using the task-create event. i.e. I'm trying to do some initial setup in my start state when I first create this task. The problem I'm having is that the call to processInstance.getTaskMgmtInstance().createStartTaskInstance(); doesn't seem to fire off the action. I assumed that during the creation of the start task my action would be called. It is getting called but I think it's getting called on taskInstance.end(); How would I do some initialisation before starting the task on the start-state?

       <start-state name="simulate suspicious transaction">
       <task name="Simulate Transaction" swimlane="Fraud Investigator">
       <controller>
       <variable name="transaction value" access="read,write,required"></variable>
       <variable name="account number" access="read,write,required"></variable>
       <variable name="transaction status" access="read,write,required"></variable>
       </controller>
       <event type="task-create">
       <action class="com.nr.tms.workflow.objectservices.actionhandlers.SetupSuspiciousTransaction"/>
       </event>
       </task>
       <transition name="submit" to="check transaction"></transition>
       </start-state>
      


        • 1. Re: task-create event
          richard bradwell Newbie

          A bit more information. It is actually calling my action from the task-create event, I was initially mistaken. Within that action I am setting the variables with something like that below.

           ContextInstance instance = executionContext.getContextInstance() ;
           instance.setVariable("id",id) ;
           instance.setVariable("transaction value",value) ;
           instance.setVariable("account number",accountNumber) ;
           instance.setVariable("notes",notes) ;
           instance.setVariable("transaction status",status) ;
          
          etc ...
          


          The returned task instance from the initial code

          processInstance.getTaskMgmtInstance().createStartTaskInstance()
          


          is returning NullInstance for the values of the variable instances. That I set above in the action tied to the task-create event.

          • 2. Re: task-create event
            Ronald van Kuijk Master

            can you make a unittest that demonstrates the problem? Easier to reproduce then...

            • 3. Re: task-create event
              richard bradwell Newbie

              Below is a simple unit test. What I am trying to do is set some values in the context on creation of the task in the start state. The event is firing and my action "SetupSuspiciousTransaction" is being run. This action sets variables in the context. Later on I try to retrieve the variables and there values in the method "retrieveVariables". When I step through this method "mappedName" has the right variable name but "value" has null as the value. I was expecting to get back the values I had put in earlier.

              import java.io.InputStream;
              import java.util.Iterator;
              import java.util.List;
              
              import org.jbpm.JbpmConfiguration;
              import org.jbpm.JbpmContext;
              import org.jbpm.context.def.VariableAccess;
              import org.jbpm.graph.def.ProcessDefinition;
              import org.jbpm.graph.exe.ProcessInstance;
              import org.jbpm.taskmgmt.def.TaskController;
              import org.jbpm.taskmgmt.exe.TaskInstance;
              import junit.framework.TestCase;
              
              public class TaskEventTestCase extends TestCase {
               static JbpmConfiguration jbpmConfiguration = null ;
              
               public TaskEventTestCase(){
               super("TaskEventTestCase") ;
               }
              
               static {
               InputStream is = ClassLoader.getSystemResourceAsStream("default.jbpm.cfg.xml");
               jbpmConfiguration = JbpmConfiguration.parseInputStream(is) ;
               }
              
               public void testSimplePersistence(){
               deployProcessDefinition() ;
               createStartTask() ;
               }
              
               public static void main(String[] args){
               new TaskEventTestCase().testSimplePersistence() ;
               }
              
               public void createStartTask(){
               JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext() ;
               try{
               jbpmContext.setActorId("Fraud Investigator");
               ProcessInstance processInstance = null;
               processInstance = jbpmContext.newProcessInstance("test");
               TaskInstance result = null;
               result = processInstance.getTaskMgmtInstance().createStartTaskInstance();
               jbpmContext.save(processInstance) ;
               retrieveVariables(result) ;
               }
               catch (Exception e){
               throw new RuntimeException(e.getMessage()) ;
               } finally {
               jbpmContext.close() ;
               }
               }
              
               protected void retrieveVariables(TaskInstance ti){
               TaskController taskController = ti.getTask().getTaskController();
               if (taskController!=null) {
               List variableAccesses = taskController.getVariableAccesses();
               Iterator iter = variableAccesses.iterator();
               while (iter.hasNext()) {
               VariableAccess variableAccess = (VariableAccess) iter.next();
               String mappedName = variableAccess.getMappedName();
               String value = (String)ti.getVariable(mappedName);
               }
               }
               }
               public void deployProcessDefinition(){
               ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
               "<process-definition name='test'>" +
               "<start-state name='state1'>" +
               "<task name='Investigate Transaction' swimlane='Fraud Investigator'>" +
               "<controller>" +
               "<variable name='transaction value' access='read,write,required'></variable>" +
               "<variable name='account number' access='read,write,required'></variable>" +
               "<variable name='transaction status' access='read,write,required'></variable>" +
               "</controller>" +
               "<event type='task-create'>" +
               " <action class='com.nr.tms.workflow.objectservices.actionhandlers.SetupSuspiciousTransaction'/>" +
               "</event>" +
               "</task>" +
               "<transition name='default' to='end1'></transition>" +
               "</start-state>" +
               "<end-state name='end1'></end-state>" +
               "</process-definition>"
               ) ;
              
               JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext() ;
               try{
               jbpmContext.deployProcessDefinition(processDefinition) ;
               } finally {
               jbpmContext.close() ;
               }
               }
              }
              
              


              • 4. Re: task-create event
                richard bradwell Newbie

                Sorry here is the code for the action ...

                public class SetupSuspiciousTransaction implements ActionHandler{
                
                 public void execute(ExecutionContext executionContext){
                
                 ContextInstance instance = executionContext.getContextInstance() ;
                 instance.setVariable("id","1") ;
                 instance.setVariable("transaction value","test transaction value") ;
                 instance.setVariable("account number","test account number") ;
                 instance.setVariable("notes","test notes") ;
                 instance.setVariable("transaction status","test transaction status") ;
                 }
                
                }