activiti工作流07-processDefinition的CRUD
曾喜
2023-12-01
package com.hhm.activitiDemo.test02;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.io.FileUtils;
/**
* 测试流程定义的增删改查
* @author Administrator
*
*/
public class ProcessDefinitionCRUD {
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
public static void main(String[] args) throws IOException {
ProcessDefinitionCRUD processDefinitionCRUD=new ProcessDefinitionCRUD();
// processDefinitionCRUD.testDeployedDefiByClassPathBPM();
// processDefinitionCRUD.testDeployedDefiByZip();
// processDefinitionCRUD.queryProceDef();
// processDefinitionCRUD.startProcessDef();
// processDefinitionCRUD.testDeleteNativeProcDef();
// processDefinitionCRUD.testDeleteActiveProcDef();
processDefinitionCRUD.testGetProcDefImg();
}
/**
* 部署流程定义,相当于加入,
*/
public void testDeployedDefiByClassPathBPM(){
//获取仓库对象
Deployment deployment=processEngine.getRepositoryService()
.createDeployment()
.name("helloworld流程定义")//部署流程定义的名称
.addClasspathResource("diagrams/helloWorld.bpmn")//加载资源文件
.addClasspathResource("diagrams/helloWorld.png")//加载资源文件
.deploy();
}
/**
* 部署流程定义,以ZIP文件格式加入文件
*/
public void testDeployedDefiByZip(){
//获取ZIP文件的输入流
InputStream in=this.getClass().getClassLoader().getResourceAsStream("diagrams/helloWorld.zip");
ZipInputStream zipIn=new ZipInputStream(in);
Deployment deployment=processEngine.getRepositoryService()
.createDeployment()
.name("hellowrld流程定义升级版")//部署名称
.addZipInputStream(zipIn)//加载ZIP文件
.deploy();
System.out.println(deployment.getName());
}
/**
* 查询流程定义
*/
public void queryProceDef(){
List<ProcessDefinition> list=processEngine.getRepositoryService().createProcessDefinitionQuery()
// .processDefinitionCategory("")//根据类别查询
// .processDefinitionCategoryLike("")//根据类别模糊查询
// .processDefinitionCategoryNotEquals("")//类别不等于
// .processDefinitionId("")//流程定义的id查询
// .processDefinitionIds(null)//一个id的set集合查询(根据多个id擦查询)
// .processDefinitionKey("")//根据流程定义的key查询
// .processDefinitionKeyLike("")//根据流程定义的key进行模糊查询
// .processDefinitionName()//根据流程定义的名称查询
// .processDefinitionNameLike("")//根据流程定义的名称进行模糊查询
// .deploymentId("")//根据部署的id查询
// .processDefinitionResourceName("")//根据流程定义的资源名称查询
// .processDefinitionResourceNameLike("")//根据流程定义的资源名称进行模糊查询
//下面的是排序
.orderByDeploymentId().asc()//根据部署的id排序,升序
// .orderByProcessDefinitionCategory().desc()//根据种类排序,降序
// .orderByProcessDefinitionId()//根据流程定义id排序
//还有更多
.list();//获取全部
for (ProcessDefinition processDefinition : list) {
System.out.println(processDefinition.getDeploymentId());
}
}
/**
* 启动流程定义
*/
public void startProcessDef(){
ProcessInstance processInstance=processEngine.getRuntimeService().startProcessInstanceById("helloWorld:3:15004");
System.out.println(processInstance.getId());
System.out.println(processInstance.getActivityId());
}
/**
* 删除非运行中的部署流程
*/
public void testDeleteNativeProcDef(){
processEngine.getRepositoryService().deleteDeployment("12501");
System.out.println("删除成功");
}
/**
* 删除正在运行中的流程定义,需要用到级联删除,即把所用该流程相关的信息全部删除,否则报错
*/
public void testDeleteActiveProcDef(){
processEngine.getRepositoryService().deleteDeployment("15001",true);
System.out.println("删除成功");
}
/**
* 获取流程的图片
* @throws IOException
*/
public void testGetProcDefImg() throws IOException{
String deploymentId="17501";
//从act_ge_bytearray表中获取数据,该表存放的是我们的流程定义文件和图片文件的数据
List<String> resources=processEngine.getRepositoryService().getDeploymentResourceNames(deploymentId);
//获取图片的名称
String imgName="";
if(resources!=null&&resources.size()>0){
for (String string : resources) {
if(string.indexOf("png")>=0){
imgName=string;
}
}
}
System.out.println(imgName);
if(imgName!=null){
File f=new File("E:/",imgName);
InputStream in=processEngine.getRepositoryService().getResourceAsStream(deploymentId, imgName);
FileUtils.copyInputStreamToFile(in, f);
}
}
}