部署流程
/**
* 部署流程使用zip 流程图的文件必须是xxxx.zip结尾
*/
@Test
public void deployProcess02() {
// 如果不加/代表从当前包里面找文件
InputStream inputStream = this.getClass().getResourceAsStream("/LeaveBill.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("部署成功,部署D:" + deploy.getId());
}
查询流程部署信息
@Test
public void queryProcess() {
RepositoryService repositoryService = proe.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("########################");
}
}
查询流程定义信息
/**
* 查询流程定义
*/
@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("##################");
}
}
}
删除流程定义
/**
* 根据流程部署id删除流程定义
* 删除流程定义
*/
@Test
public void deleteProcessDef() {
RepositoryService repositoryService = proe.getRepositoryService();
String deploymentId = "1";
// 根据流程部署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("删除成功");
}
查询流程图
@Test
public void queryProcessImage() {
RepositoryService repositoryService = proe.getRepositoryService();
String processDefinitionId= "hy:1:12504";
InputStream inputStream = repositoryService.getProcessDiagram(processDefinitionId);
File file = new File("d:/HelloWorld.png");
/*
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();
}
}
查询最新的流程定义
public void queryNewProcess() {
RepositoryService res = proe.getRepositoryService();
String processDefinitionKey ="hy";
List<ProcessDefinition> proList = res.createProcessDefinitionQuery()
.processDefinitionKey(processDefinitionKey)
.orderByProcessDefinitionVersion()
.asc()
.list();
}
/**
* 已知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);
}
}
}
}