flowable实现多实例节点的自由跳转

最近弃用activiti,改用flowable,发现在实现多节点实例自由跳转时,有很大区别。
自由跳转整理如下:


import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.Process;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.FlowableEngineAgenda;
import org.flowable.engine.impl.cmd.NeedsActiveTaskCmd;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.impl.util.ProcessDefinitionUtil;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;

import java.util.List;
import java.util.Map;


/**
 * @description: 自由跳转流程
 * @author: starmark
 * @create: 2018-10-13 09:22
 **/
public class ActJumpTaskCmd extends NeedsActiveTaskCmd {

    protected String processId;//执行实例id
    protected String targetNodeId;//目标节点
    protected Map formData;//变量
    protected String operationCode;
    public ActJumpTaskCmd(String taskId,   String processId, String targetNodeId, Map formData,String operationCode) {
        super(taskId);
        this.processId = processId;
        this.targetNodeId = targetNodeId;
        this.formData = formData;
        this.operationCode=operationCode;
    }


    @Override
    protected Boolean execute(CommandContext commandContext, TaskEntity task) {
        ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();
        ExecutionEntity rootExecution=  executionEntityManager.findChildExecutionsByParentExecutionId(processId).get(0);


        CommandContextUtil.getTaskService().deleteTask(task, true);

        List executionEntityList= executionEntityManager.findChildExecutionsByParentExecutionId(rootExecution.getId());
        for(ExecutionEntity executionEntity:executionEntityList){
            List executionEntityList2= executionEntityManager.findChildExecutionsByParentExecutionId(executionEntity.getId());

            for(ExecutionEntity executionEntity2:executionEntityList2){
                CommandContextUtil.getTaskService().deleteTasksByExecutionId(executionEntity2.getId());
                executionEntityManager.deleteChildExecutions(executionEntity2,"delete",true);
                executionEntityManager.delete(executionEntity2);
                CommandContextUtil.getVariableService().deleteVariablesByExecutionId(executionEntity2.getId());
            }
            CommandContextUtil.getTaskService().deleteTasksByExecutionId(executionEntity.getId());
            executionEntityManager.deleteChildExecutions(executionEntity,"delete",true);
            executionEntityManager.delete(executionEntity);
            CommandContextUtil.getVariableService().deleteVariablesByExecutionId(executionEntity.getId());
        }




        Process process = ProcessDefinitionUtil.getProcess(rootExecution.getProcessDefinitionId());
        FlowElement targetFlowElement = process.getFlowElement(targetNodeId);
        rootExecution.setCurrentFlowElement(targetFlowElement);
        FlowableEngineAgenda agenda = CommandContextUtil.getAgenda();
        agenda.planContinueProcessInCompensation(rootExecution);

        return true;
    }


}

看完代码,我们再说说过程,流程实例的驱动主要是靠表act_ru_execution来驱动的。前后跳转主要操作以下步骤:

  • 清除相关任务(act_ru_task)
  • 清除局部变量(act_ru_variable),注意是局部变量
  • 清除轨迹(act_ru_execution)
  • 保留act_ru_execution到只剩下两条记录再往下驱动流程.
    注意:activti自由跳转也是同样的道理.
    有朋友私信我,说我的代码不支持并行分支的驳回,这个确实是这样。
    但并行分支的驳回有两种。
  1. 分支内的驳回,即驳回前有多少条分支,驳回后还是有多少条分支,研究一下act_ru_execution,看清哪些数据再处理
  2. 分支外的驳回,即原来有5条分支,可能驳回后只有一条。这样的话,还是删除掉act_ru_execution到只剩下两条记录即可.
    后续有空,我再考虑这两种驳回吧!

你可能感兴趣的:(flowable实现多实例节点的自由跳转)