侧边栏壁纸
博主头像
峰峰火火博主等级

一条咸鱼罢了

  • 累计撰写 124 篇文章
  • 累计创建 90 个标签
  • 累计收到 59 条评论

目 录CONTENT

文章目录

Activity 流程模型导入导出

峰峰火火
2023-01-12 / 0 评论 / 0 点赞 / 808 阅读 / 414 字 / 正在检测是否收录...
温馨提示:
若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

环境版本

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();
    }

0

评论区