repositoryService 流程存储服务组件

1.流程文件的部署

act_re_deployment-->act_ge_bytearray(两条数据:流程图片的文件数据和流程的描述文件数据)--> act_re_procdef(流程定义表)
a. classpath资源
//创建DeploymentBuilder实例
DeploymentBuilder builder = repositoryService.createDeployment();
//添加classpath下的资源
builder.addClasspathResource("artifact/classpath.png");
//执行部署(写入到数据库中)
builder.deploy();
b.输入流资源
// 为DeploymentBuilder添加资源输入流
InputStream is1 = new FileInputStream(new File(
"resource/artifact/flow_inputstream1.png"));
builder.addInputStream("inputA", is1);
c.字符串资源
deployment.addString(resourceName, text)
d. zip压缩
//获取zip文件的输入流
FileInputStream fis = new FileInputStream(new File("resource/artifact/ZipInputStream.zip"));
//读取zip文件,创建ZipInputStream对象
ZipInputStream zi = new ZipInputStream(fis);
//添加Zip压缩包资源
builder.addZipInputStream(zi);
//执行部署(写入到数据库中)
builder.deploy();
e.修改部署名称(act_re_deployment表中的name字段)
builder.name("crazyit");
//执行部署(写入到数据库中)
builder.deploy();
f.过滤重复部署(只要改变,就会同时向两个表中添加一条数据)
//创建第一个部署对象
DeploymentBuilder builderA = repositoryService.createDeployment();
builderA.addClasspathResource("artifact/DuplicateFilter.txt");
builderA.name("DuplicateFilterA");
builderA.deploy();
//由于资源一样,并且调用了enableDuplicateFiltering方法,因此不会再写入到数据库中
DeploymentBuilder builderB = repositoryService.createDeployment();
builderB.addClasspathResource("artifact/DuplicateFilter.txt");
builderB.name("DuplicateFilterA");
builderB.enableDuplicateFiltering();
builderB.deploy();
//由于资源发生变化,即使调用了enableDuplicateFiltering方法,也会写到数据库中
DeploymentBuilder builderC = repositoryService.createDeployment();
builderC.addClasspathResource("artifact/DuplicateFilterB.txt");
builderC.name("DuplicateFilterA");
builderC. enableDuplicateFiltering();
builderC.deploy();

2.流程的终止和启动

// 调用suspendProcessDefinitionById中止流程定义
repositoryService.suspendProcessDefinitionById(def.getId());
// 调用activateProcessDefinitionById激活流程定义
repositoryService.activateProcessDefinitionById(def.getId());
// 调用suspendProcessDefinitionByKey中止流程定义
repositoryService.suspendProcessDefinitionByKey(def.getKey());
// 调用activateProcessDefinitionByKey激活流程定义
repositoryService.activateProcessDefinitionByKey(def.getKey());

3.流程用户权限

//部署流程描述文件
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/candidateUser.bpmn").deploy();
//查询流程定义实体
ProcessDefinition def = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 写入用户数据
creatUser(identityService, "user1", "angus", "young", "[email protected]", "123");
// 设置用户组与流程定义的关系(设置权限)
repositoryService.addCandidateStarterUser(def.getId(), "user1");

4.流程用户组权限

//部署流程描述文件
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/candidateGroup.bpmn").deploy();
//查询流程定义实体
ProcessDefinition def = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 写入用户组数据
createGroup(identityService, "group1", "Group A", "type A");
// 设置流程开始用户组权限
repositoryService.addCandidateStarterGroup(def.getId(), "group1");

5.查询

// 用户--> 流程定义
List defs = repositoryService.createProcessDefinitionQuery()
.startableByUser("user1").list();
// 流程定义--> 用户组数据
List groups = identityService.createGroupQuery().potentialStarter(def.getId()).list();
// 流程定义--> 用户数据
List users = identityService.createUserQuery().potentialStarter(def.getId()).list();
// 根据流程定义查询 全部的 IdentityLink(ACT_RU_IDENTITYLINK表) 数据
List links = repositoryService.getIdentityLinksForProcessDefinition(def.getId());

6.查询资源

// 查询资源文件名称集合
List names = repositoryService.getDeploymentResourceNames(dep.getId());

//deploymentId方法
Deployment depAQuery = repositoryService.createDeploymentQuery()
.deploymentId(depA.getId()).singleResult();
System.out.println("根据id查询:" + depAQuery.getName());
//deploymentName方法
Deployment depBQuery = repositoryService.createDeploymentQuery()
.deploymentName("b").singleResult();
System.out.println("查询名称为b:" + depBQuery.getName());
//deploymentNameLike, 模糊查询,结果集为2
List depCQuery = repositoryService.createDeploymentQuery()
.deploymentNameLike("%b%").list();

// 查询资源文件( 从资源中获取资源文件流)(资源id<——流程定义中有,第二个参数为act_ge_bytearray中的NAME_)
InputStream is = repositoryService.getResourceAsStream(dep.getId(),
"artifact/GetResource.txt");

// 部署一份txt文件( 查询流程文件)
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/getProcessModel.bpmn").deploy();
//查询流程定义实体
ProcessDefinition def = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 查询资源文件
InputStream is = repositoryService. getProcessModel(def.getId());

// 部署一份流程文件与相应的流程图文件( 查询流程图
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/getProcessDiagram.bpmn")
.addClasspathResource("bpmn/getProcessDiagram.png").deploy();
// 查询流程定义
ProcessDefinition def = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 查询资源文件
InputStream is = repositoryService. getProcessDiagram(def.getId());
// 将输入流转换为图片对象
BufferedImage image = ImageIO.read(is);
// 保存为图片文件
File file = new File("resource/artifact/result.png");
if (!file.exists()) file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
ImageIO.write(image, "png", fos);
fos.close();
is.close();

7.删除部署资源

// 查询流程定义
ProcessDefinition def = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 开始流程
engine.getRuntimeService().startProcessInstanceById(def.getId());
try {
// 删除部署数据失败,此时将会抛出异常,由于cascade为false
repositoryService.deleteDeployment(dep.getId());
} catch (Exception e) {
System.out.println("删除失败,流程开始,没有设置cascade为true");
}
System.out.println("============分隔线");
// 成功删除部署数据
repositoryService.deleteDeployment(dep.getId(), true);





















你可能感兴趣的:(activiti)