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(); } }