作者:手机用户2602914627 | 来源:互联网 | 2024-11-11 10:46
在Flowable流程图中,通常仅显示当前节点,而路径则需自行获取。特别是在多次驳回的情况下,节点可能会出现混乱。本文重点探讨了如何准确地展示流程图效果,包括已结束的流程和正在执行的流程。具体实现方法包括生成带有高亮红色标记的图片,以增强可视化效果,确保用户能够清晰地了解每个节点的状态。
效果:
已经结束的流程
正在执行的流程
代码:1、生成图片
// 源码 www.1b23.com
public byte[] createImage2(String processInstanceId) {
//1.获取当前的流程实例
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
String processDefinitiOnId= null;
List activeActivityIds = null;
//2.获取所有的历史轨迹对象
List list = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId).list();
Map hisActivityMap = new HashMap<>();
list.forEach(historicActivityInstance -> {
if (!hisActivityMap.containsKey(historicActivityInstance.getActivityId())) {
hisActivityMap.put(historicActivityInstance.getActivityId(), historicActivityInstance);
}
});
//3. 获取流程定义id和高亮的节点id
if (processInstance != null) {
//3.1. 正在运行的流程实例
processDefinitiOnId= processInstance.getProcessDefinitionId();
activeActivityIds = runtimeService.getActiveActivityIds(processInstanceId);
} else {
//3.2. 已经结束的流程实例
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
processDefinitiOnId= historicProcessInstance.getProcessDefinitionId();
activeActivityIds = new ArrayList<>();
List endEvents = bpmnModelService.findEndFlowElement(processDefinitionId);
List finalActiveActivityIds = activeActivityIds;
endEvents.forEach(endEvent -> {
if (hisActivityMap.containsKey(endEvent.getId())) {
finalActiveActivityIds.add(endEvent.getId());
}
});
}
//4. 获取流程定义的所有节点信息
List flowNodes = bpmnModelService.findFlowNodes(processDefinitionId);
Map activityMap = flowNodes.stream().collect(Collectors.toMap(FlowNode::getId, flowNode -> flowNode));
List highLightedFlows = new ArrayList<>();
//5. 递归得到高亮线
activeActivityIds.forEach(activeActivityId -> this.getHighLightedFlows(activityMap, hisActivityMap, activeActivityId, highLightedFlows, activeActivityId));
//6. 获取bpmnModel对象
BpmnModel bpmnModel = bpmnModelService.getBpmnModelByProcessDefId(processDefinitionId);
//7. 生成图片流
InputStream inputStream = flowProcessDiagramGenerator.generateDiagram(bpmnModel, activeActivityIds, highLightedFlows);
//8. 转化成byte便于网络传输
byte[] datas = IoUtil.readInputStream(inputStream, "image inputStream name");
return datas;
}
递归获取高
//www.1b23.com
private void getHighLightedFlows(Map flowNodeMap,
Map hisActivityMap,
String activeActivityId,
List highLightedFlows,
String oldActivityId) {
FlowNode flowNode = flowNodeMap.get(activeActivityId);
List incomingFlows = flowNode.getIncomingFlows();
for (SequenceFlow sequenceFlow : incomingFlows) {
String sourceRefId = sequenceFlow.getSourceRef();
if (hisActivityMap.containsKey(sourceRefId) && !oldActivityId.equals(sourceRefId)) {
highLightedFlows.add(sequenceFlow.getId());
this.getHighLightedFlows(flowNodeMap, hisActivityMap, sourceRefId, highLightedFlows, oldActivityId);
} else {
if (hisActivityMap.containsKey(sourceRefId)){
highLightedFlows.add(sequenceFlow.getId());
}
break;
} } }