基于Activiti工作流引擎(二)

流程定义的CRUD例子

package com;
import java.io.File;
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.apache.commons.io.FileUtils;
import org.junit.Test;
public class DeploymentDefinition {
    ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
    /**部署流程定义方法一(classpath路径下加载资源文件)*/
    @Test
    public void deploy()throws Exception{
        Deployment deployment=processEngine.getRepositoryService()
                              .createDeployment()
                              .name("请假流程名称") //添加部署规则的显示别名
                              .addClasspathResource("diagrams/MyProcess.bpmn")//添加定义的规则文件
                              .addClasspathResource("diagrams/MyProcess.png")//添加定义的规则图片
                              .deploy();
        System.out.println(deployment.getId()+"========="+deployment.getName());
    }
    /**部署流程定义方法二*/
    @Test
    public void deployZip()throws Exception{
        InputStream in= this.getClass().getClassLoader().getResourceAsStream("diagrams/MyProcess.zip");
        ZipInputStream zipInputStream=new ZipInputStream(in);
        Deployment deployment=processEngine.getRepositoryService()
                              .createDeployment()
                              .name("请假流程")  //添加部署规则的别名
                              .addZipInputStream(zipInputStream) //使用zip的输入流完成部署
                              .deploy();//完成发布
        System.out.println(deployment.getId()+"============"+deployment.getName());
    }
    @Test
    /**
     * 查看流程定义
     *     id:(key):(version):(随机值)
     *     name:对应流程文件process节点的name属性
     *     key:对应流程文件process节点的id属性
     *     version:发布时自动生成的,如果第一次发布的流程,version默认从1开始;
     *     如果当前流程引擎中已经存在相同key的流程,则找到当前key对应的最高版本号,在最高的版本号上加1
     * @throws Exception
     */
    public void queryProcessDefinition() throws Exception{
    List pdList= processEngine.getRepositoryService()
                .createProcessDefinitionQuery()
                //添加查询条件
                //.processDefinitionName(processDefinitionName)
                //.processDefinitionId(processDefinitionId)
                //.processDefinitionKey(processDefinitionKey)
                //排序
                .orderByProcessDefinitionVersion().asc()
                //查询的结果集
                //.count();返回结果集数量
                //.listPage(firstResult, maxResult)分页查询
                //.singleResult()//唯一结果集
                .list();
    for (ProcessDefinition pd : pdList) {
        System.out.println("id:"+pd.getId());
        System.out.println("name:"+pd.getName());
        System.out.println("key:"+pd.getKey());
        System.out.println("version:"+pd.getVersion());
        System.out.println("resuourceName:"+pd.getDiagramResourceName());   
    }
    }
    /**删除流程*/
    @Test
    public void deleteDeployment()throws Exception{
        String deploymentId="1";
        //获取仓库服务对象
        RepositoryService repositoryService=processEngine.getRepositoryService();
        /*
         * 普通删除,如果当前有正在执行规则的流程则抛出异常
         */
        repositoryService.deleteDeployment(deploymentId);
        System.out.println("删除成功!");
    }
    /**查看流程附件(查看流程图片)*/
    @Test
    public void viewImage()throws Exception{
        String deploymentId="601";
        List names= processEngine.getRepositoryService().getDeploymentResourceNames(deploymentId);
        String imgName="";
        if(names!=null){
            for (String name : names) {
                if(name.indexOf(".png")>-1){
                    imgName=name;
                }
            }
        }
        if(imgName!=null){
            File f=new File("D:/"+imgName);
            //通过部署ID和文件名称得到文件输入流
            InputStream in=processEngine.getRepositoryService().getResourceAsStream(deploymentId, imgName);
            FileUtils.copyInputStreamToFile(in, f);
        }
    }

}

你可能感兴趣的:(工作流引擎学习,工作流引擎)