public void getPreOneIncomeNode(String taskId) {
List<Map<String, String>> incomeNodes = new ArrayList<>();
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
String currActivityId = task.getTaskDefinitionKey();
// 获取当前用户任务节点
BpmnModel bpmnModel = repositoryService.getBpmnModel(task.getProcessDefinitionId());
Process process = bpmnModel.getProcesses().get(0);
getIncomeNodesRecur(currActivityId, incomeNodes, process, false);
FlowNode currFlow = (FlowNode)bpmnModel.getMainProcess().getFlowElement(currActivityId);
if (currFlow == null) {
List<SubProcess> subProcessList = bpmnModel.getMainProcess().findFlowElementsOfType(SubProcess.class, true);
for (SubProcess subProcess : subProcessList) {
FlowElement flowElement = subProcess.getFlowElement(currActivityId);
if (flowElement != null) {
currFlow = (FlowNode) flowElement;
break;
}
}
}
// 记录原活动方向
List<SequenceFlow> oriSequenceFlows = Lists.newArrayList();
oriSequenceFlows.addAll(currFlow.getOutgoingFlows());
// 清理活动方向
currFlow.getOutgoingFlows().clear();
List<SequenceFlow> newSequenceFlows = Lists.newArrayList();
for (int i = 0; i< incomeNodes.size(); i++) {
Map<String, String> item = incomeNodes.get(i);
String nodeId = item.get("id");
// 获取目标节点
FlowNode target = (FlowNode)bpmnModel.getFlowElement(nodeId);
//如果不是同一个流程(子流程)不能驳回
if (!(currFlow.getParentContainer().equals(target.getParentContainer()))) {
continue;
}
// 建立新方向
SequenceFlow newSequenceFlow = new SequenceFlow();
String uuid = UUID.randomUUID().toString().replace("-","");
newSequenceFlow.setId(uuid);
newSequenceFlow.setSourceFlowElement(currFlow);// 原节点
newSequenceFlow.setTargetFlowElement(target);// 目标节点
newSequenceFlows.add(newSequenceFlow);
};
currFlow.setOutgoingFlows(newSequenceFlows);
// 拒接、通过、驳回指定节点
taskService.complete(taskId);
//恢复原方向
currFlow.setOutgoingFlows(oriSequenceFlows);
}
/**
* 递归遍历获取上个任务节点
**/
public void getIncomeNodesRecur(String currentNodeId, List<Map<String, String>> incomeNodes, Process process, boolean isAll) {
FlowElement currentFlowElement = process.getFlowElement(currentNodeId);
List<SequenceFlow> incomingFlows = null;
if (currentFlowElement instanceof UserTask) {
incomingFlows = ((UserTask) currentFlowElement).getIncomingFlows();
} else if (currentFlowElement instanceof Gateway) {
incomingFlows = ((Gateway) currentFlowElement).getIncomingFlows();
} else if (currentFlowElement instanceof StartEvent) {
incomingFlows = ((StartEvent) currentFlowElement).getIncomingFlows();
}
if (incomingFlows != null && incomingFlows.size() > 0) {
incomingFlows.forEach(incomingFlow -> {
String expression = incomingFlow.getConditionExpression();
// 出线的上一节点
String sourceFlowElementID = incomingFlow.getSourceRef();
// 查询上一节点的信息
FlowElement preFlowElement = process.getFlowElement(sourceFlowElementID);
//用户任务
if (preFlowElement instanceof UserTask) {
Map<String,String> tempMap = new HashMap<>();
tempMap.put("id", preFlowElement.getId());
tempMap.put("name", preFlowElement.getName());
incomeNodes.add(tempMap);
if (isAll) {
getIncomeNodesRecur(preFlowElement.getId(), incomeNodes, process, true);
}
}
//排他网关
else if (preFlowElement instanceof ExclusiveGateway) {
getIncomeNodesRecur(preFlowElement.getId(), incomeNodes, process, isAll);
}
//并行网关
else if (preFlowElement instanceof ParallelGateway) {
getIncomeNodesRecur(preFlowElement.getId(), incomeNodes, process, isAll);
}
});
}
}
springboot+activiti7驳回至上一步实现代码
最新推荐文章于 2023-08-04 16:29:14 发布