首页 > 代码库 > 流程定义管理

流程定义管理

package com.kite.pd;

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

import org.apache.log.output.FileOutputLogTarget;
import org.jbpm.api.Configuration;
import org.jbpm.api.Deployment;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.NewDeployment;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.RepositoryService;
import org.jbpm.pvm.internal.repository.RepositoryCache;
import org.junit.Test;

/**
 * 流程定义管理
 *         流程定义
 *         把流程定义文档部署到jdpm中
 *         查询
 *         删除
 *         查看流程图
 * @author admin
 *
 */
public class PDManager {
        
    
    /**
     * 牵涉到的表
     *      *  JBPM4_DEPLOYMENT
     *             部署表  用来说明一次部署
     *             字段说明
     *                  DBID_: 主键、部署ID
     *                  STATE: 状态   active    
     *      *  JBPM4_LOB 
     *             仓库表 存放了流程定义的文档(xml.png)
     *             字段说明
     *                 DEPLOYMENT_:部署ID  外键
     *                 NAME_:  xml或者png的文件路径
     *   *  JBPM4_DEPLOYPROP
     *           部署属性表
     *           字段
     *               DBID_:主键
     *               OBJNAME_:流程定义名称
     *               KEY_:
     *                   每部署一次,生成4行记录
     *                        langid  语言版本  jpdl-4.4
     *                        pdid    {pdkey-version}
     *                        pdkey   流程定义名称
     *                            一般情况下,pdkey和objname_的值是一样的
     *                        pdversion  版本号
     *                            如果pdkey没有发生改变,每部署一次,版本号加1
     *                            如果pdkey发生改变,则是一个全新的名称,所以版本号应该从1开始计算
     */
    /**
     * 从classpath加载
     */
    @Test
    public void testDeployFromClasspath(){
        //Process 过程  Engine 发动机  给 什么安装发动机
        ProcessEngine processEngine =  Configuration.getProcessEngine();
//        RepositoryService repositoryService = processEngine.getRepositoryService();
//        NewDeployment newDeployment =  repositoryService.createDeployment();
//        newDeployment.addResourceFromClasspath("");
//        newDeployment.addResourceFromClasspath("");
//        newDeployment.deploy();
        processEngine.getRepositoryService().
        createDeployment().
        addResourceFromClasspath("qingjia.jpdl.xml").//加载文件
        addResourceFromClasspath("qingjia.png").//加载文件
        deploy();
    }
    
    /**
     * 从inputstream 加载
     */
    @Test
    public void testDeployFromInputStream(){
        
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("qingjia.jpdl.xml");
        Configuration.getProcessEngine()
        .getRepositoryService()
        .createDeployment()
        .addResourceFromInputStream("qingjia.jpdl.xml", inputStream).deploy();
    }
    
    /**
     * 从zipinputstram 中加载
     */
    @Test
    public void testDeployFromZipInputstream(){
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("qingjia.zip");
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        Configuration.getProcessEngine()
        .getRepositoryService()
        .createDeployment()
        .addResourcesFromZipInputStream(zipInputStream)
        .deploy();
    }
    
    /**
     * 查询所有的部署
     */
    @Test
    public void testQueryAllDeploy(){
        List<Deployment> deploymentList =  Configuration.getProcessEngine().getRepositoryService()
        .createDeploymentQuery()
        .list();
        
        for(Deployment deployment : deploymentList){
            System.out.print(deployment.getId() + "\t");
            System.out.println(deployment.getState());
        }
    }
    /**
     * 根据部署id查询的部署
     */
    @Test
    public void testQueryDeployByID(){
        Deployment deployment = 
        Configuration.getProcessEngine().getRepositoryService()
        .createDeploymentQuery().deploymentId("10001").uniqueResult();
        System.out.print(deployment.getId() + "\t");
        System.out.println(deployment.getState());
    }
    
    /**
     * 查询所有的流程定义
     */
    @Test
    public void testQueryAllPD(){
        List<ProcessDefinition> pdList =  Configuration.getProcessEngine().getRepositoryService()
        .createProcessDefinitionQuery()
        .list();
        
        for(ProcessDefinition processDefinition : pdList){
            System.out.print("部署id:" +  processDefinition.getDeploymentId() + "\t");
            
            System.out.print("流程id:" + processDefinition.getId()+ "\t");
            
            System.out.println("图片路径:" +  processDefinition.getImageResourceName());
        }
    }
    /**
     * 根据部署id查询流程定义
     */
    @Test
    public void testQueryPDByID(){
        ProcessDefinition processDefinition = Configuration.getProcessEngine().getRepositoryService()
        .createProcessDefinitionQuery().deploymentId("10001").uniqueResult();
        
        
        System.out.print("部署id:" +  processDefinition.getDeploymentId() + "\t");
        
        System.out.print("流程id:" + processDefinition.getId()+ "\t");
        
        System.out.println("图片路径:" +  processDefinition.getImageResourceName());
    }
    /**
     * 根据PDID查询流程定义
     */
    @Test
    public void testQueryPDByPDID(){
        ProcessDefinition processDefinition = Configuration.getProcessEngine().getRepositoryService()
        .createProcessDefinitionQuery().processDefinitionId("qingjia-1").uniqueResult();
            System.out.print("部署id:" +  processDefinition.getDeploymentId() + "\t");
            
            System.out.print("流程id:" + processDefinition.getId()+ "\t");
            
            System.out.println("图片路径:" +  processDefinition.getImageResourceName());
    }
    
    /**
     * 根据pdkey查询流程定义
     */
    @Test
    public void testQueryPDByPDKEY(){
        List<ProcessDefinition> pdList = Configuration.getProcessEngine()
        .getRepositoryService()
        .createProcessDefinitionQuery()
        .processDefinitionKey("qingjia").list();
        
        for(ProcessDefinition processDefinition : pdList){
            System.out.print("部署id:" +  processDefinition.getDeploymentId() + "\t");
            
            System.out.print("流程id:" + processDefinition.getId()+ "\t");
            
            System.out.println("图片路径:" +  processDefinition.getImageResourceName());
        }
    }
    
    /**
     *  删除
     *      只能直接删除流程部署
     *      而没有提供删除流程定义的api  
     */
    @Test
    public void testDeleteDeployment(){
        Configuration.getProcessEngine().getRepositoryService()
        .deleteDeployment("40001"); //d
    }
    /**
     * 根据key得到所有的流程定义,然后遍历每一个流程定义,得到流程部署,然后依次删除
     */
    
    /**
     * 查询流程图
     * @throws FileNotFoundException 
     */
    @Test
    public void testQueryImage() throws Exception{
        InputStream inputStream = Configuration.getProcessEngine()
        .getRepositoryService()
        //第一个为deploymentid 第二个参数为lob表中的name属性
        .getResourceAsStream("1", "qingjia.png");
        OutputStream outputStream = new FileOutputStream("d:\\a.png");
        
        byte[] buf = new byte[1024];
        int len = 0;
        while((len = inputStream.read(buf)) != -1){
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
    }
    
    
}