package com.nantian.jbpm.utils;
import org.jbpm.api.Configuration;
import org.jbpm.api.ProcessEngine;
import org.junit.Before;
public class BaseJbpm {
public static ProcessEngine processEngine;
@Before
public void testBase(){
this.processEngine = Configuration.getProcessEngine();
}
}
package com.nantian.jbpm.pd;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipInputStream;
import org.jbpm.api.Deployment;
import org.jbpm.api.ProcessDefinition;
import org.junit.Test;
import com.nantian.jbpm.utils.BaseJbpm;
/**
* 流程定义管理
* * 流程定义
* * 把流程定义文档部署到jbpm中
* * 查询
* * 查询流程部署
* * 查询所有的流程部署
* * 根据部署ID查询流程部署
* * 查询流程定义
* * 查询所有的流程定义
* * 根据deploymentID查询流程定义
* * 根据pdid查询流程定义
* * 根据pdkey查询流程定义
* * 删除
* * 查看流程图
* @author Administrator
*
*/
public class PDManager extends BaseJbpm{
/**
* 涉及到的表
* * 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(){
//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(){
//this.getClass().getClassLoader().getResourceAsStream("qingjia.jpdl.xml")获得classpath路径(src)
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("qingjia.jpdl.xml");
processEngine.getRepositoryService()
.createDeployment()
.addResourceFromInputStream("qingjia.jpdl.xml", inputStream)
.deploy();
}
/**
* 从zipinoutStream加载
*/
@Test
public void testDeployFromZipinputStream(){
InputStream in = this.getClass().getClassLoader().getResourceAsStream("qingjia.zip");
ZipInputStream zipInputStream = new ZipInputStream(in);
processEngine.getRepositoryService()
.createDeployment()
.addResourcesFromZipInputStream(zipInputStream)
.deploy();
}
/**
* 查询所有的部署
*/
@Test
public void testQueryAllDeploy(){
List<Deployment> deploymentList = processEngine.getRepositoryService()
.createDeploymentQuery()
.list();
for(Deployment deployment:deploymentList){
System.out.print(deployment.getId());
System.out.print(" ");
System.out.println(deployment.getState());
}
}
/**
* 根据部署ID查询部署
*/
@Test
public void testQueryDeployByID(){
Deployment deployment = processEngine.getRepositoryService()
.createDeploymentQuery()
.deploymentId("10001")
.uniqueResult();
System.out.print(deployment.getId());
System.out.print(" ");
System.out.println(deployment.getState());
}
/**
* 查询所有的流程定义
*/
@Test
public void testQueryAllPD(){
List<ProcessDefinition> pdList = processEngine.getRepositoryService()
.createProcessDefinitionQuery()
.list();
for(ProcessDefinition pd:pdList){
System.out.print("deploymentId:"+pd.getDeploymentId()+",");
System.out.print("imageURL:"+pd.getImageResourceName()+",");
System.out.print("key:"+pd.getKey()+",");
System.out.println("version:"+pd.getVersion());
}
}
/**
* 根据部署ID查询流程定义
*/
@Test
public void testQueryPDByDeploymentId(){
ProcessDefinition pd = processEngine.getRepositoryService()
.createProcessDefinitionQuery()
.deploymentId("30001")
.uniqueResult();
System.out.print("deploymentId:"+pd.getDeploymentId()+",");
System.out.print("imageURL:"+pd.getImageResourceName()+",");
System.out.print("key:"+pd.getKey()+",");
System.out.println("version:"+pd.getVersion());
}
/**
* 根据PDID查询流程定义
*/
@Test
public void testQueryPDByPDID(){
ProcessDefinition pd = processEngine.getRepositoryService()
.createProcessDefinitionQuery()
.processDefinitionId("qingjia-2")
.uniqueResult();
System.out.print("deploymentId:"+pd.getDeploymentId()+",");
System.out.print("imageURL:"+pd.getImageResourceName()+",");
System.out.print("key:"+pd.getKey()+",");
System.out.println("version:"+pd.getVersion());
}
/**
* 根据pdkey查询流程定义
*/
@Test
public void testQueryPDByPDKEY(){
List<ProcessDefinition> pdList = processEngine
.getRepositoryService()
.createProcessDefinitionQuery()
.processDefinitionKey("qingjia")
.list();
for(ProcessDefinition pd:pdList){
System.out.print("deploymentId:"+pd.getDeploymentId()+",");
System.out.print("imageURL:"+pd.getImageResourceName()+",");
System.out.print("key:"+pd.getKey()+",");
System.out.println("version:"+pd.getVersion());
}
}
/**
* 删除
* 只能直接删除流程部署
* 而没有提供删除流程定义的API
*/
@Test
public void testDeleteDeployment(){
processEngine.getRepositoryService()
.deleteDeploymentCascade("10001");
}
/**
* 根据key得到所有的流程定义,然后遍历每一个流程定义,得到流程部署,然后依次删除
*/
/**
* 查询流程图
*/
@Test
public void testShowImage() throws Exception{
InputStream inputStream = processEngine.getRepositoryService()
.getResourceAsStream("20001", "qingjia.png");
OutputStream outputStream = new FileOutputStream("c:/processimg.png");
for(int b=-1;(b=inputStream.read())!=-1;){
outputStream.write(b);
}
inputStream.close();
outputStream.close();
}
}