Activity 流程模型导入导出-activity流程模型导入导出
环境版本
springboot: 2.5.6
activity: 5.22.0
导出
@RequestMapping("/exportModelXml")
public void exportModelXml(HttpServletResponse response, @RequestParam String modelId) throws IOException {
Model model = repositoryService.getModel(modelId);
if (model == null) {
throw new TipException("流程模型不存在");
}
try (ServletOutputStream outputStream = response.getOutputStream()) {
BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
JsonNode editorNode = new ObjectMapper().readTree(repositoryService.getModelEditorSource(model.getId()));
BpmnModel bpmnModel = jsonConverter.convertToBpmnModel(editorNode);
BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
byte[] bpmnBytes = xmlConverter.convertToXML(bpmnModel);
String name = StringUtils.trim(bpmnModel.getMainProcess().getName());
String filename = name + ".bpmn20.xml";
response.reset();
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
String showFileName = URLEncoder.encode(filename, "UTF-8");
response.setHeader("Content-Disposition", "attachment;fileName=" + showFileName);
outputStream.write(bpmnBytes);
outputStream.flush();
}
}
导入
@RequestMapping("/importModelXml")
public ResultDto createModelByXmlFile(@RequestParam MultipartFile file,
@RequestParam String name,
@RequestParam String description,
@RequestParam String key) throws Exception {
InputStreamSource inputStreamSource = new InputStreamSource(file.getInputStream());
// 解析xml文件
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(inputStreamSource, false, true);
if (name.length() > 50) {
throw new TipException("流程名称不能超过50个字符");
}
if (key.length() > 50) {
throw new TipException("流程标识不能超过50个字符");
}
ModelQuery modelQuery = repositoryService.createModelQuery();
if (StrUtil.isNotBlank(key)) {
modelQuery.modelKey(key);
}
int total = modelQuery.list().size();
if (total > 0) {
throw new TipException("流程标识不能重复");
}
try {
// 唯一标识
Model model = repositoryService.newModel();
model.setKey(key);
// 名称
model.setName(name);
// 分类
// model.setCategory(key);
// 版本号
model.setVersion(1);
ObjectNode modelObjectNode = new ObjectMapper().createObjectNode();
modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, name);
modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, model.getVersion());
modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
// 相关信息
model.setMetaInfo(modelObjectNode.toString());
//保存模型,存储数据到表:act_re_model
repositoryService.saveModel(model);
BpmnJsonConverter converter = new BpmnJsonConverter();
ObjectNode modelNode = converter.convertToJson(bpmnModel);
modelNode.put("id", "canvas");
modelNode.put("resourceId", "canvas");
ObjectNode stencilSetNode = objectMapper.createObjectNode();
stencilSetNode.put("namespace",
"http://b3mn.org/stencilset/bpmn2.0#");
modelNode.put("stencilset", stencilSetNode);
//更新资源信息到model
repositoryService.addModelEditorSource(model.getId(), modelNode.toString().getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
return returnFail("模型创建失败");
}
return returnSuccess();
}