功能:对流程的增加 修改 删除 查询
act_ge_bytearray act_re_deployment act_re_procdef
1.流程图
2.部署流程【两种方式 classpath zip】
//得到流程引擎
private ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
/**
* 部署流程使用classpath
*/
@Test
public void deployProcess01() {
//得到流程部署的service
RepositoryService repositoryService = this.processEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment().name("请假流程001")
.addClasspathResource("HelloWorld.bpmn")
.addClasspathResource("HelloWorld.png")
.deploy();
System.out.println("部署成功:流程部署ID:"+deploy.getId());
}
/**
* 部署流程使用zip
* 流程图的文件必须是xxxx.zip结尾
*/
@Test
public void deployProcess02() {
//如果不加/代表从当前包里面找文件
InputStream inputStream = this.getClass().getResourceAsStream("/HelloWorld.zip");
//如果加/代表从classpath的根目录里面找文件
//InputStream inputStream = this.getClass().getResourceAsStream("/HelloWorld.zip");
RepositoryService repositoryService = this.processEngine.getRepositoryService();
ZipInputStream zipInputStream =new ZipInputStream(inputStream);
Deployment deploy = repositoryService.createDeployment().name("请假流程001")
.addZipInputStream(zipInputStream)//添加流程图的流
.deploy();//确定部署
System.out.println("部署成功,部署ID:"+deploy.getId());
}
3. 查询部署信息act_re_deployment
/**
* 查询流程部署信息 act_re_deployment
*/
@Test
public void queryProcessDeploy () {
RepositoryService repositoryService = this.processEngine.getRepositoryService();
//创建部署信息的查询
String deploymentId="1";
// Deployment deploy = repositoryService.createDeploymentQuery()
List<Deployment> list= repositoryService.createDeploymentQuery()
//条件
//.deploymentId(deploymentId) //根据部署ID去查询
//.deploymentName(name)//根据部署名称去查询
//.deploymentTenantId(tenantId)//根据tenantId去查询
// .deploymentNameLike(nameLike)//根据部署名称模糊查询
//.deploymentTenantIdLike(tenantIdLike)//根据tenantId模糊查询
//排序
// .orderByDeploymentId().asc() //根据部署ID升序
//.orderByDeploymenTime().desc() //根据部署时间降序
//.orderByDeploymentName()//根据部署名称升序
//结果集
.list(); //查询返回list集合
// .listPage(firstResult, maxResults) 分页查询返回list集合
//.singleResult(); //返回单个对象
// .count();
/*System.out.println("部署ID:"+deploy.getId());
System.out.println("部署名称:"+deploy.getName());
System.out.println("部署时间:"+deploy.getDeploymentTime());*/
// System.out.println(count);
for (Deployment deployment : list) {
System.out.println("部署ID:"+deployment.getId());
System.out.println("部署名称:"+deployment.getName());
System.out.println("部署时间:"+deployment.getDeploymentTime());
System.out.println("########################");
}
}
4.查询流程定义信息act_re_procdef
/**
* 查询流程定义
*/
@Test
public void queryProcDef() {
RepositoryService repositoryService = this.processEngine.getRepositoryService();
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
//条件
// .deploymentId(deploymentId) 根据部署ID查询
// .deploymentIds(deploymentIds) 根据部署ID的集合查询Set<String> deploymentIds
// .processDefinitionId(processDefinitionId)//根据流程定义IDHelloWorld:1:4
// .processDefinitionIds(processDefinitionIds)//根据流程定义的IDS查询
// .processDefinitionKey(processDefinitionKey)//根据流程定义的的key查询
// .processDefinitionKeyLike(processDefinitionKeyLike)//根据流程定义的的key模糊查询
// .processDefinitionName(processDefinitionName)//根据流程定义的名称查询
// .processDefinitionNameLike(processDefinitionNameLike)//根据流程定义的名称模糊查询
// .processDefinitionResourceName(resourceName)//根据流程图的BPMN文件名查询
// .processDefinitionResourceNameLike(resourceNameLike)//根据流程图的BPMN文件名模糊查询
// .processDefinitionVersion(processDefinitionVersion)//根据流程定义的版本查询
// .processDefinitionVersionGreaterThan(processDefinitionVersion)//version>num
// .processDefinitionVersionGreaterThanOrEquals(processDefinitionVersion)//version>=num
// .processDefinitionVersionLowerThan(processDefinitionVersion)//version<num
// .processDefinitionVersionLowerThanOrEquals(processDefinitionVersion)//version<=num
//排序
// .orderByDeploymentId()
// .orderByProcessDefinitionId()
// .orderByProcessDefinitionKey()
// .orderByProcessDefinitionName()
// .orderByProcessDefinitionVersion()
//结果集
.list();
// .listPage(firstResult, maxResults)\
// .count()
// .singleResult()
if(null!=list&&list.size()>0) {
for (ProcessDefinition pd : list) {
System.out.println("流程定义ID:"+pd.getId());
System.out.println("流程部署ID:"+pd.getDeploymentId());
System.out.println("流程定义KEY:"+pd.getKey());
System.out.println("流程定义的名称:"+pd.getName());
System.out.println("流程定义的bpmn文件名:"+pd.getResourceName());//bpmn的name
System.out.println("流程图片名:"+pd.getDiagramResourceName());//png的name
System.out.println("流程定义的版本号:"+pd.getVersion());
System.out.println("##################");
}
}
}
5.删除流程定义信息
/**
* 删除流程定义
*/
@Test
public void deleteProcessDef() {
RepositoryService repositoryService = this.processEngine.getRepositoryService();
String deploymentId="2501";
//根据流程部署id删除流程定义 如果当前id的流程正在执行,那么会报错
//repositoryService.deleteDeployment(deploymentId);
//根据流程部署id删除删除流程定义 如果当前id的流程正在执行,会把正在执行的流程数据删除 act_ru_*和act_hi_*表里面的数据
repositoryService.deleteDeployment(deploymentId, true);
// repositoryService.deleteDeploymentCascade(deploymentId);==repositoryService.deleteDeployment(deploymentId, true);
System.out.println("删除成功");
}
6.修改流程定义信息
修改流程图之后重新部署,只要key不变,它的版本号就会+1
7.查询流程图
/**
* 查询流程图 根据流程定义ID
*/
@Test
public void viewProcessImg() {
RepositoryService repositoryService = this.processEngine.getRepositoryService();
String processDefinitionId="HelloWorld:1:4";
InputStream inputStream = repositoryService.getProcessDiagram(processDefinitionId);
File file=new File("d:/HelloWorld.png");
try {
BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream(file));
int len=0;
byte[] b=new byte[1024];
while((len=inputStream.read(b))!=-1) {
outputStream.write(b, 0, len);
outputStream.flush();
}
outputStream.close();
inputStream.close();
System.out.println("查询成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 查询流程图 根据流流程部署ID
*/
@Test
public void viewProcessImg2() {
RepositoryService repositoryService = this.processEngine.getRepositoryService();
//根据流程部署ID查询流程定义对象
String deploymentId="1";
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
//从流程定义对象里面查询出流程定义ID
String processDefinitionId=processDefinition.getId();
InputStream inputStream = repositoryService.getProcessDiagram(processDefinitionId);
File file=new File("d:/"+processDefinition.getDiagramResourceName());
try {
BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream(file));
int len=0;
byte[] b=new byte[1024];
while((len=inputStream.read(b))!=-1) {
outputStream.write(b, 0, len);
outputStream.flush();
}
outputStream.close();
inputStream.close();
System.out.println("查询成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
8.附加功能:查询最新版本的流程定义
/**
* 查询最新的流程定义
*/
@Test
public void queryNewProcessDef() {
Map<String, ProcessDefinition> map = new HashMap<>();
// 查询所有的流程定义根据版本号升序
RepositoryService repositoryService = this.processEngine.getRepositoryService();
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
.orderByProcessDefinitionVersion().asc().list();
if(null!=list&&list.size()>0) {
for (ProcessDefinition pd : list) {
map.put(pd.getKey(), pd);
}
}
//循环map集合
Collection<ProcessDefinition> values = map.values();
for (ProcessDefinition pd : values) {
System.out.println("流程定义ID:" + pd.getId());
System.out.println("流程部署ID:" + pd.getDeploymentId());
System.out.println("流程定义KEY:" + pd.getKey());
System.out.println("流程定义的名称:" + pd.getName());
System.out.println("流程定义的bpmn文件名:" + pd.getResourceName());// bpmn的name
System.out.println("流程图片名:" + pd.getDiagramResourceName());// png的name
System.out.println("流程定义的版本号:" + pd.getVersion());
System.out.println("##################");
}
}
9.附加功能:删除流程定义(删除key相同的所有不同版本的流程定义)
/**
* 已知key 附加功能:删除流程定义(删除key相同的所有不同版本的流程定义)
*/
public void deleteAllSameVersion() {
String processDefinitionKey = "HelloWorld";
RepositoryService repositoryService = this.processEngine.getRepositoryService();
// 根据流程定义的key查询流程集合
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey(processDefinitionKey).list();
if (null != list && list.size() > 0) {
for (ProcessDefinition pd : list) {
repositoryService.deleteDeployment(pd.getDeploymentId(), true);
}
}
}