Here is a method I use (in my own framework), to re-route a token.
It may be simpler for you, but the way we do things in our app, this worked for 
us. Hopefully you can use it or get some ideas:
private void rerouteToken (HttpServletRequest request, ActionErrors errors, 
ActionMessages messages, ProcessInstance processInstance) {
  |             User currentUser = getUser(request);
  |             String tokenId = 
request.getParameter(Constants.PROCESS_ADMIN_ACTION_TOKEN_ID);
  |             String nodeName = 
request.getParameter(Constants.PROCESS_ADMIN_ACTION_NODE_NAME);
  |             log.info("A token re-route was requested by user 
'"+currentUser.getUsername()+"' for tokenId '"+tokenId+"' to node 
'"+nodeName+"'");
  |             if (tokenId == null || tokenId.equals("")) {
  |                     String errorMsg = "Cannot reroute token, as no tokenId 
was specified.";
  |                     log.error(errorMsg);
  |                     errors.add(ActionMessages.GLOBAL_MESSAGE, 
  |                                     new 
ActionMessage("errors.customMessage",errorMsg));
  |                     return;
  |             }
  |             if (nodeName == null || nodeName.equals("")) {
  |                     String errorMsg = "Cannot reroute token, as no node 
name was specified.";
  |                     log.error(errorMsg);
  |                     errors.add(ActionMessages.GLOBAL_MESSAGE, 
  |                                     new 
ActionMessage("errors.customMessage",errorMsg));
  |                     return;
  |             }
  |             Token token = jbpmContext.getToken(Long.parseLong(tokenId));
  |             if (token == null) {
  |                     String errorMsg = "Cannot reroute token, as token with 
ID '"+tokenId+"' was not found.";
  |                     log.error(errorMsg);
  |                     errors.add(ActionMessages.GLOBAL_MESSAGE, 
  |                                     new 
ActionMessage("errors.customMessage",errorMsg));
  |                     return;
  |             }
  |             
  |             Node newNode = 
processInstance.getProcessDefinition().getNode(nodeName);
  |             //don't allow re-route if Start Node was selected.
  |             if (newNode.getId() == 
newNode.getProcessDefinition().getStartState().getId()) { //if new node is 
start node
  |                     String errorMsg = "Cannot re-route token to a Start 
State. You should rather start a new process.";
  |                     log.error(errorMsg);
  |                     errors.add(ActionMessages.GLOBAL_MESSAGE, 
  |                                     new 
ActionMessage("errors.customMessage",errorMsg));
  |             }
  |             else {
  |                     //---process reroute---
  |                     //Cancel current incomplete task 
instances-------------------------------------------------------------
  |                     log.info("Removing task instances found at token with 
ID '"+tokenId+"'");
  |                     List deleteTaskInstanceList = new ArrayList();
  |                     //create a seperate list of those task instances to 
delete. If deleting them
  |                     //straight from iterator, a 
ConcurrentModificationException will occur when calling it.next(); 
  |                     for(Iterator it = 
processInstance.getTaskMgmtInstance().getTaskInstances().iterator();it.hasNext();)
 {
  |                             TaskInstance taskInstance = 
(TaskInstance)it.next();
  |                             
if(tokenId.equals(taskInstance.getToken().getId()+"")) {
  |                                     if (taskInstance.getEnd() == null) { 
//not complete task
  |                                             
deleteTaskInstanceList.add(taskInstance);
  |                                     }
  |                             }
  |                     }
  |                     for (int x = 0; x < deleteTaskInstanceList.size(); x++) 
{
  |                             TaskInstance taskInstance = 
(TaskInstance)deleteTaskInstanceList.get(x);
  |                             //Clear local 
variables-----------------------------------------------
  |                             //We must clear local variables, or task 
instance will save variables
  |                             //from previous instance. We must not delete 
them, but rather set them
  |                             //to blank, or an exception will be thrown if 
the variables were 
  |                             //set to 'required' in the process definition.
  |                             Map localVariables = 
taskInstance.getVariablesLocally();
  |                             for (Iterator it = 
localVariables.keySet().iterator();it.hasNext();) {
  |                                     String variableName = (String)it.next();
  |                                     
taskInstance.setVariableLocally(variableName, "");
  |                             }
  |                             
//--------------------------------------------------------------------
  |                             log.info("Cancelling task instance with ID 
'"+taskInstance.getId()+"'");
  |                             boolean overwriteSwimlane = false; //don't 
overwrite swimlane actor
  |                             taskInstance.setActorId("[rerouted by 
"+currentUser.getUsername()+"]", overwriteSwimlane);
  |                             taskInstance.setSignalling(false);
  |                             //Workaround to stop task-end event from firing 
when we cancel a task instance. 
  |                             //Remove event, cancel task instance, add event 
again-----------------
  |                             Task task = taskInstance.getTask();
  |                             Event endTaskEvent = 
task.getEvent(Event.EVENTTYPE_TASK_END);
  |                             if (endTaskEvent != null) {
  |                                     task.removeEvent(endTaskEvent);         
                        
  |                             }
  |                             taskInstance.cancel();
  |                             if (endTaskEvent != null) {
  |                                     task.addEvent(endTaskEvent);            
                        
  |                             }
  |                             
//-------------------------------------------------------------------
  |                     }
  |                     
//-----------------------------------------------------------------------------------------------------
  |                     newNode.enter(new ExecutionContext(token));
  |                     //log info
  |                     String msg = "Token with ID '"+token.getId()+"' was 
rerouted to node '"+nodeName+"'";
  |                     log.info(msg);
  |                     messages.add(ActionMessages.GLOBAL_MESSAGE,
  |                                     new 
ActionMessage("messages.customMessage", msg));
  |             }
  |             
  |     }

View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4145737#4145737

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4145737
_______________________________________________
jboss-user mailing list
jboss-user@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/jboss-user

Reply via email to