JBPM3动态分支的实现

有朋友也遇到这方面的问题,我就贴个贴子,不再一一回复了,参考了论坛上的一些资料,代码如下:

 

public class ForkManageActionHandler implements ActionHandler {

 public void execute(ExecutionContext executionContext) throws Exception {
  
  ProcessInstance pi = executionContext.getProcessInstance();
  ContextInstance ci = executionContext.getContextInstance();
  Token rootToken = pi.getRootToken();
  ForkVariableVo  forkVariableVo = (ForkVariableVo)ci.getVariable(Constants.FORK_VARIABLE_VO);
    
  //角色发送和多人签收分支
  if(forkVariableVo.isRoleSend() && forkVariableVo.isMorePeopleSign()){
   
   Node node = rootToken.getNode();
   List<String> sendUserList = new ArrayList<String>() ;
   List<ForkedTransition> forkTransitions = new ArrayList<ForkedTransition>();
   Map<String,String> roleSendAndMorePeopleMap = forkVariableVo.getRoleSendAndMorePeople();
   
   for(int j=0;j<node.getLeavingTransitions().size();j++){
    Transition trans = (Transition)node.getLeavingTransitions().get(j);
    
    //考虑到有多个角色发送和多人签收的分支情况,MAP过滤,获取动态分支用户
    String activityId = trans.getName() ;
    if(roleSendAndMorePeopleMap.get(activityId)!=null){
     if(forkVariableVo.mOfficeTargetUser.get(activityId)!=null){
      sendUserList = (List<String>)forkVariableVo.mOfficeTargetUser.get(activityId);
     }else{
      sendUserList = (List<String>)forkVariableVo.noMOfficeTargetUser.get(activityId);
     }
    }
    if(sendUserList ==null || sendUserList.size() ==0){
     continue;
    }
    //创建分支Token及Transition
    for(int i=1; i<sendUserList.size(); i++){
     String tokenName = this.getTokenName(rootToken,  activityId, i);
     Token loopToken = new Token(rootToken,tokenName);
     loopToken.setTerminationImplicit(true);
     executionContext.getJbpmContext().getSession().save(loopToken);
     
     ExecutionContext newExecutionContext = new ExecutionContext(
       loopToken);
     //newExecutionContext.getContextInstance().createVariable(String.valueOf(i),
     //(String)sendUserList.get(i), loopToken);
 
     // 记录下每一transition
     ForkedTransition forkTransition = new ForkedTransition();
     forkTransition.executionContext = newExecutionContext;
     forkTransition.transition = trans;
     forkTransitions.add(forkTransition);
    } 
    sendUserList = null ;
   }
   for (ForkedTransition forkTransition : forkTransitions) {
    node.leave(forkTransition.executionContext, forkTransition.transition);
   }
  } 
 }

 /**
  * 获取Token名称
  * @param parent
  * @param transitionName
  * @param loopIndex
  * @return
  */
 protected String getTokenName(Token parent, String transitionName,int loopIndex) {
  String tokenName = null;
  if (transitionName != null) {
   if (!parent.hasChild(transitionName)) {
    tokenName = transitionName;
   } else {
    int i = 2;
    tokenName = transitionName + Integer.toString(i);
    while (parent.hasChild(tokenName)) {
     i++;
     tokenName = transitionName + Integer.toString(i);
    }
   }
  } else {
   // 没有转向
   int size = (parent.getChildren() != null ? parent.getChildren()
     .size() + 1 : 1);
   tokenName = Integer.toString(size);
  }
  return tokenName + "." + loopIndex;
 } 
 
 private class ForkedTransition {  
  private ExecutionContext executionContext;
  private Transition transition;
 }
}

在FORK节点的node-enter中配置该ACTION类,凭借原来那条分支,根据发送的用户的LIST,动态去构建TOKEN,每个TOKEN指向其原分支上的transition,中间那部分角色发送是我自己的需求而写的,重要的是创建分支这一部分。

你可能感兴趣的:(J#)