ACTIVITI6实现节点的自由跳转

1271

1.新增删除节点操作类

import org.activiti.engine.impl.cmd.NeedsActiveTaskCmd;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntityManagerImpl;

/**
 * @description: 删除节点操作
 * @author: Neal zhi
 * @create: 2020-05-24 13:00
 **/

public class DeleteTaskCmd extends NeedsActiveTaskCmd<String> {
    public DeleteTaskCmd(String taskId) {
        super(taskId);
    }

    @Override
    protected String getSuspendedTaskException() {
        return "挂起的任务不能跳转";
    }

    @Override
    protected String execute(CommandContext commandContext, TaskEntity taskEntity) {
        //获取所需服务
        TaskEntityManagerImpl taskEntityManager = (TaskEntityManagerImpl) commandContext.getTaskEntityManager();
        //获取当前任务的来源任务及来源节点信息
        ExecutionEntity executionEntity = taskEntity.getExecution();
        //删除当前任务,来源任务
        taskEntityManager.deleteTask(taskEntity, "jumpFlowNode", false, false);
        return executionEntity.getId();
    }
}

2.新增跳转节点操作类

import org.activiti.bpmn.model.FlowNode;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;

import java.util.List;

/**
 * @description: 流程执行到目标节点
 * @author: Neal zhi
 * @create: 2020-05-24 13:03
 **/

public class SetFLowNodeAndGoCmd implements Command<Void> {

    private FlowNode flowElement;
    private String executionId;

    public SetFLowNodeAndGoCmd() {
    }

    public SetFLowNodeAndGoCmd(FlowNode flowElement, String executionId) {
        this.flowElement = flowElement;
        this.executionId = executionId;
    }

    @Override
    public Void execute(CommandContext commandContext) {
        //获取目标节点的来源连线
        List<SequenceFlow> flows = flowElement.getIncomingFlows();
        if (flows == null || flows.size() < 1) {
            throw new ActivitiException("跳转错误,目标节点没有来源连线");
        }
        //随便选一条连线来执行,当前执行计划为,从连线流转到目标节点,实现跳转
        ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findById(executionId);
        executionEntity.setCurrentFlowElement(flows.get(0));
        commandContext.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionEntity, true);
        return null;
    }
}

3.编写service方法

public void jumpNode(String taskId, String flowElementId) {
        //获取当前任务对象
        Task currentTask = taskService.createTaskQuery().taskId(taskId).singleResult();
        //获取流程定义id
        String processDefinitionId = currentTask.getProcessDefinitionId();
        //获取bpmn模板
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
        //获取目标节点定义
        FlowNode targetNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(flowElementId);
        //删除当前运行任务
        String executionEntityId = managementService.executeCommand(new DeleteTaskCmd(currentTask.getId()));
        //流程执行到目标节点
        managementService.executeCommand(new SetFLowNodeAndGoCmd(targetNode, executionEntityId));
    }