Activiti流程定义模型管理
上文链接: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成功!");
}
标签:定义,Activiti,流程,System,println,new,repositoryService,模型,out
From: https://www.cnblogs.com/wangshaoyun/p/17150560.html