Browse Source

更新图谱实体的属性

MarkHuang 4 years ago
parent
commit
7e9fac07d0

+ 39 - 0
src/main/java/com/diagbot/util/BeanMapUtils.java

@@ -0,0 +1,39 @@
+package com.diagbot.util;
+
+import org.springframework.cglib.beans.BeanMap;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @program: simple_tools
+ * @description: BeanMapUtils
+ * @author: ChenWenLong
+ * @create: 2019-12-29 14:14
+ **/
+public class BeanMapUtils {
+
+    /**
+     * 将对象属性转化为map结合
+     */
+    public static <T> Map<String, Object> beanToMap(T bean) {
+        Map<String, Object> map = new HashMap<>();
+        if (bean != null) {
+            BeanMap beanMap = BeanMap.create(bean);
+            for (Object key : beanMap.keySet()) {
+                map.put(key+"", beanMap.get(key));
+            }
+        }
+        return map;
+    }
+
+    /**
+     * 将map集合中的数据转化为指定对象的同名属性中
+     */
+    public static <T> T mapToBean(Map<String, Object> map,Class<T> clazz) throws Exception {
+        T bean = clazz.newInstance();
+        BeanMap beanMap = BeanMap.create(bean);
+        beanMap.putAll(map);
+        return bean;
+    }
+}

+ 46 - 0
src/main/java/com/diagbot/util/GenericNode.java

@@ -0,0 +1,46 @@
+package com.diagbot.util;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class GenericNode<T> {
+
+    public List<Map<String, Object>> fillNodeTree(List<?> nodelist) {
+        List<Map<String, Object>> baseNodes = new ArrayList<>();
+        Map<String, Object> node;
+
+        for (Object nd : nodelist) {
+            node = BeanMapUtils.beanToMap(nd);
+
+            baseNodes.add(node);
+        }
+
+        return baseNodes;
+    }
+
+
+    public static List<Map<String, Object>> removeRelation(List<Map<String, Object>> nodes) {
+        List<String> rmkeys = new ArrayList<>();
+        try {
+            for (String key : nodes.get(0).keySet()) {
+                if (nodes.get(0).get(key) instanceof Set) {
+                    rmkeys.add(key);
+                }
+            }
+
+            for (Map<String, Object> node : nodes) {
+                for (String key : rmkeys) {
+                    node.remove(key);
+                }
+            }
+        }
+        catch (Exception ex) {
+            ex.printStackTrace();
+        }
+        finally {
+            return nodes;
+        }
+    }
+}