Activity 流程模型匯入匯出-activity流程模型匯入匯出

小白不爱發表於2024-03-08

環境版本

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

相關文章