Activiti流程定義相關

本文主要講述如何使用RepositoryService進行工作流定義相關的操作。如果還沒有搭建Activiti基礎環境請參考這篇博客:

 https://blog.csdn.net/houkai18792669930/article/details/103385764

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ActivitiApplication.class)
public class ProcessDefinitionTest {

    private Gson gson = new Gson();
    @Autowired
    private ProcessEngine processEngine;

    /**
     * 流程的部署定義
     * addInputStream 支持 ZipInputStream 可以將bpmn文件和圖片打zip包傳入
     *
     * 操作三張表:
     *  1. act_re_deployment(部署對象表) 存放流程定義的顯示名和部署時間,每部署一次增加一條記錄
     *  2.act_re_procdef(流程定義表)     存放流程定義的屬性信息,部署每個新的流程定義都會在這張表中增加一條記錄。
     *       注意:當流程定義的key相同的情況下,使用的是版本升級
     *  3.act_ge_bytearray(資源文件表) 存儲流程定義相關的部署信息。即流程定義文檔的存放地。每部署一次就會增加兩條記錄,
     *      一條是關於bpmn規則文件的,一條是圖片的(如果部署時只指定了bpmn一個文件,activiti會在部署時解析bpmn文件內容自
     *      動生成流程圖)。兩個文件不是很大,都是以二進制形式存儲在數據庫中。
     */
    @Test
    public void deployTest() throws IOException {
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .name("demo")
                .addInputStream("test.bpmn",new FileInputStream("D:\\my_svn\\Architecture-activiti\\activiti\\src\\test\\resources\\helloworld.xml"))
                .addInputStream("test.png",new FileInputStream("D:\\my_svn\\Architecture-activiti\\activiti\\src\\test\\resources\\helloworld.png"))
                .deploy();
        System.out.printf(deployment.getId() +"   "+ deployment.getName());
    }

    /**
     * 查詢流程定義 act_re_procdef
     * id規則 processDefinitionKey:processDefinitionVersion:generated-id    generated-id是一個自動生成的唯一的數字
     */
    @Test
    public void queryProcessDefinitionTest(){
        ProcessDefinition processDefinition = processEngine.getRepositoryService()
                .createProcessDefinitionQuery()
                .deploymentId("10001").singleResult();
        System.out.printf(gson.toJson(processDefinition));

        //查詢最新版本的流程定義
        List<ProcessDefinition> processDefinitionList = processEngine.getRepositoryService()
                .createProcessDefinitionQuery()
                .orderByProcessDefinitionVersion()
                .desc()
                .list();
        System.out.printf(gson.toJson(processDefinitionList.get(0)));
    }

    /**
     * 獲取流程定義文檔的資源
     *
     * 在 ACT_GE_BYTEARRAY 中獲取名稱
     * ACT_GE_BYTEARRAY 中獲取
     */
    @Test
    public void viewImageTest() throws Exception{
        List<String> nameList=  processEngine.getRepositoryService().getDeploymentResourceNames("10001");
        String imageName = null;
        for (String temp: nameList) {
            System.out.printf("name:" + temp);
            if(temp.indexOf(".png") >= 0){
                imageName = temp;
            }
        }
        System.out.printf("imageName:" + imageName);
        InputStream is = processEngine.getRepositoryService().getResourceAsStream("10001", imageName);
        byte[] buffer = new byte[is.available()];
        is.read(buffer);
        File targetFile = new File("D:\\image.png");
        try(OutputStream outStream = new FileOutputStream(targetFile);){
            outStream.write(buffer);
        }
    }

    /**
     * 刪除流程的定義
     * cascade: 是否級聯,默認爲false
     *     false: 如果存在正在執行的流程則報異常
     *     true: 會刪除當前規則相關的所有的信息例如:正在執行的信息,歷史信息
     */
    @Test
    public void deleteDeploymentTest(){
        processEngine.getRepositoryService().deleteDeployment("7501", false);

        //刪除key相同的所有不同版本的流程定義
        List<ProcessDefinition> processDefinitionList = processEngine.getRepositoryService()
                .createProcessDefinitionQuery()
                .orderByProcessDefinitionName()
                .list();
        processDefinitionList.forEach(temp->{
            processEngine.getRepositoryService().deleteDeployment(temp.getDeploymentId());
        });
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章