JBPM 流程定义的增删改查

来源:互联网 发布:excel2003显示重复数据 编辑:程序博客网 时间:2024/04/29 04:39
package com.itcast.processdefinition;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import java.util.zip.ZipInputStream;import org.jbpm.api.Configuration;import org.jbpm.api.ProcessDefinition;import org.jbpm.api.ProcessDefinitionQuery;import org.jbpm.api.ProcessEngine;import org.junit.Test;public class ProcessDefinitionTest {    private static ProcessEngine processEngine;    static{        //processEngine = new Configuration().setResource("jbpm.cfg.xml").buildProcessEngine();        //processEngine = new Configuration().buildProcessEngine();        processEngine = Configuration.getProcessEngine();    }    //部署(添加)    @Test    public void deploy() throws Exception {        String deployId = processEngine.getRepositoryService().createDeployment()        .addResourceFromClasspath("helloworld/helloworld.jpdl.xml")        .addResourceFromClasspath("helloworld/helloworld.png")        .deploy();        System.out.println("部署成功:deployId=="+deployId);    }    //部署(添加)    @Test    public void deployZip() throws Exception {        InputStream is = this.getClass().getClassLoader().getResourceAsStream("helloworld/helloworld.zip");        String deployId = processEngine.getRepositoryService().createDeployment()        //.addResourceFromFile(new File("d:/helloworld.zip"))        .addResourcesFromZipInputStream(new ZipInputStream(is))        .deploy();        System.out.println("部署成功:deployId=="+deployId);    }    //查询    @Test    public void findAll() throws Exception {        List<ProcessDefinition> list = processEngine.getRepositoryService().createProcessDefinitionQuery()        .processDefinitionKey("helloworld")        .processDefinitionNameLike("%helloworld%")        .orderAsc(ProcessDefinitionQuery.PROPERTY_ID)        .orderDesc(ProcessDefinitionQuery.PROPERTY_KEY)        //.page(firstResult, maxResults)        .list();        if(list != null){            for(ProcessDefinition pd : list){                System.out.println("deploymentId="+pd.getDeploymentId()                        +",name="+pd.getName()                        +",version="+pd.getVersion()                        +",id="+pd.getId()                        +",key="+pd.getKey()                        );            }        }    }    //查询最新版本的流程    @Test    public void findAllLatestVersions() throws Exception {        List<ProcessDefinition> list = processEngine.getRepositoryService().createProcessDefinitionQuery()        .orderAsc(ProcessDefinitionQuery.PROPERTY_VERSION)        .list();        //这样map里存的就是最新的ProcessDefinition        Map<String, ProcessDefinition> map = new HashMap<String, ProcessDefinition>();        for(ProcessDefinition pd : list){            map.put(pd.getKey(), pd);        }        if(list != null){            for(ProcessDefinition pd : map.values()){                System.out.println("deploymentId="+pd.getDeploymentId()                        +",name="+pd.getName()                        +",version="+pd.getVersion()                        +",id="+pd.getId()                        +",key="+pd.getKey()                        );            }        }    }    //查询    @Test    public void count() throws Exception {        long count = processEngine.getRepositoryService().createProcessDefinitionQuery()                .processDefinitionKey("helloworld")                .processDefinitionNameLike("%helloworld%")                .orderAsc(ProcessDefinitionQuery.PROPERTY_ID)                .orderDesc(ProcessDefinitionQuery.PROPERTY_KEY)                .count();        System.out.println("count=="+ count);    }    //查询    @Test    public void find() throws Exception {        String deploymentId = "80001";        ProcessDefinition pd = processEngine.getRepositoryService().createProcessDefinitionQuery()                .processDefinitionKey("helloworld")                .processDefinitionNameLike("%helloworld%")                .orderAsc(ProcessDefinitionQuery.PROPERTY_ID)                .orderDesc(ProcessDefinitionQuery.PROPERTY_KEY)                .deploymentId(deploymentId)                .uniqueResult();        System.out.println("id="+pd.getDeploymentId()+",name="+pd.getName()+",assigne="+pd.getVersion());    }    //删除    @Test    public void deleteById() throws Exception {        String deploymentId = "80001";        //有关联的信息删除时会报错        //processEngine.getRepositoryService().deleteDeployment(deploymentId);        //级联删除        processEngine.getRepositoryService().deleteDeploymentCascade(deploymentId);    }    //删除所有    @Test    public void deleteAllByKey() throws Exception {        List<ProcessDefinition> list = processEngine.getRepositoryService().createProcessDefinitionQuery()                .orderAsc(ProcessDefinitionQuery.PROPERTY_VERSION)                .list();        //有关联的信息删除时会报错        //processEngine.getRepositoryService().deleteDeployment(deploymentId);        for(ProcessDefinition pd : list){            //级联删除            processEngine.getRepositoryService().deleteDeploymentCascade(pd.getDeploymentId());        }    }    //查看流程图(***.png)    @Test    public void getImageResource() throws Exception {        String deploymentId = "80001";        String resourceName = "helloworld/helloworld.png";        Set<String> resourceNames = processEngine.getRepositoryService().getResourceNames(deploymentId);        //processEngine.getRepositoryService().getResourceAsStream(deploymentId, resourceName);        for(String name :resourceNames){            System.out.println("imageName=="+name + "\t\n");        }        InputStream is = processEngine.getRepositoryService().getResourceAsStream(deploymentId, resourceName);        FileOutputStream fos = new FileOutputStream(new File("d:/process.png"));        int len = -1;        while((len = is.read())!=-1){            fos.write(len);        }        is.close();        fos.close();    }}
0 0