Browse Source

优化清空异常节点逻辑

xuezizhuo 1 year ago
parent
commit
48b657a0c6

+ 35 - 151
zkqy-common/src/main/java/com/zkqy/common/utils/bpm/XmlDataParserUtils.java

@@ -216,131 +216,72 @@ public class XmlDataParserUtils {
      * @param currentNodeId 当前节点ID
      * @return
      */
-    public static String clearExceptionTask(String xmlData,String currentNodeId) throws Exception{
+    public static String clearExceptionTask(String xmlData,String currentNodeId) {
         //将xml文件内容转成Document对象
         Document document = XmlUtil.parseXml(xmlData);
-
         //得到当前所有节点ID
         Map<String, Object> allNodeKey = getAllNodeKey(xmlData);
         List<String> list = (List<String>) allNodeKey.get("nodeList");
         //截取当前节点后的所有ID
         list = new ArrayList<>(list.subList(list.indexOf(currentNodeId),list.size()));
-
         //获取所有异常节点,筛选出正常节点和异常节点
         List<String> exceptionList = new ArrayList<>();
         for (int i = 1; i < list.size(); i++){
-            Element node = getNodeById(document, list.get(i));
+            Element node = getElement(document, list.get(i),"id");
             String nodeTagName = node.getTagName();
             if (nodeTagName.equals("bpmn:exceptionTask")){
                 exceptionList.add(list.get(i));
                 list.remove(i);
             }
         }
-        System.err.println(exceptionList.toString());
-
-        // 循环正常节点,修改下一个指向
+        // 循环正常节点,修改下一个节点指向
         for (int i = 0; i < list.size() - 1; i++ ) {
-
-            //查看下一节点标签是否异常节点,如果是异常节点,先找到当前节点的序列线,记录下来
-            Element nextNode = getNodeById(document, list.get(i + 1));
-            String nodeTagName = nextNode.getTagName();
-//            if (nodeTagName.equals("bpmn:userTask")) {
-//                continue;
-//            } else {
-                // 得到上一个正常节点得序列流元素,修改targetRef= 下一个节点ID
-                Element currentSequenceFlow = getNodeById1(document,list.get(i));
-                currentSequenceFlow.setAttribute("targetRef",list.get(i+1));
-                //修改下一个节点入口incoming=第一个节点序列流ID
-                Element elementOutgoing = XmlUtil.getElement(nextNode,"bpmn:incoming");
-                elementOutgoing.setTextContent(currentSequenceFlow.getAttributes().getNamedItem("id").getNodeValue());
-//            }
-
+            //获取下一节点
+            Element nextNode = getElement(document, list.get(i + 1),"id");
+            //得到上一个正常节点得序列流元素,修改targetRef指向下一个节点ID
+            Element currentSequenceFlow = getElement(document,list.get(i),"sourceRef");
+            currentSequenceFlow.setAttribute("targetRef",list.get(i+1));
+            //修改下一个节点入口incoming指向上一个节点序列流ID
+            Element elementOutgoing = XmlUtil.getElement(nextNode,"bpmn:incoming");
+            elementOutgoing.setTextContent(currentSequenceFlow.getAttributes().getNamedItem("id").getNodeValue());
         }
-        //删除标签
+        //删除异常节点及异常节点序列线
         for (int i = 0; i < exceptionList.size(); i++) {
-            System.err.println(exceptionList.get(i));
-            Element node = getNodeById(document, exceptionList.get(i));
-            System.err.println(node.getTagName());
-//            System.err.println(node.getAttributes().getNamedItem("id").getNodeValue());
+            //获取异常节点-删除
+            Element node = getElement(document, exceptionList.get(i),"id");
             node.getParentNode().removeChild(node);
-
-            Element nodeXian = getNodeById1(document, exceptionList.get(i));
-            System.err.println(nodeXian.getTagName());
-            System.err.println(nodeXian.getAttributes().getNamedItem("id").getNodeValue());
+            //获取序列线-删除
+            Element nodeXian = getElement(document, exceptionList.get(i),"sourceRef");
             nodeXian.getParentNode().removeChild(nodeXian);
         }
-
-//将document对象转成字符串
-        TransformerFactory transformerFactory = TransformerFactory.newInstance();
-        Transformer transformer = transformerFactory.newTransformer();
-        DOMSource source = new DOMSource(document);
-        StreamResult result = new StreamResult(new StringWriter()); // 使用StringWriter来将结果输出为字符串
-        transformer.transform(source, result);
-        String data = result.getWriter().toString();
-
-//        for (int i = 1; i < list.size(); i++){
-//            //查看下一节点标签是否异常节点,如果是异常节点,先找到当前节点的序列线,记录下来
-//            String nodeType = getNodeById(xmlData, list.get(i));
-//
-//            if(nodeType.equals("bpmn:userTask")){
-//                continue;
-//            } else {
-//            // 得到上一个正常节点得序列流ID
-//            String currentSequenceFlow = getNodeById1(xmlData,list.get(i-1));
-//
-//
-//            }
-//        }
-        return data;
-    }
-
-    private static Element getNodeById(Document document,String nodeId){
-//        Document document= XmlUtil.parseXml(xmlData);
-
+        //将document对象转成字符串
+        String data = "";
         try {
-            // 创建XPath表达式来查找所有id属性为nodeId的元素
-//            String expression = "//*[local-name()='userTask' and namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@id='Activity_db1ad3cc-30d2-44c8-9371-647a9bcbb7e5']";
-            String expression = "//*[namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@id='"+nodeId+"']";
-            XPath xpath = XPathFactory.newInstance().newXPath();
-            NodeList nodeList = (NodeList) xpath.compile(expression).evaluate(document, XPathConstants.NODESET);
-
-            // 遍历节点列表并获取第一个匹配的元素
-            Element userTaskElement = null;
-            for (int i = 0; i < nodeList.getLength(); i++) {
-                Node node = nodeList.item(i);
-                if (node instanceof Element) {
-                    userTaskElement = (Element) node;
-                    break;
-                }
-            }
-
-            // 检查是否找到了匹配的元素
-            if (userTaskElement != null) {
-//                System.out.println("Found user task element: " + userTaskElement.getTagName());
-//                System.out.println("Name attribute: " + userTaskElement.getAttribute("name")); // 打印name属性值
-//                return userTaskElement.getTagName();
-                return userTaskElement;
-            } else {
-                System.out.println("No matching user task element found.");
-                return null;
-            }
-
+            TransformerFactory transformerFactory = TransformerFactory.newInstance();
+            Transformer transformer = transformerFactory.newTransformer();
+            DOMSource source = new DOMSource(document);
+            StreamResult result = new StreamResult(new StringWriter()); // 使用StringWriter来将结果输出为字符串
+            transformer.transform(source, result);
+            data =  result.getWriter().toString();
         } catch (Exception e) {
             e.printStackTrace();
         }
-        return null;
-    }
 
-    private static Element getNodeById1(Document document,String nodeId){
-//        Document document= XmlUtil.parseXml(xmlData);
+        return data;
+    }
 
+    /**
+     * 获取元素
+     * @param document
+     * @param nodeAttributeId 节点属性ID
+     * @param nodeAttributeName 节点属性名称
+     * @return
+     */
+    private static Element getElement(Document document,String nodeAttributeId,String nodeAttributeName){
         try {
-            // 创建XPath表达式来查找所有id属性为nodeId的元素
-//            String expression = "//*[local-name()='userTask' and namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@id='Activity_db1ad3cc-30d2-44c8-9371-647a9bcbb7e5']";
-            String expression = "//*[namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@sourceRef='"+nodeId+"']";
+            String expression = "//*[namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@"+nodeAttributeName+"='"+nodeAttributeId+"']";
             XPath xpath = XPathFactory.newInstance().newXPath();
             NodeList nodeList = (NodeList) xpath.compile(expression).evaluate(document, XPathConstants.NODESET);
-
             // 遍历节点列表并获取第一个匹配的元素
             Element userTaskElement = null;
             for (int i = 0; i < nodeList.getLength(); i++) {
@@ -350,74 +291,17 @@ public class XmlDataParserUtils {
                     break;
                 }
             }
-
             // 检查是否找到了匹配的元素
             if (userTaskElement != null) {
-//                System.out.println("Found user task element: " + userTaskElement.getTagName());
-//                System.out.println("Name attribute: " + userTaskElement.getAttribute("name")); // 打印name属性值
-//                return userTaskElement.getAttribute("id");
                 return userTaskElement;
             } else {
                 System.out.println("No matching user task element found.");
                 return null;
             }
-
         } catch (Exception e) {
             e.printStackTrace();
         }
         return null;
     }
 
-//    private static String getNodeById2(String xmlData,String nodeId){
-//        Document document= XmlUtil.parseXml(xmlData);
-//
-//        try {
-//            // 创建XPath表达式来查找所有id属性为nodeId的元素
-////            String expression = "//*[local-name()='userTask' and namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@id='Activity_db1ad3cc-30d2-44c8-9371-647a9bcbb7e5']";
-//            String expression = "//*[namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@targetRef='"+nodeId+"']";
-//            XPath xpath = XPathFactory.newInstance().newXPath();
-//            NodeList nodeList = (NodeList) xpath.compile(expression).evaluate(document, XPathConstants.NODESET);
-//
-//            // 遍历节点列表并获取第一个匹配的元素
-//            Element userTaskElement = null;
-//            for (int i = 0; i < nodeList.getLength(); i++) {
-//                Node node = nodeList.item(i);
-//                if (node instanceof Element) {
-//                    userTaskElement = (Element) node;
-//                    break;
-//                }
-//            }
-//
-//            // 检查是否找到了匹配的元素
-//            if (userTaskElement != null) {
-//                System.out.println("Found user task element: " + userTaskElement.getTagName());
-//                System.out.println("Name attribute: " + userTaskElement.getAttribute("name")); // 打印name属性值
-//                return userTaskElement.getAttribute("id");
-//            } else {
-//                System.out.println("No matching user task element found.");
-//                return null;
-//            }
-//
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//        }
-//        return null;
-//    }
-
-
-//    public Map<String,String> getAaa(String xmlData,List<String> list,Map<String,String> map){
-//        map.put("",getNodeById2(xmlData,list.get(0)));
-//        for (String s : list){
-////            map.put("rukou",);
-//            String str = getNodeById(xmlData,s);
-//            if(!str.equals("bpmn:exceptionTask")){
-//                break;
-//            }else {
-//
-//            }
-//        }
-//        return map;
-//    }
-
-
 }