1. 回退(驳回)
回退的思路就是动态更改节点的流向。先遇水搭桥,最后再过河拆桥。
具体操作如下:
取得当前节点的信息
取得当前节点的上一个节点的信息
保存当前节点的流向
新建流向,由当前节点指向上一个节点
将当前节点的流向设置为上面新建的流向
当前节点完成任务
将当前节点的流向还原
取得之前上个节点的执行人
设置上个节点的assignee为之前的执行人
代码实现起来可能是这样的:
@Test public void huitui() throws Exception { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService.createTaskQuery().processInstanceId("55001").singleResult(); backProcess(task); } /** * 驳回 / 回退 * 按照这种方法,可以回退至任意节点 * @param task * @throws Exception */ public void backProcess(Task task) throws Exception { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); HistoryService historyService = processEngine.getHistoryService(); RepositoryService repositoryService = processEngine.getRepositoryService(); TaskService taskService = processEngine.getTaskService(); String processInstanceId = task.getProcessInstanceId(); // 获取所有历史任务(按创建时间降序) List<HistoricTaskInstance> hisTaskList = historyService.createHistoricTaskInstanceQuery() .processInstanceId(processInstanceId) .orderByTaskCreateTime() .desc() .list(); List<HistoricActivityInstance> hisActivityList = historyService.createHistoricActivityInstanceQuery() .processInstanceId(processInstanceId).list(); if (CollectionUtils.isEmpty(hisTaskList) || hisTaskList.size() < 2) { return; } // 当前任务 HistoricTaskInstance currentTask = hisTaskList.get(0); // 前一个任务 HistoricTaskInstance lastTask = hisTaskList.get(1); // 当前活动 HistoricActivityInstance currentActivity = hisActivityList.stream().filter(e -> currentTask.getId().equals(e.getTaskId())).collect(Collectors.toList()).get(0); // 前一个活动 HistoricActivityInstance lastActivity = hisActivityList.stream().filter(e -> lastTask.getId().equals(e.getTaskId())).collect(Collectors.toList()).get(0); BpmnModel bpmnModel = repositoryService.getBpmnModel(task.getProcessDefinitionId()); // 获取前一个活动节点 FlowNode lastFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(lastActivity.getActivityId()); // 获取当前活动节点 FlowNode currentFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(currentActivity.getActivityId()); // 临时保存当前活动的原始方向 List<SequenceFlow> originalSequenceFlowList = new ArrayList<>(); originalSequenceFlowList.addAll(currentFlowNode.getOutgoingFlows()); // 清理活动方向 currentFlowNode.getOutgoingFlows().clear(); // 建立新方向 SequenceFlow newSequenceFlow = new SequenceFlow(); newSequenceFlow.setId("newSequenceFlowId"); newSequenceFlow.setSourceFlowElement(currentFlowNode); newSequenceFlow.setTargetFlowElement(lastFlowNode); List<SequenceFlow> newSequenceFlowList = new ArrayList<>(); newSequenceFlowList.add(newSequenceFlow); // 当前节点指向新的方向 currentFlowNode.setOutgoingFlows(newSequenceFlowList); // 完成当前任务 taskService.complete(task.getId()); // 重新查询当前任务 Task nextTask = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult(); if (null != nextTask) { taskService.setAssignee(nextTask.getId(), lastTask.getAssignee()); } // 恢复原始方向 currentFlowNode.setOutgoingFlows(originalSequenceFlowList); }