最近再搞流程,最后有个确认环节,如果认为没有处理好就驳回到上一节点,使用complete方法传入sequence flow的condition,但是流程会直接结束,最后找了好多帖子,才发现这个处理方式,这个是我的代码逻辑,
以下的注入根据各自的情况处理
@Autowired
RepositoryService repositoryService;
@Autowired
RuntimeService runtimeService;
@Autowired
TaskService taskService;
@Autowired
HistoryService historyService;
回退方法如下:
/**
* 退回到上一节点
*
* @param task 当前任务
*/
@Override
public void backProcess(String processInstanceId, String taskId) throws Exception {
// 取得所有历史任务按时间降序排序
List
Integer size = 2;
if (ObjectUtils.isEmpty(htiList) || htiList.size() < size) {
return;
}
// list里的第二条代表上一个任务
HistoricTaskInstance lastTask = htiList.get(1);
// list里第一条代表当前任务
HistoricTaskInstance curTask = htiList.get(0);
// 当前节点的executionId
String curExecutionId = curTask.getExecutionId();
// 上个节点的taskId
String lastTaskId = lastTask.getId();
// 上个节点的executionId
String lastExecutionId = lastTask.getExecutionId();
if (null == lastTaskId) {
throw new Exception("LAST TASK IS NULL");
}
String processDefinitionId = lastTask.getProcessDefinitionId();
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
String lastActivityId = null;
List
.executionId(lastExecutionId).finished().list();
for (HistoricActivityInstance hai : haiFinishedList) {
if (lastTaskId.equals(hai.getTaskId())) {
// 得到ActivityId,只有HistoricActivityInstance对象里才有此方法
lastActivityId = hai.getActivityId();
break;
}
}
// 得到上个节点的信息
FlowNode lastFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(lastActivityId);
// 取得当前节点的信息
Execution execution = runtimeService.createExecutionQuery().executionId(curExecutionId).singleResult();
String curActivityId = execution.getActivityId();
FlowNode curFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(curActivityId);
// 记录当前节点的原活动方向
List
oriSequenceFlows.addAll(curFlowNode.getOutgoingFlows());
// 清理活动方向
curFlowNode.getOutgoingFlows().clear();
// 建立新方向
List
SequenceFlow newSequenceFlow = new SequenceFlow();
newSequenceFlow.setId("flow4");
newSequenceFlow.setSourceFlowElement(curFlowNode);
newSequenceFlow.setTargetFlowElement(lastFlowNode);
newSequenceFlowList.add(newSequenceFlow);
curFlowNode.setOutgoingFlows(newSequenceFlowList);
// 完成任务
taskService.complete(taskId);
// 恢复原方向
curFlowNode.setOutgoingFlows(oriSequenceFlows);
Task nextTask = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
// 设置执行人
if (nextTask != null) {
taskService.setAssignee(nextTask.getId(), lastTask.getAssignee());
}
}
public List
return historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId)
.orderByTaskCreateTime().desc().list();
}