Activiti 流程定义实例

Source

转载自:http://blog.csdn.net/caoyue_new/article/details/52164756

package com.activiti.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.junit.Test;

public class ProcessDefinitionTest {

    //获取流程定义
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**部署流程定义(方法一:从classpath下加载的资源,一次只能加载一个文件)*/
    @Test
    public void deploy() {
        Deployment deploy = processEngine.getRepositoryService()
                .createDeployment()
                .name("请假流程")
                .addClasspathResource("diagrams/leave.bpmn")
                .addClasspathResource("diagrams/leave.bpmn")
                .deploy();
        System.out.println("部署ID:" + deploy.getId());
        System.out.println("部署名称:" + deploy.getName());
    }

    /**部署流程定义(方法二:从类路径下加载zip)**/
    @Test
    public void deployZip() {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("diagrams/leave.zip");
        ZipInputStream zipInput = new ZipInputStream(in);
        Deployment deploy = processEngine.getRepositoryService()
                .createDeployment()
                .name("请假流程")
                .addZipInputStream(zipInput)
                .deploy();
        System.out.println("部署ID:" + deploy.getId());
        System.out.println("部署名称:" + deploy.getName());
    }

    /**查询流程定义*/
    @Test
    public void queryProcessDefinition() {
        List<ProcessDefinition> list = processEngine.getRepositoryService()//与流程定义和部署对象相关的Service
                .createProcessDefinitionQuery()//创建流程定义查询对象
                /**指定查询条件,where条件*/
//              .latestVersion()
//              .deploymentId(deploymentId)//使用部署对象ID查询
//              .processDefinitionId(processDefinitionId)//使用流程定义ID查询
//              .processDefinitionKey(processDefinitionKey)//使用流程定义的key查询
//              .processDefinitionNameLike(processDefinitionNameLike)//使用流程定义的名称模糊查询

                /**排序*/
                .orderByProcessDefinitionVersion().asc()//按照版本的升序排列
//              .orderByProcessDefinitionName().desc()//按照流程定义的名称降序排列

                /**返回的结果集*/
                .list();//返回一个集合列表,封装流程定义
//              .singleResult();//返回惟一结果集
//              .count();//返回结果集数量
//              .listPage(firstResult, maxResults);//分页查询

        if (list != null && list.size() > 0) {
            for (ProcessDefinition pd : list) {
                System.out.println("流程定义ID:" + pd.getId());//流程定义的key+版本+随机生成数
                System.out.println("流程定义的名称:" + pd.getName());//对应helloworld.bpmn文件中的name属性值
                System.out.println("流程定义的key:" + pd.getKey());//对应helloworld.bpmn文件中的id属性值
                System.out.println("流程定义的版本:" + pd.getVersion());//当流程定义的key值相同的相同下,版本升级,默认1
                System.out.println("资源名称bpmn文件:" + pd.getResourceName());
                System.out.println("资源名称png文件:" + pd.getDiagramResourceName());
                System.out.println("部署对象ID:" + pd.getDeploymentId());
                System.out.println("=============================================");
            }
        }
    }

    /***删除流程定义**/
    @Test
    public void deleteProcessDefinition() {
        String deploymentId = "7501";
        RepositoryService repositoryService = processEngine.getRepositoryService();

        //不带级联的删除:只能删除没有启动的流程,如果流程启动,就会抛出异常
        //repositoryService.deleteDeployment(deploymentId);

        //级联删除:不管流程是否启动,都能可以删除
        repositoryService.deleteDeployment(deploymentId, true);
        System.out.println("删除成功");
    }

    /**查看流程图(将生成图片放到文件夹下)**/
    @Test
    public void viewProcessDefinition() {
        String deploymentId = "20001";
        //获取图片路径
        List<String> list = processEngine.getRepositoryService()
                .getDeploymentResourceNames(deploymentId);
        String resourceName = null;
        if (list != null && list.size() > 0) {
            for (String str : list) {
                if (str.endsWith(".png")) {
                    resourceName = str;
                    break;
                }
            }
        }

        InputStream in = processEngine.getRepositoryService()
                .getResourceAsStream(deploymentId, resourceName);//获取图片输入流
        File file = new File("D:/" + resourceName);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
            byte[] words = new byte[64];
            while (in.read(words) != -1) {
                out.write(words);
            }
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /***删除所有key相同的流程定义**/
    @Test
    public void removeAllByKey() {
        String processDefinitionKey = "helloworld";
        List<ProcessDefinition> list = processEngine.getRepositoryService()
                .createProcessDefinitionQuery()
                .processDefinitionKey(processDefinitionKey)
                .list();
        if (list != null && list.size() > 0) {
            for (ProcessDefinition processDefinition : list) {
                processEngine.getRepositoryService()
                    .deleteDeployment(processDefinition.getDeploymentId(), true);
            }
        }
        System.out.println("删除成功");
    }
}