[2023 latest Activiti7 is the most complete in the whole network] 3. Basic operation of Activiti7

Activiti basic operation explanation

1 Deployment of the process

Deploying the process defined above in the designer to the activiti database is what we call process deployment.
By calling Activiti's api, add and deploy the bpmn and png files defined by the process to activiti one by one, and you can also deploy the two files in a taxi zip package.

1.1 Single file deployment
  Deploy bpmn files and png images separately
 

    /**
     * 实现文件的单个部署
     */
    @Test
    public void test03(){
        // 1.获取ProcessEngine对象
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取RepositoryService进行部署操作
        RepositoryService service = engine.getRepositoryService();
        // 3.使用RepositoryService进行部署操作
        Deployment deploy = service.createDeployment()
                .addClasspathResource("bpmn/evection.bpmn") // 添加bpmn资源
                .addClasspathResource("bpmn/evection.png") // 添加png资源
                .name("出差申请流程")
                .deploy();// 部署流程
        // 4.输出流程部署的信息
        System.out.println("流程部署的id:" + deploy.getId());
        System.out.println("流程部署的名称:" + deploy.getName());
    }

The relevant output information can be seen in the log

1.2 Deploy the zip file

  Package the bpmn file and the png file into a zip file and upload them together

    /**
     * 通过一个zip文件来部署操作
     */
    @Test
    public void test04(){
        // 定义zip文件的输入流
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("bpmn/evection.zip");
        // 对 inputStream 做装饰
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = engine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .name("出差申请流程")
                .deploy();
        // 4.输出流程部署的信息
        System.out.println("流程部署的id:" + deploy.getId());
        System.out.println("流程部署的名称:" + deploy.getName());
    }

The data in the uploaded database is actually the same as that of a single file upload.

1.3 Operating Data Sheet

  After the process definition is deployed, operate the three tables in activiti

  act_re_deployment: process definition deployment table, add a record every time it is deployed

 act_re_procdef : Process definition table, deploying each new process definition will add a record to this table

act_ge_bytearray: process resource table, the bpmn files and png images deployed by the process will be saved in this table 

 2 Start the process instance

After the process definition is deployed in Activiti, the business process can be managed through workflow, which means that the business trip application process deployed above can be used.
  For this process, starting a process means launching a new business trip application form, which is equivalent to the relationship between a Java class and a Java object. After the class is defined, you need to create an object with new. Of course, you can create multiple objects with new. For For the business trip application process, Zhang San can initiate a business trip application form and need to start a process instance.

    /**
     * 启动一个流程实例
     */
    @Test
    public void test05(){
        // 1.创建ProcessEngine对象
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取RuntimeService对象
        RuntimeService runtimeService = engine.getRuntimeService();
        // 3.根据流程定义的id启动流程
        String id= "evection";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(id);
        // 4.输出相关的流程实例信息
        System.out.println("流程定义的ID:" + processInstance.getProcessDefinitionId());
        System.out.println("流程实例的ID:" + processInstance.getId());
        System.out.println("当前活动的ID:" + processInstance.getActivityId());
    }

output:

The table structure involved in starting the process instance

act_hi_actinst process instance execution history
act_hi_identitylink process participating user historical information
act_hi_procinst process instance historical information
act_hi_taskinst process task historical information
act_ru_execution process execution information
act_ru_identitylink process participating user information
act_ru_task task information

3 task search

  After the process is started, the person in charge of the task can query the tasks that he can currently handle. The tasks that are queried are all to-do tasks of the current user

    /**
     * 任务查询
     */
    @Test
    public void test06(){
        String assignee ="zhansan";
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 任务查询 需要获取一个 TaskService 对象
        TaskService taskService = engine.getTaskService();
        // 根据流程的key和任务负责人 查询任务
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("evection")
                .taskAssignee(assignee)
                .list();
        // 输出当前用户具有的任务
        for (Task task : list) {
            System.out.println("流程实例id:" + task.getProcessInstanceId());
            System.out.println("任务id:" + task.getId());
            System.out.println("任务负责人:" + task.getAssignee());
            System.out.println("任务名称:" + task.getName());
        }
    }

output result

4 Process task processing

  The person in charge of the task finds out the person to be done, selects the task to process, and completes the task

    /**
     * 流程任务的处理
     */
    @Test
    public void test07(){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = engine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processDefinitionKey("evection")
                .taskAssignee("zhansan")
                .singleResult();
        // 完成任务
        taskService.complete(task.getId());
    }

After zhangsan handles this operation, the process flows to lisi

 Then different users log in, and then query the task processing tasks until the task process is completed.

5 Query of process definition

  Query process-related information, including process definition, process deployment, and process definition version

    /**
     * 查询流程的定义
     */
    @Test
    public void test08(){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = engine.getRepositoryService();
        // 获取一个 ProcessDefinitionQuery对象 用来查询操作
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        List<ProcessDefinition> list = processDefinitionQuery.processDefinitionKey("evection")
                .orderByProcessDefinitionVersion() // 安装版本排序
                .desc() // 倒序
                .list();
        // 输出流程定义的信息
        for (ProcessDefinition processDefinition : list) {
            System.out.println("流程定义的ID:" + processDefinition.getId());
            System.out.println("流程定义的name:" + processDefinition.getName());
            System.out.println("流程定义的key:" + processDefinition.getKey());
            System.out.println("流程定义的version:" + processDefinition.getVersion());
            System.out.println("流程部署的id:" + processDefinition.getDeploymentId());
        }
    }

 output result

ID of process definition: evection:1:12504
name of process definition: business trip application
key of process definition: evection
version of process definition: 1
id of process deployment: 12501

6 Process deletion 

    /**
     * 删除流程
     */
    @Test
    public void test09(){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = engine.getRepositoryService();
        // 删除流程定义,如果该流程定义已经有了流程实例启动则删除时报错
        repositoryService.deleteDeployment("12501");
        // 设置为TRUE 级联删除流程定义,及时流程有实例启动,也可以删除,设置为false 非级联删除操作。
        //repositoryService.deleteDeployment("12501",true);
    }

Note : The permission of cascading delete operations in project development is generally only developed for super administrators.

7 Download of Process Resources 

Now our process resource files have been uploaded to the database. If other users want to view these resources, they can download these resources from the database to the local.

solution:

jdbc handles the blob type and reads the clob type data.
Use activiti's api to implement operations.
To use activiti's api to operate, we need to add the commons-io dependency
 

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

 Implementation code

    /**
     * 读取数据库中的资源文件
     */
    @Test
    public void test10() throws Exception{
        // 1.得到ProcessEngine对象
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取RepositoryService对象
        RepositoryService repositoryService = engine.getRepositoryService();
        // 3.得到查询器
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("evection")
                .singleResult();
        // 4.获取流程部署的id
        String deploymentId = definition.getDeploymentId();
        // 5.通过repositoryService对象的相关方法 来获取图片信息和bpmn信息
        // png图片
        InputStream pngInput = repositoryService
                .getResourceAsStream(deploymentId, definition.getDiagramResourceName());
        // bpmn 文件的流
        InputStream bpmnInput = repositoryService
                .getResourceAsStream(deploymentId, definition.getResourceName());
        // 6.文件的保存
        File filePng = new File("d:/evection.png");
        File fileBpmn = new File("d:/evection.bpmn");
        OutputStream pngOut = new FileOutputStream(filePng);
        OutputStream bpmnOut = new FileOutputStream(fileBpmn);

        IOUtils.copy(pngInput,pngOut);
        IOUtils.copy(bpmnInput,bpmnOut);

        pngInput.close();
        pngOut.close();
        bpmnInput.close();
        bpmnOut.close();
    }

8 View process history information

  Even if the process definition has been deleted, the instance information of process execution is still stored in the related table structure of Activiti's act_hi_* through the previous analysis, so we can still query the historical information of process execution, which can be viewed through HistoryService

    /**
     * 流程历史信息查看
     */
    @Test
    public void test11(){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 查看历史信息我们需要通过 HistoryService来实现
        HistoryService historyService = engine.getHistoryService();
        // 获取 actinst 表的查询对象
        HistoricActivityInstanceQuery instanceQuery = historyService.createHistoricActivityInstanceQuery();
        instanceQuery.processDefinitionId("evection:1:12504");
        instanceQuery.orderByHistoricActivityInstanceStartTime().desc();
        List<HistoricActivityInstance> list = instanceQuery.list();
        // 输出查询的结果
        for (HistoricActivityInstance hi : list) {
            System.out.println(hi.getActivityId());
            System.out.println(hi.getActivityName());
            System.out.println(hi.getActivityType());
            System.out.println(hi.getAssignee());
            System.out.println(hi.getProcessDefinitionId());
            System.out.println(hi.getProcessInstanceId());
            System.out.println("-----------------------");
        }
    }

output result

usertask3
总经理审批
userTask
wangwu
evection:1:12504
15001
-----------------------
usertask2
经理审批
userTask
lisi
evection:1:12504
15001
-----------------------
usertask1
创建请假单
userTask
zhansan
evection:1:12504
15001
-----------------------
startevent1
Start
startEvent
null
evection:1:12504
15001
-----------------------

The basic operation of Activiti is introduced here, and the next article will explain how to use it in detail.

Guess you like

Origin blog.csdn.net/wufaqidong1/article/details/129582659