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;
/**
* Process definition management
** Process Definition
* * Deploy the process definition document to jbpm
* * Inquire
** Query process deployment
* * Query all process deployments
* * Query process deployment based on deployment ID
** Query process definition
* * Query all process definitions
* * Query process definition based on deploymentID
* * According to pdid query process definition
* * Defined according to the pdkey query process
* * delete
** View flowchart
* @author Administrator
*
*/
public class PDManager extends BaseJbpm{
/**
* Tables involved
* * JBPM4_DEPLOYMENT
* * Deployment table, used to describe a deployment
** Field Description
* DBID_: primary key, deployment ID
* STATE: state active
* * JBPM4_LOB
* * Warehouse table stores process definition documents (xml, png)
** Field Description
* DEPLOYMENT_: Deployment ID foreign key
* NAME_: xml or png file path
* * JBPM4_DEPLOYPROP
** Deployment property sheet
** Field
* DBID_: primary key
* OBJNAME_: Process definition name
* KEY_:
* * For each deployment, 4 lines of records are generated
* langid language version jpdl-4.4
* pdid {pdkey-version}
* pdkey process definition name
* In general, the values of pdkey and objname_ are the same
* pdversion version number
* If the pdkey has not changed, the version number is incremented by 1 each time it is deployed
* If the pdkey changes, it is a brand new name, so the version number should start from 1
*/
/**
* Load from 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();
}
/**
* Load from inputstream
*/
@Test
public void testDeployFromInputStream(){
//this.getClass().getClassLoader().getResourceAsStream("qingjia.jpdl.xml") gets the classpath path (src)
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("qingjia.jpdl.xml");
processEngine.getRepositoryService ()
.createDeployment()
.addResourceFromInputStream("qingjia.jpdl.xml", inputStream)
.deploy();
}
/**
* Load from zipinoutStream
*/
@Test
public void testDeployFromZipinputStream(){
InputStream in = this.getClass().getClassLoader().getResourceAsStream("qingjia.zip");
ZipInputStream zipInputStream = new ZipInputStream(in);
processEngine.getRepositoryService ()
.createDeployment()
.addResourcesFromZipInputStream(zipInputStream)
.deploy();
}
/**
* Query all deployments
*/
@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());
}
}
/**
* Query deployment based on deployment 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());
}
/**
* Query all process definitions
*/
@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());
}
}
/**
* Query process definition based on deployment 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());
}
/**
* According to PDID query process definition
*/
@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());
}
/**
* Defined according to the pdkey query process
*/
@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());
}
}
/**
* delete
* Only process deployment can be deleted directly
* without providing an API to delete process definitions
*/
@Test
public void testDeleteDeployment(){
processEngine.getRepositoryService ()
.deleteDeploymentCascade("10001");
}
/**
* Get all process definitions according to the key, then traverse each process definition, get the process deployment, and delete them in turn
*/
/**
* Query flow chart
*/
@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();
}
}