上文链接:https://www.cnblogs.com/wangshaoyun/p/16356115.html
查询所有流程模型
@Autowired
RepositoryService repositoryService;
@Test
public void modelList() {
//1,模型查询对象
ModelQuery query = repositoryService.createModelQuery();
List<Model> list = query.orderByCreateTime()
.desc()
.list();
list.forEach(item -> {
System.out.println("模型id:" + item.getId());
System.out.println("模型名称:" + item.getName());
System.out.println("模型描述:" + item.getMetaInfo());
System.out.println("模型标识key:" + item.getKey());
System.out.println("模型版本号:" + item.getVersion());
});
}
删除流程模型
@Autowired
RepositoryService repositoryService;
@Test
public void deleteModel() {
String modelId = "841ed641-b24e-11ed-bc00-38f3abe10e1d";
repositoryService.deleteModel(modelId);
System.out.println("删除成功!!");
}
导出下载zip压缩包
@Autowired
RepositoryService repositoryService;
@Autowired
ObjectMapper objectMapper;
@Test
public void exportZip() throws IOException {
//1,查询模型基本信息
String modelId = "1a5bbc00-b25f-11ed-8cfb-38f3abe10e1d";
Model model = repositoryService.getModel(modelId);
if (null != model) {
//2,查询流程定义模型的json字节码
byte[] bpmnJsonBytes = repositoryService.getModelEditorSource(modelId);
//2.1,将json字节码转换为xml字节码
byte[] xmlBytes = bpmnJsonXmlForBytes(bpmnJsonBytes);
if (null == xmlBytes) {
System.out.println("数据模型数据为空-请先设计流程定义模型,再导出");
} else {
String zipName = model.getName() + "." + model.getKey() + ".zip";
File file = new File("C:/study/activiti/" + zipName);
FileOutputStream fileOutputStream = new FileOutputStream(file);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
//将xml添加压缩包中(指定xml文件名:请假流程.bpmn20.xml)
zipOutputStream.putNextEntry(new ZipEntry(model.getName() + ".bpmn20.xml"));
zipOutputStream.write(xmlBytes);
//3,查询流程定义模型的图片字节码
byte[] pngBytes = repositoryService.getModelEditorSource(modelId);
if (null != pngBytes) {
//图片文件名
zipOutputStream.putNextEntry(new ZipEntry(model.getName() + "." + model.getKey() + ".png"));
zipOutputStream.write(pngBytes);
}
//4,以压缩包的方式导出流程定义模型文件
zipOutputStream.closeEntry();
zipOutputStream.close();
System.out.println("导出成功!!");
}
} else {
System.out.println("流程模型不存在!!");
}
}
private byte[] bpmnJsonXmlForBytes(byte[] jsonBytes) throws IOException {
if (null == jsonBytes) {
return null;
}
//json字节码转成 BpmnModel 对象
JsonNode jsonNode = objectMapper.readTree(jsonBytes);
BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(jsonNode);
if (bpmnModel.getProcesses().size() == 0) {
return null;
}
//BpmnModel 对象转为xml字节码
return new BpmnXMLConverter().convertToXML(bpmnModel);
}
导出下载xml文件
@Autowired
RepositoryService repositoryService;
@Autowired
ObjectMapper objectMapper;
@Test
public void exportXml() throws IOException {
//1,查询模型基本信息
String modelId = "1a5bbc00-b25f-11ed-8cfb-38f3abe10e1d";
byte[] bytes = repositoryService.getModelEditorSource(modelId);
String fileName = null;
ByteArrayInputStream inputStream = null;
if (null != bytes) {
//json字节码转成 BpmnModel 对象
JsonNode jsonNode = objectMapper.readTree(bytes);
BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(jsonNode);
if (bpmnModel.getProcesses().size() > 0) {
byte[] xmlBytes = new BpmnXMLConverter().convertToXML(bpmnModel);
inputStream = new ByteArrayInputStream(xmlBytes);
fileName = StringUtils.isBlank(bpmnModel.getMainProcess().getName())
? bpmnModel.getMainProcess().getName() : bpmnModel.getMainProcess().getId();
}
}
if (null == fileName) {
fileName = "模型数据为空,请先设计流程,再导出";
inputStream = new ByteArrayInputStream(fileName.getBytes(StandardCharsets.UTF_8));
}
FileOutputStream outputStream = new FileOutputStream(new File("C:/study/activiti/" + fileName + "bpmn20.xml"));
IOUtils.copy(inputStream, outputStream);
outputStream.close();
inputStream.close();
System.out.println("导出xml成功!");
}
通过模型数据部署流程定义
@Autowired
RepositoryService repositoryService;
@Autowired
ObjectMapper objectMapper;
@Test
public void deploy() throws IOException {
//查询流程定义json字节码
String id = "1a5bbc00-b25f-11ed-8cfb-38f3abe10e1d";
byte[] jsonBytes = repositoryService.getModelEditorSource(id);
if (null == jsonBytes) {
System.out.println("模型数据为null,请先设计流程模型,在进行部署");
return;
}
//将json字节码转为xml字节码,因为bpmn2.0规范中关于流程模型的描述是xml格式的,而activiti遵守了这个规范
byte[] xmlBytes = bpmnJsonXmlForBytes(jsonBytes);
if (null == xmlBytes) {
System.out.println("模型数据不符合要求,请先设计流程模型,在进行部署");
return;
}
//流程图片字节码
byte[] pngBytes = repositoryService.getModelEditorSourceExtra(id);
//获取模型
Model model = repositoryService.getModel(id);
//流程定义xml名称
String processName = model.getName() + "bpmn20.xml";
//流程定义png名称
String pngName = model.getName() + "." + model.getKey() + ".png";
//流程部署
Deployment deployment = repositoryService.createDeployment()
.name(model.getName()) //部署名称
.addString(processName, new String(xmlBytes, "utf-8")) //xml文件
.addBytes(pngName, pngBytes) //图片
.deploy();
//更新 部署id 到模型对象(将模型与部署数据绑定)
model.setDeploymentId(deployment.getId());
repositoryService.saveModel(model);
System.out.println("部署成功!");
}
private byte[] bpmnJsonXmlForBytes(byte[] jsonBytes) throws IOException {
if (null == jsonBytes) {
return null;
}
//json字节码转成 BpmnModel 对象
JsonNode jsonNode = objectMapper.readTree(jsonBytes);
BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(jsonNode);
if (bpmnModel.getProcesses().size() == 0) {
return null;
}
//BpmnModel 对象转为xml字节码
return new BpmnXMLConverter().convertToXML(bpmnModel);
}
通过XML和ZIP和BPMN文件部署流程定义
//通过zip部署流程定义
@Test
public void deployByZip() throws IOException {
File file = new File("C:/study/activiti/请假流程模型.leave.zip");
//创建输入流
FileInputStream in = new FileInputStream(file);
//读取zip资源压缩包,转成输入流
ZipInputStream zipInputStream = new ZipInputStream(in);
Deployment deploy = repositoryService.createDeployment()
.addZipInputStream(zipInputStream)
.name("请假申请流程-压缩包")
.deploy();
System.out.println("部署ID: " + deploy.getId());
System.out.println("部署名称: " + deploy.getName());
}
//通过文件部署流程定义
@Test
public void deployByName() throws IOException {
File file = new File("C:/study/activiti/请假流程模型.bpmn20.xml");
//创建输入流
FileInputStream in = new FileInputStream(file);
//资源名称
String fileName = file.getName();
//调用相关api方法进行部署
Deployment deploy = repositoryService.createDeployment()
.addInputStream(fileName, in)
.name("请假申请流程-文件名")
.deploy();
System.out.println("部署ID: " + deploy.getId());
System.out.println("部署名称: " + deploy.getName());
}
根据部署id删除流程定义
@Test
public void deleteDeployModel() {
try {
String id = "92437f59-b671-11ed-8ef9-38f3abe10e1d";
//默认不是级联操作,如果有正在执行的流程实例,则删除时会抛出异常,并且不会删除历史表
repositoryService.deleteDeployment(id);
//or
//如果为true则是级联操作,如果流程定义启动了对应的流程实例,也可以进行删除,并且会删除历史数据
repositoryService.deleteDeployment(id, true);
System.out.println("删除流程定义数据成功");
} catch (Exception e) {
e.printStackTrace();
}
}