xiangpei
2024-11-22 1ce2bb9038b16d0e59e7eb50f8aefcea2619b226
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.ycl.web.util;
 
import com.ycl.framework.web.domain.server.Sys;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowNode;
import org.flowable.bpmn.model.SequenceFlow;
import org.flowable.common.engine.impl.util.IoUtil;
import org.flowable.engine.HistoryService;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.image.ProcessDiagramGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 流程追踪图
 */
@Component
public class ActivitiTracingChart {
 
    private static final Logger logger = LoggerFactory.getLogger(ActivitiTracingChart.class);
 
 
    @Resource
    private HistoryService historyService;
 
    @Resource
    private RepositoryService repositoryService;
 
    @Resource
    private ProcessEngineConfiguration processEngineConfiguration;
 
    /**
     * 生成流程追踪图
     *
     * @param processInstanceId 流程实例id
     * @param outputStream      输出流
     */
    public void generateFlowChart(String processInstanceId, OutputStream outputStream) {
            if (StringUtils.isEmpty(processInstanceId)) {
                logger.error("processInstanceId is null");
                return;
            }
            // 获取历史流程实例
            HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId)
                .singleResult();
            // 获取流程中已经执行的节点
            String processDefinitionId = historicProcessInstance.getProcessDefinitionId();
            List<String> highLightedActivities = new ArrayList<>();
            // 获得活动的节点
            List<HistoricActivityInstance> highLightedActivityList = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).orderByHistoricActivityInstanceStartTime().asc().list();
            List<String> highLightedFlows = new ArrayList<>();
            for (HistoricActivityInstance tempActivity : highLightedActivityList) {
                String activityId = tempActivity.getActivityId();
                highLightedActivities.add(activityId);
                if ("sequenceFlow".equals(tempActivity.getActivityType())) {
                    highLightedFlows.add(tempActivity.getActivityId());
                }
            }
            // 获取流程图
            BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
            ProcessEngineConfiguration engConf = processEngineConfiguration.getProcessEngineConfiguration();
 
            ProcessDiagramGenerator diagramGenerator = engConf.getProcessDiagramGenerator();
            InputStream in = diagramGenerator.generateDiagram(bpmnModel, "bmp", highLightedActivities, highLightedFlows, "宋体",
                    "宋体", "宋体", engConf.getClassLoader(), 1.0, true);
            byte[] buf = new byte[1024];
            int length;
            try {
                while ((length = in.read(buf)) != -1) {
                    outputStream.write(buf, 0, length);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IoUtil.closeSilently(outputStream);
                IoUtil.closeSilently(in);
            }
    }
}