0 Replies Latest reply on Apr 17, 2009 3:22 PM by Charles Williams

    jumping to a new task node

    Charles Williams Newbie

      For various reasons within my app that is using JBPM I need to be able to abort all current tasks and jump to a different location in the process under certain circumstances (specifically if at any time the underlying data is updated). I have written a tag to do this though it isn't quite working the way I expected.

      The code will create the task I specify, but it sometimes create two of them, and sometimes will also create an additional task form my startnode. I know I must be missing something but I can't see what.

      Any help is greatly appreciated.

      public class RestartToNodeActionListener implements JbpmActionListener
      {
       private final ValueExpression processInstanceExpression;
       private final ValueExpression toNodeExpression;
      
       public RestartToNodeActionListener( final ValueExpression processInstance, final ValueExpression toNode )
       {
       processInstanceExpression = processInstance;
       toNodeExpression = toNode;
       }
      
       public String getName()
       {
       return "restartToNodeActionListener";
       }
      
       public void handleAction( JbpmJsfContext context, ActionEvent event )
       {
       try
       {
       final FacesContext facesContext = FacesContext.getCurrentInstance();
       final ELContext elContext = facesContext.getELContext();
       ProcessInstance processInstance = ( ProcessInstance ) processInstanceExpression.getValue( elContext );
       String toNodeName = ( String ) toNodeExpression.getValue( elContext );
      
       // get the task manager
       TaskMgmtInstance taskManager = processInstance.getTaskMgmtInstance();
      
       // get the old root token
       Token oldRoot = processInstance.getRootToken();
      
       // get the def so we can find the start node
       ProcessDefinition processDefinition = processInstance.getProcessDefinition();
      
       // get the start node to create the new root token
       Node startNode = processDefinition.getStartState();
      
       // get all tasks for the current instance
       List<TaskInstance> tasks = new ArrayList<TaskInstance>( taskManager.getTaskInstances() );
      
       int taskCount = tasks.size();
       for ( int i = 0; i < taskCount; i++ )
       {
       TaskInstance taskInstance = tasks.get( i );
      
       if ( taskInstance != null )
       {
       // remove each task instance
       taskManager.removeTaskInstance( taskInstance );
      
       // if the task is active cancel it
       if ( taskInstance.isOpen() && !taskInstance.isCancelled() )
       {
       // try to cancel the task instance
       taskInstance.cancel();
       }
       }
       }
      
       // added this because not all tasks were being canceled and was hoping it would be a catch all.
       taskManager.endAll();
      
       // get the task definition manager
       TaskMgmtDefinition taskManagerDefinition = taskManager.getTaskMgmtDefinition();
      
       // set the node we want to jump to
       Task startTask = taskManagerDefinition.getTask( toNodeName );
      
       // get the node to move the root token to
       Node startingNode = taskManagerDefinition.getProcessDefinition().getNode( toNodeName );
      
       // set the root token to point to the node
       oldRoot.setNode( startingNode );
      
       // create the new task instance
       TaskInstance newStartInstance = taskManager.createTaskInstance( startTask, oldRoot );
      
       // add in a start task
       taskManager.addTaskInstance( newStartInstance );
      
      
       context.selectOutcome("success");
       }
       catch ( Exception ex )
       {
       context.setError( "Error clearing tasks and resetting to task", ex );
       return;
       }
       }
      
      }