activiti study 6: After starting the process to obtain the dynamic flow chart

activiti study 6: After starting the process to obtain the dynamic flow chart

On a blog post describes how activiti open process and let the process go forward, the flowchart activiti at this time to learn how to get a dynamic

activiti version used in this article is 5.22.0

First, the principle of drawing

activiti provides a class can be used to draw the flowchart DefaultProcessDiagramGenerator, in this class and the version 5.22.0 above

By way of a separate jar package is provided, it is necessary to introduce the appropriate dependencies.

<dependency>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-image-generator</artifactId>
    <version>5.22.0</version>
</dependency>

Create objects of this class use,

//这个类在5.22.0往上的版本中才有
DefaultProcessDiagramGenerator diagramGenerator=new DefaultProcessDiagramGenerator();

Then call the drawing methods

diagramGenerator.generateDiagram(bpmnModel, imageType, highLightedActivities);

One of several parameters:

bpmnModel: The current process model corresponding to the process, can be obtained by repositoryService.getBpmnModel (processDefinitionId)

imageType: image type, jpg, png, etc.

id needs highlighted node: highLightedActivities

Second, according to the drawing process definition id

The primary key definition table id act_re_procdef drawing process, then the process can not, as long as it will have successfully deployed flowchart recording act_re_procdef table. Therefore, the problem does not exist node highlighted, and because the process instance independent FIGS.

    @Test
    public void test1() throws Exception {
        
        //根据流程定义id来获取BpmnModel对象
        String processDefinitionId="process:2:4304";
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
        //这个类在5.22.0往上的版本中才有
        DefaultProcessDiagramGenerator diagramGenerator=new DefaultProcessDiagramGenerator();
        //绘制bpmnModel代表的流程的流程图
        InputStream inputStream = diagramGenerator.generateDiagram(bpmnModel, "png", new ArrayList<String>());
        FileOutputStream output=new FileOutputStream(new File("d:/test.png"));
        IOUtils.copy(inputStream, output);
        System.out.println("输出完成");
    }

Note that this third parameter generateDiagram () method is passed an empty list, this list represents the set of nodes to highlight id display. That is, the flow chart of each task id

Third, according to the drawing process instance id

3.1 The basic principle

The above has been achieved according to processDefinitionId drawing. According to the time processInstanceId drawing, in fact, it is based on processInstanceId first querying the corresponding process instance of an object ProcessInstance,

Then the object obtained by the process instance to processDefinitionId, wherein the method of using the getprocessInstance.getProcessDefinitionId()

Think about how to get to the node id needs highlighted: the current node + walked node.

3.2 of the current node acquisition

Use runtimeService way to get the service component

String executionId=processInstance.getId();
List<String> activeActivityIds = runtimeService.getActiveActivityIds(executionId);

activeActivityIds this list is a collection of the current node id, if there are multiple parallel processes, otherwise only one.

3.3 traversed nodes to obtain

Traveled node needs to obtain from the historical behavior activiti table act_hi_actinst in. Flow behavior data is recorded in the table, when the flow proceeds to a node, the table may record information flow nodes, including the node id, name and the like. Wherein a ACT_ID_represents the flowchart of the corresponding node id

That is, the node id needs highlighted

So we have to do is to query from this table to the data. activiti has provided query this table api, may be obtained by historyService this service component

//得到已经走过的节点的id集合
        HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
        List<HistoricActivityInstance> historicActivityInstanceList = historicActivityInstanceQuery.processInstanceId(processInstanceId).list();
        for(HistoricActivityInstance hi:historicActivityInstanceList) {
            String taskKey=hi.getActivityId();
            activeActivityIds.add(taskKey);
        }

A first check code above ACT_ID_, and then add them to the needs highlighted in the set activeActivityIds

3.4 Drawing

DefaultProcessDiagramGenerator defaultProcessDiagramGenerator=new DefaultProcessDiagramGenerator();
//获取流程图的输入流
InputStream inputStream = defaultProcessDiagramGenerator.generateDiagram(bpmnModel, "png", activeActivityIds);
//输出图片到指定路径
IOUtils.copy(inputStream, new FileOutputStream(new File("d:/test2.png")));
System.out.println("输出成功");

The node has gone through this flowchart plotted will be highlighted.

IV Summary

Principle flow diagrams using the method DefaultProcessDiagramGenerator class

This class provides a separate jar, it is necessary to add a new dependency

To highlight some of the nodes, give drawing method of node id of the collection.

Guess you like

Origin www.cnblogs.com/chengxuxiaoyuan/p/12026834.html