From 652f5161a91475ae0c22a88eb06b36b240edb744 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期三, 15 一月 2025 17:40:31 +0800
Subject: [PATCH] update 适配 新版本将xml转为json

---
 ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/FlwDefinitionServiceImpl.java |   53 ++++++++++++++++++++++-------------------------------
 1 files changed, 22 insertions(+), 31 deletions(-)

diff --git a/ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/FlwDefinitionServiceImpl.java b/ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/FlwDefinitionServiceImpl.java
index baec223..a881ba6 100644
--- a/ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/FlwDefinitionServiceImpl.java
+++ b/ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/FlwDefinitionServiceImpl.java
@@ -3,22 +3,20 @@
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.convert.Convert;
+import cn.hutool.core.io.IoUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.dom4j.Document;
-import org.dom4j.io.OutputFormat;
-import org.dom4j.io.XMLWriter;
 import org.dromara.common.core.exception.ServiceException;
 import org.dromara.common.core.utils.StreamUtils;
 import org.dromara.common.core.utils.StringUtils;
+import org.dromara.common.json.utils.JsonUtils;
 import org.dromara.common.mybatis.core.page.PageQuery;
 import org.dromara.common.mybatis.core.page.TableDataInfo;
-import org.dromara.warm.flow.core.dto.FlowCombine;
-import org.dromara.warm.flow.core.entity.Definition;
+import org.dromara.warm.flow.core.dto.DefJson;
 import org.dromara.warm.flow.core.enums.NodeType;
 import org.dromara.warm.flow.core.enums.PublishStatus;
 import org.dromara.warm.flow.core.service.DefService;
@@ -30,6 +28,7 @@
 import org.dromara.warm.flow.orm.mapper.FlowHisTaskMapper;
 import org.dromara.warm.flow.orm.mapper.FlowNodeMapper;
 import org.dromara.warm.flow.orm.mapper.FlowSkipMapper;
+import org.dromara.workflow.common.ConditionalOnEnable;
 import org.dromara.workflow.common.constant.FlowConstant;
 import org.dromara.workflow.domain.FlowCategory;
 import org.dromara.workflow.domain.vo.FlowDefinitionVo;
@@ -41,6 +40,7 @@
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -52,6 +52,7 @@
  *
  * @author may
  */
+@ConditionalOnEnable
 @Slf4j
 @RequiredArgsConstructor
 @Service
@@ -60,9 +61,9 @@
     private final DefService defService;
     private final FlowDefinitionMapper flowDefinitionMapper;
     private final FlowHisTaskMapper flowHisTaskMapper;
-    private final FlwCategoryMapper flwCategoryMapper;
     private final FlowNodeMapper flowNodeMapper;
     private final FlowSkipMapper flowSkipMapper;
+    private final FlwCategoryMapper flwCategoryMapper;
 
     /**
      * 鏌ヨ娴佺▼瀹氫箟鍒楄〃
@@ -118,6 +119,7 @@
      * @param id 娴佺▼瀹氫箟id
      */
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public boolean publish(Long id) {
         List<FlowNode> flowNodes = flowNodeMapper.selectList(new LambdaQueryWrapper<FlowNode>().eq(FlowNode::getDefinitionId, id));
         List<String> errorMsg = new ArrayList<>();
@@ -141,16 +143,15 @@
      * @param file 鏂囦欢
      */
     @Override
-    public boolean importXml(MultipartFile file, String category) {
+    @Transactional(rollbackFor = Exception.class)
+    public boolean importJson(MultipartFile file, String category) {
         try {
-            FlowCombine combine = defService.readXml(file.getInputStream());
-            // 娴佺▼瀹氫箟
-            Definition definition = combine.getDefinition();
-            definition.setCategory(category);
-            defService.importFlow(combine);
-        } catch (Exception e) {
-            log.error("瀵煎叆娴佺▼瀹氫箟閿欒: {}", e.getMessage(), e);
-            throw new RuntimeException(e);
+            DefJson defJson = JsonUtils.parseObject(file.getBytes(), DefJson.class);
+            defJson.setCategory(category);
+            defService.importDef(defJson);
+        } catch (IOException e) {
+            log.error("璇诲彇鏂囦欢娴侀敊璇�: {}", e.getMessage(), e);
+            throw new IllegalStateException("鏂囦欢璇诲彇澶辫触锛岃妫�鏌ユ枃浠跺唴瀹�", e);
         }
         return true;
     }
@@ -164,24 +165,14 @@
      */
     @Override
     public void exportDef(Long id, HttpServletResponse response) throws IOException {
-        Document document = defService.exportXml(id);
-        // 璁剧疆鐢熸垚xml鐨勬牸寮�
-        OutputFormat of = OutputFormat.createPrettyPrint();
-        // 璁剧疆缂栫爜鏍煎紡
-        of.setEncoding("UTF-8");
-        of.setIndent(true);
-        of.setIndent("    ");
-        of.setNewlines(true);
-
-        // 鍒涘缓涓�涓獂ml鏂囨。缂栬緫鍣�
-        XMLWriter writer = new XMLWriter(response.getOutputStream(), of);
-        writer.setEscapeText(false);
+        byte[] data = defService.exportJson(id).getBytes(StandardCharsets.UTF_8);
+        // 璁剧疆鍝嶅簲澶村拰鍐呭绫诲瀷
         response.reset();
-        response.setCharacterEncoding("UTF-8");
-        response.setContentType("application/x-msdownload");
+        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
+        response.setContentType("application/text");
         response.setHeader("Content-Disposition", "attachment;");
-        writer.write(document);
-        writer.close();
+        response.addHeader("Content-Length", "" + data.length);
+        IoUtil.write(response.getOutputStream(), false, data);
     }
 
     /**

--
Gitblit v1.9.3