From a42dda2bd3cc1cf8c20cc61e7ad9211eadecbaf3 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期四, 24 二月 2022 16:55:06 +0800
Subject: [PATCH] 规范数据库,添加必要约束,优化通道批量导入功能

---
 src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java |  230 +++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 188 insertions(+), 42 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
index e0d776c..94e0cd8 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
@@ -1,12 +1,8 @@
 package com.genersoft.iot.vmp.gb28181.utils;
 
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
 import org.dom4j.Attribute;
 import org.dom4j.Document;
 import org.dom4j.DocumentException;
@@ -14,14 +10,20 @@
 import org.dom4j.io.SAXReader;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.util.StringUtils;
+
+import javax.sip.RequestEvent;
+import javax.sip.message.Request;
+import java.io.ByteArrayInputStream;
+import java.io.StringReader;
+import java.util.*;
 
 /**
  * 鍩轰簬dom4j鐨勫伐鍏峰寘
  * 
  * 
  */
-public class XmlUtil
-{
+public class XmlUtil {
     /**
      * 鏃ュ織鏈嶅姟
      */
@@ -30,22 +32,18 @@
     /**
      * 瑙f瀽XML涓篋ocument瀵硅薄
      * 
-     * @param xml
-     *            琚В鏋愮殑XMl
+     * @param xml 琚В鏋愮殑XMl
+     * 
      * @return Document
      */
-    public static Element parseXml(String xml)
-    {
+    public static Element parseXml(String xml) {
         Document document = null;
         //
         StringReader sr = new StringReader(xml);
         SAXReader saxReader = new SAXReader();
-        try
-        {
+        try {
             document = saxReader.read(sr);
-        }
-        catch (DocumentException e)
-        {
+        } catch (DocumentException e) {
             LOG.error("瑙f瀽澶辫触", e);
         }
         return null == document ? null : document.getRootElement();
@@ -54,16 +52,12 @@
     /**
      * 鑾峰彇element瀵硅薄鐨則ext鐨勫��
      * 
-     * @param em
-     *            鑺傜偣鐨勫璞�
-     * @param tag
-     *            鑺傜偣鐨則ag
+     * @param em  鑺傜偣鐨勫璞�
+     * @param tag 鑺傜偣鐨則ag
      * @return 鑺傜偣
      */
-    public static String getText(Element em, String tag)
-    {
-        if (null == em)
-        {
+    public static String getText(Element em, String tag) {
+        if (null == em) {
             return null;
         }
         Element e = em.element(tag);
@@ -74,16 +68,12 @@
     /**
      * 閫掑綊瑙f瀽xml鑺傜偣锛岄�傜敤浜� 澶氳妭鐐规暟鎹�
      * 
-     * @param node
-     *            node
-     * @param nodeName
-     *            nodeName
+     * @param node     node
+     * @param nodeName nodeName
      * @return List<Map<String, Object>>
      */
-    public static List<Map<String, Object>> listNodes(Element node, String nodeName)
-    {
-        if (null == node)
-        {
+    public static List<Map<String, Object>> listNodes(Element node, String nodeName) {
+        if (null == node) {
             return null;
         }
         // 鍒濆鍖栬繑鍥�
@@ -93,12 +83,9 @@
 
         Map<String, Object> map = null;
         // 閬嶅巻灞炴�ц妭鐐�
-        for (Attribute attribute : list)
-        {
-            if (nodeName.equals(node.getName()))
-            {
-                if (null == map)
-                {
+        for (Attribute attribute : list) {
+            if (nodeName.equals(node.getName())) {
+                if (null == map) {
                     map = new HashMap<String, Object>();
                     listMap.add(map);
                 }
@@ -110,12 +97,171 @@
         // 閬嶅巻褰撳墠鑺傜偣涓嬬殑鎵�鏈夎妭鐐� 锛宯odeName 瑕佽В鏋愮殑鑺傜偣鍚嶇О
         // 浣跨敤閫掑綊
         Iterator<Element> iterator = node.elementIterator();
-        while (iterator.hasNext())
-        {
+        while (iterator.hasNext()) {
             Element e = iterator.next();
             listMap.addAll(listNodes(e, nodeName));
         }
         return listMap;
     }
 
+    /**
+     * xml杞琷son
+     * 
+     * @param element
+     * @param json
+     */
+    public static void node2Json(Element element, JSONObject json) {
+        // 濡傛灉鏄睘鎬�
+        for (Object o : element.attributes()) {
+            Attribute attr = (Attribute) o;
+            if (!StringUtils.isEmpty(attr.getValue())) {
+                json.put("@" + attr.getName(), attr.getValue());
+            }
+        }
+        List<Element> chdEl = element.elements();
+        if (chdEl.isEmpty() && !StringUtils.isEmpty(element.getText())) {// 濡傛灉娌℃湁瀛愬厓绱�,鍙湁涓�涓��
+            json.put(element.getName(), element.getText());
+        }
+
+        for (Element e : chdEl) {   // 鏈夊瓙鍏冪礌
+            if (!e.elements().isEmpty()) {  // 瀛愬厓绱犱篃鏈夊瓙鍏冪礌
+                JSONObject chdjson = new JSONObject();
+                node2Json(e, chdjson);
+                Object o = json.get(e.getName());
+                if (o != null) {
+                    JSONArray jsona = null;
+                    if (o instanceof JSONObject) {  // 濡傛灉姝ゅ厓绱犲凡瀛樺湪,鍒欒浆涓簀sonArray
+                        JSONObject jsono = (JSONObject) o;
+                        json.remove(e.getName());
+                        jsona = new JSONArray();
+                        jsona.add(jsono);
+                        jsona.add(chdjson);
+                    }
+                    if (o instanceof JSONArray) {
+                        jsona = (JSONArray) o;
+                        jsona.add(chdjson);
+                    }
+                    json.put(e.getName(), jsona);
+                } else {
+                    if (!chdjson.isEmpty()) {
+                        json.put(e.getName(), chdjson);
+                    }
+                }
+            } else { // 瀛愬厓绱犳病鏈夊瓙鍏冪礌
+                for (Object o : element.attributes()) {
+                    Attribute attr = (Attribute) o;
+                    if (!StringUtils.isEmpty(attr.getValue())) {
+                        json.put("@" + attr.getName(), attr.getValue());
+                    }
+                }
+                if (!e.getText().isEmpty()) {
+                    json.put(e.getName(), e.getText());
+                }
+            }
+        }
+    }
+    public static  Element getRootElement(RequestEvent evt) throws DocumentException {
+
+        return getRootElement(evt, "gb2312");
+    }
+
+    public static Element getRootElement(RequestEvent evt, String charset) throws DocumentException {
+        Request request = evt.getRequest();
+        return getRootElement(request.getRawContent(), charset);
+    }
+
+    public static Element getRootElement(byte[] content, String charset) throws DocumentException {
+        if (charset == null) {
+            charset = "gb2312";
+        }
+        SAXReader reader = new SAXReader();
+        reader.setEncoding(charset);
+        Document xml = reader.read(new ByteArrayInputStream(content));
+        return xml.getRootElement();
+    }
+
+    public static DeviceChannel channelContentHander(Element itemDevice){
+        Element channdelNameElement = itemDevice.element("Name");
+        String channelName = channdelNameElement != null ? channdelNameElement.getTextTrim().toString() : "";
+        Element statusElement = itemDevice.element("Status");
+        String status = statusElement != null ? statusElement.getTextTrim().toString() : "ON";
+        DeviceChannel deviceChannel = new DeviceChannel();
+        deviceChannel.setName(channelName);
+        Element channdelIdElement = itemDevice.element("DeviceID");
+        String channelId = channdelIdElement != null ? channdelIdElement.getTextTrim().toString() : "";
+        deviceChannel.setChannelId(channelId);
+        // ONLINE OFFLINE HIKVISION DS-7716N-E4 NVR鐨勫吋瀹规�у鐞�
+        if (status.equals("ON") || status.equals("On") || status.equals("ONLINE")) {
+            deviceChannel.setStatus(1);
+        }
+        if (status.equals("OFF") || status.equals("Off") || status.equals("OFFLINE")) {
+            deviceChannel.setStatus(0);
+        }
+
+        deviceChannel.setManufacture(XmlUtil.getText(itemDevice, "Manufacturer"));
+        deviceChannel.setModel(XmlUtil.getText(itemDevice, "Model"));
+        deviceChannel.setOwner(XmlUtil.getText(itemDevice, "Owner"));
+        deviceChannel.setCivilCode(XmlUtil.getText(itemDevice, "CivilCode"));
+        deviceChannel.setBlock(XmlUtil.getText(itemDevice, "Block"));
+        deviceChannel.setAddress(XmlUtil.getText(itemDevice, "Address"));
+        if (XmlUtil.getText(itemDevice, "Parental") == null
+                || XmlUtil.getText(itemDevice, "Parental") == "") {
+            deviceChannel.setParental(0);
+        } else {
+            deviceChannel.setParental(Integer.parseInt(XmlUtil.getText(itemDevice, "Parental")));
+        }
+        deviceChannel.setParentId(XmlUtil.getText(itemDevice, "ParentID"));
+        if (XmlUtil.getText(itemDevice, "SafetyWay") == null
+                || XmlUtil.getText(itemDevice, "SafetyWay") == "") {
+            deviceChannel.setSafetyWay(0);
+        } else {
+            deviceChannel.setSafetyWay(Integer.parseInt(XmlUtil.getText(itemDevice, "SafetyWay")));
+        }
+        if (XmlUtil.getText(itemDevice, "RegisterWay") == null
+                || XmlUtil.getText(itemDevice, "RegisterWay") == "") {
+            deviceChannel.setRegisterWay(1);
+        } else {
+            deviceChannel.setRegisterWay(Integer.parseInt(XmlUtil.getText(itemDevice, "RegisterWay")));
+        }
+        deviceChannel.setCertNum(XmlUtil.getText(itemDevice, "CertNum"));
+        if (XmlUtil.getText(itemDevice, "Certifiable") == null
+                || XmlUtil.getText(itemDevice, "Certifiable") == "") {
+            deviceChannel.setCertifiable(0);
+        } else {
+            deviceChannel.setCertifiable(Integer.parseInt(XmlUtil.getText(itemDevice, "Certifiable")));
+        }
+        if (XmlUtil.getText(itemDevice, "ErrCode") == null
+                || XmlUtil.getText(itemDevice, "ErrCode") == "") {
+            deviceChannel.setErrCode(0);
+        } else {
+            deviceChannel.setErrCode(Integer.parseInt(XmlUtil.getText(itemDevice, "ErrCode")));
+        }
+        deviceChannel.setEndTime(XmlUtil.getText(itemDevice, "EndTime"));
+        deviceChannel.setSecrecy(XmlUtil.getText(itemDevice, "Secrecy"));
+        deviceChannel.setIpAddress(XmlUtil.getText(itemDevice, "IPAddress"));
+        if (XmlUtil.getText(itemDevice, "Port") == null || XmlUtil.getText(itemDevice, "Port") == "") {
+            deviceChannel.setPort(0);
+        } else {
+            deviceChannel.setPort(Integer.parseInt(XmlUtil.getText(itemDevice, "Port")));
+        }
+        deviceChannel.setPassword(XmlUtil.getText(itemDevice, "Password"));
+        if (NumericUtil.isDouble(XmlUtil.getText(itemDevice, "Longitude"))) {
+            deviceChannel.setLongitude(Double.parseDouble(XmlUtil.getText(itemDevice, "Longitude")));
+        } else {
+            deviceChannel.setLongitude(0.00);
+        }
+        if (NumericUtil.isDouble(XmlUtil.getText(itemDevice, "Latitude"))) {
+            deviceChannel.setLatitude(Double.parseDouble(XmlUtil.getText(itemDevice, "Latitude")));
+        } else {
+            deviceChannel.setLatitude(0.00);
+        }
+        if (XmlUtil.getText(itemDevice, "PTZType") == null
+                || XmlUtil.getText(itemDevice, "PTZType") == "") {
+            deviceChannel.setPTZType(0);
+        } else {
+            deviceChannel.setPTZType(Integer.parseInt(XmlUtil.getText(itemDevice, "PTZType")));
+        }
+        deviceChannel.setHasAudio(true); // 榛樿鍚湁闊抽锛屾挱鏀炬椂鍐嶆鏌ユ槸鍚︽湁闊抽鍙婃槸鍚AC
+        return deviceChannel;
+    }
 }

--
Gitblit v1.8.0