注意: flowable 6.6.0 之后的版本
流程流转过程中 可能会存在 驳回的情况, 所以 做了筛选, 只保留最新的线路图
/**
* 流程记录
*/
private InputStream processRecordImage(String processInstanceId) throws IOException {
HistoricProcessInstance hiProcInst =
Optional.ofNullable(historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult())
.orElseThrow(()->new IOException("流程实例没有找到"));
List<String> highLightedActivities = taskService.createTaskQuery().processInstanceId(processInstanceId).list().stream()
.map(task -> runtimeService.createExecutionQuery().executionId(task.getExecutionId()).singleResult().getActivityId()).collect(Collectors.toList());
List<String> flowIds;
if (highLightedActivities.size() > 0) {
flowIds = runtimeService.createActivityInstanceQuery()
.orderByActivityInstanceStartTime().asc()
.orderByActivityInstanceEndTime().asc()
.processInstanceId(processInstanceId).list().stream()
.filter(hiActInst -> "sequenceFlow".equals(hiActInst.getActivityType()))
.map(ActivityInstance::getActivityId)
.collect(Collectors.toCollection(CopyOnWriteArrayList::new));
} else {
flowIds = historyService.createHistoricActivityInstanceQuery().orderByHistoricActivityInstanceStartTime().asc()
.orderByHistoricActivityInstanceEndTime().asc()
.processInstanceId(processInstanceId).list().stream()
.filter(hiActInst -> "sequenceFlow".equals(hiActInst.getActivityType()))
.map(HistoricActivityInstance::getActivityId)
.collect(Collectors.toCollection(CopyOnWriteArrayList::new));
}
List<String> highLightedFlows = new CopyOnWriteArrayList<>();
flowIds.forEach(id -> {
if (highLightedFlows.contains(id)) {
int index = highLightedFlows.indexOf(id);
highLightedFlows.removeIf(id1 -> highLightedFlows.indexOf(id1) > index);
} else {
highLightedFlows.add(id);
}
});
return new DefaultProcessDiagramGenerator().generateDiagram(
repositoryService.getBpmnModel(hiProcInst.getProcessDefinitionId()),
"PNG",
highLightedActivities,
highLightedFlows,
"宋体",
"宋体",
"宋体",
null,
1.0,
true);
}
flowable 6.6.0 之前的版本
/**
* 流程记录
*/
private InputStream processRecordImage(String processInstanceId) {
// 获取流程走向信息
List<Map<String, String>> histActInsts = new ArrayList<>();
// 连线信息
List<Map<String, String>> flows = new ArrayList<>();
// 高亮 流程节点
List<String> activeActivityIds = new ArrayList<>();
// 高亮连线ID
List<String> highLightedFlows = new ArrayList<>();
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery()
.orderByHistoricActivityInstanceStartTime().asc()
.orderByHistoricActivityInstanceEndTime().asc()
.processInstanceId(processInstanceId).list();
//获取流程走向信息
historicActivityInstances.forEach(hai -> {
//如果存在 返回当前值 如果不存在 则返回Null
Map<String, String> hihai = histActInsts.stream()
.filter(map -> map.get("id").equals(hai.getActivityId()))
.findFirst().orElse(null);
if (hihai == null) {
Map<String, String> hti = new HashMap<>();
hti.put("id", hai.getActivityId());
hti.put("name", hai.getActivityName());
hti.put("type", hai.getActivityType());
histActInsts.add(hti);
} else {
int index = histActInsts.indexOf(hihai);
histActInsts.removeIf(map -> histActInsts.indexOf(map) > index);
}
});
// 所有节点都高亮
// activeActivityIds = histActInsts.stream().map(map -> map.get("id")).collect(Collectors.toList());
//当前节点高亮
List<Task> taskList = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
for (Task obj : taskList) {
ExecutionEntity executionEntity = (ExecutionEntity) runtimeService.createExecutionQuery()
.executionId(obj.getExecutionId()).singleResult();
activeActivityIds.add(executionEntity.getActivityId());
}
for (int i = 0; i < histActInsts.size(); i++) {
if (i == histActInsts.size()-1){
break;
}
Map<String, String> flow = new HashMap<>();
flow.put("source", histActInsts.get(i).get("id"));
flow.put("target", histActInsts.get(i + 1).get("id"));
flows.add(flow);
}
/* findFlowElementsOfTypeByProcInstId(processInstanceId, SequenceFlow.class).forEach(sequenceFlow -> {
flows.forEach(flow -> {
if (flow.get("source").equals(sequenceFlow.getSourceRef()) && flow.get("target").equals(sequenceFlow.getTargetRef())
|| flow.get("target").equals(sequenceFlow.getSourceRef()) && flow.get("source").equals(sequenceFlow.getTargetRef())){
highLightedFlows.add(sequenceFlow.getId());
}
});
}); */
BpmnModel bpmnModel = repositoryService.getBpmnModel(historicProcessInstance.getProcessDefinitionId());
// 查询连接线信息-> update 2023-05-10
Collection<FlowElement> flowElements = bpmnModel.getMainProcess().getFlowElements();
List<SequenceFlow> elements = new ArrayList<>();
for (FlowElement flowElement : flowElements) {
if (flowElement instanceof SequenceFlow) {
elements.add((SequenceFlow) flowElement);
}
}
// 查询连接线信息结束 -> update 2023-05-10
elements.forEach(sequenceFlow -> {
flows.forEach(flow -> {
if (flow.get("source").equals(sequenceFlow.getSourceRef()) && flow.get("target").equals(sequenceFlow.getTargetRef())
|| flow.get("target").equals(sequenceFlow.getSourceRef()) && flow.get("source").equals(sequenceFlow.getTargetRef())) {
highLightedFlows.add(sequenceFlow.getId());
}
});
});
DefaultProcessDiagramGenerator defaultProcessDiagramGenerator = new DefaultProcessDiagramGenerator();
//8. 转化成byte便于网络传输
return defaultProcessDiagramGenerator.generateDiagram(bpmnModel, "PNG", activeActivityIds, highLightedFlows, "宋体", "宋体", "宋体", null, 1.0, true);
}