public class ProcessDefinitionTest
{
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**
* 部署流程定义
*
*/
@Test
public void deployProcessDefintion_classPath()
{
Deployment deployment = processEngine.getRepositoryService() // 与流程定义和部署相关的service
.createDeployment() // 创建一个部署对象
.name("第一个流程")
.addClasspathResource("diagrams/hello.bpmn") // 从classPath中加载资源一次只能加载一个文件
.addClasspathResource("diagrams/hello.png") // 从classPath中加载资源一次只能加载一个文件
.deploy(); // 完成部署
System.out.println("部署ID" + deployment.getId()); // 1
System.out.println("部署名称" + deployment.getName()); // 第一个流程
}
/**
* 部署流程定义
*
*/
@Test
public void deployProcessDefintion_zip()
{
InputStream in = this.getClass().getClassLoader().getResourceAsStream("diagrams/hello.zip");
ZipInputStream zipInputStream = new ZipInputStream(in);
Deployment deployment = processEngine.getRepositoryService() // 与流程定义和部署相关的service
.createDeployment() // 创建一个部署对象
.name("第一个流程")
.addZipInputStream(zipInputStream) // 使用zip输入流完成部署
.deploy(); // 完成部署
System.out.println("部署ID" + deployment.getId()); // 1
System.out.println("部署名称" + deployment.getName()); // 第一个流程
}
/**
* 查询流程定义
*
*/
@Test
public void queryProcessDefinition()
{
List<ProcessDefinition> list = processEngine.getRepositoryService()
.createProcessDefinitionQuery()//创建一个流程定义查询
/**查询条件**/
// .deploymentId(deploymentId)//部署对象id查询
// .processDefinitionId(processDefinitionId)// 流程定义id查询
// .processDefinitionKey(processDefinitionKey)// 流程定义key查询
// .processDefinitionNameLike(processDefinitionNameLike) // 流程名称模糊查询
/**排序***/
// .orderByProcessDefinitionVersion().desc()
.orderByProcessDefinitionName().asc()
/**返回结果集**/
.list(); //返回一个集合
// .singleResult() // 返回唯一结果集
// .count() // 返回结果集数量
// .listPage(firstResult, maxResults) // 分页查询
if (list != null && !list.isEmpty())
{
for (ProcessDefinition processDefinition : list)
{
System.out.println(processDefinition.getId());
System.out.println(processDefinition.getDeploymentId());
System.out.println(processDefinition.getKey());//对应bpmn文件中的id
System.out.println(processDefinition.getName());//对应bpmn文件中的name
System.out.println(processDefinition.getResourceName());
System.out.println(processDefinition.getDiagramResourceName());
System.out.println(processDefinition.getVersion());
System.out.println(processDefinition.getTenantId());
System.out.println("---------------------------------");
}
}
}
/**
* 删除流程定义
*
*/
@Test
public void deleteProcessDefinition()
{
String deploymentId = "1";
processEngine.getRepositoryService()
// 如果已启动流程报异常
// .deleteDeployment(deploymentId); //使用部署id删除
// 级联删除,删除已经启动的流程
.deleteDeployment(deploymentId, true);
System.out.println("删除成功:" + deploymentId);
}
/**
* 获取图片
*
* @throws IOException
*/
@Test
public void viewImage() throws IOException
{
String deploymentId = "15001";
String imageName = "";
List<String> deploymentResourceNames = processEngine.getRepositoryService()
.getDeploymentResourceNames(deploymentId);
for (String name : deploymentResourceNames)
{
if (name.endsWith(".png"))
{
imageName = name;
break;
}
}
InputStream in = processEngine.getRepositoryService()
.getResourceAsStream(deploymentId, imageName);
File file = new File("D:\\" + imageName);
FileUtils.copyInputStreamToFile(in, file);
}
/**
* 查询所有最新版本流程定义
*/
@Test
public void queryAllLastVersions()
{
List<ProcessDefinition> list = processEngine.getRepositoryService()
.createProcessDefinitionQuery()
.orderByProcessDefinitionVersion().asc()
.list();
Map<String, ProcessDefinition> map = new LinkedHashMap<String, ProcessDefinition>();
for (ProcessDefinition processDefinition : list)
{
map.put(processDefinition.getKey(), processDefinition);
}
for (ProcessDefinition processDefinition : map.values())
{
System.out.println(processDefinition.getId());
System.out.println(processDefinition.getDeploymentId());
System.out.println(processDefinition.getKey());//对应bpmn文件中的id
System.out.println(processDefinition.getName());//对应bpmn文件中的name
System.out.println(processDefinition.getResourceName());
System.out.println(processDefinition.getDiagramResourceName());
System.out.println(processDefinition.getVersion());
System.out.println(processDefinition.getTenantId());
System.out.println("---------------------------------");
}
}
/**
* 根据key删除所有版本流程定义
*
*/
@Test
public void deleteProcessDefinitionByKey()
{
String processDefinitionKey = "hello";
List<ProcessDefinition> list = processEngine.getRepositoryService()
.createProcessDefinitionQuery()
.processDefinitionKey(processDefinitionKey)
.list();
for (ProcessDefinition processDefinition : list)
{
processEngine.getRepositoryService()
.deleteDeployment(processDefinition.getDeploymentId(), true);
System.out.println("删除" + processDefinition.getDeploymentId());
}
System.out.println("删除成功");
}
}
Deployment 部署对象
1、一次部署的多个文件的信息。对于不需要的流程可以删除和修改。
2、对应的表:
act_re_deployment:部署对象表
act_re_procdef:流程定义表
act_ge_bytearray:资源文件表
act_ge_property:主键生成策略表
ProcessDefinition 流程定义
1、解析.bpmn后得到的流程定义规则的信息,工作流系统就是按照流程定义的规则执行的。