//获取流程定义 Process process = repositoryService.getBpmnModel(processDefinitionId).getMainProcess(); //获取目标节点定义 FlowNode targetNode = (FlowNode) process.getFlowElement(flowElementId); List<Execution> executionList = runtimeService.createExecutionQuery().parentId(taskList.get(0).getProcessInstanceId()).list(); //并行网关会有多个主execution 选取isActive =true的execution驳回,否则会导致网关判断失败 Execution remainExecution = null; boolean isChoose = false; List<Execution> deleteList = Lists.newArrayList(); for (Execution e : executionList) { ExecutionEntityImpl executionEntity = (ExecutionEntityImpl) e; if (executionEntity.isActive() && !isChoose) { remainExecution = executionEntity; isChoose = true; } else { deleteList.add(e); } deleteList.addAll(runtimeService.createExecutionQuery().parentId(e.getId()).list()); } if (remainExecution == null) { ActivitiAssert.EXECUTION_NOT_SINGLE.assertFail(); } //流程执行到来源节点 managementService.executeCommand(new SetNodeAndGoCmd(targetNode, remainExecution.getId())); if (CollectionUtil.isNotEmpty(deleteList)) { List<String> ids = deleteList.stream().map(Execution::getId).collect(Collectors.toList()); managementService.executeCommand(new DeleteExecutionCmd(ids)); } //删除当前运行任务 for (Task currentTask : taskList) { managementService.executeCommand(new DeleteTaskCmd(currentTask.getId())); }
public class SetNodeAndGoCmd implements Command<Void> { private FlowNode flowElement; private String executionId; private String executionName; private String startUserId; public SetNodeAndGoCmd(FlowNode flowElement, String executionId) { this.flowElement = flowElement; this.executionId = executionId; } public SetNodeAndGoCmd(FlowNode flowElement, String executionId, String executionName, String startUserId) { this.flowElement = flowElement; this.executionId = executionId; this.executionName = executionName; this.startUserId = startUserId; } @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)); if (StrUtil.isNotEmpty(executionName)) { executionEntity.setName(executionName); } if (StrUtil.isNotEmpty(startUserId)) { executionEntity.setStartUserId(startUserId); } log.info("SetNodeAndGoCmd执行节点跳转:processInstanceId:{},来源:{},出线:{}", executionEntity.getProcessInstanceId(), flows.get(0).getSourceRef(), flows.get(0).getTargetRef()); commandContext.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionEntity, true); return null; } }
public class DeleteExecutionCmd implements Command<Void>, Serializable { private List<String> executionIds; public DeleteExecutionCmd(List<String> executionIds) { super(); this.executionIds = executionIds; } /** * @param * @param * @return */ @Override public Void execute(CommandContext commandContext) { //删除当前任务,来源任务 ExecutionEntityManager executionEntityManager = (ExecutionEntityManagerImpl) commandContext.getExecutionEntityManager(); for (String id : executionIds) { executionEntityManager.delete(id); } return null; } }