Browse Source

新增人员关系接口

zhouhao 7 years ago
parent
commit
93b1464c04

+ 41 - 0
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-authorization/src/main/java/org/hswebframework/web/organizational/authorization/relation/Relation.java

@@ -0,0 +1,41 @@
+package org.hswebframework.web.organizational.authorization.relation;
+
+import java.io.Serializable;
+
+/**
+ * TODO 完成注释
+ *
+ * @author zhouhao
+ */
+public interface Relation extends Serializable {
+    String getType();
+
+    String getRelation();
+
+    String getTarget();
+
+    String getName();
+
+    Object getTargetObject();
+
+    Direction getDirection();
+
+    default boolean matchDirection(Direction direction) {
+        if (getDirection() == Direction.ALL) return true;
+        return getDirection() == direction;
+    }
+
+    enum Direction {
+        POSITIVE, REVERSE, ALL;
+
+        public static Direction fromString(String direction) {
+            if (direction == null) return null;
+            for (Direction dir : values()) {
+                if (dir.toString().startsWith(direction.toUpperCase()))
+                    return dir;
+            }
+            return null;
+        }
+    }
+
+}

+ 73 - 0
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-authorization/src/main/java/org/hswebframework/web/organizational/authorization/relation/Relations.java

@@ -0,0 +1,73 @@
+package org.hswebframework.web.organizational.authorization.relation;
+
+import java.io.Serializable;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * 人员关系信息
+ *
+ * @author zhouhao
+ * @since 3.0
+ */
+public interface Relations extends Serializable {
+
+    /**
+     * 判断人员与目标是否存在某个关系
+     * <pre>
+     *     //判断是否是人员:张三的leader关系
+     *     relations.has("leader","person","张三",POSITIVE);
+     * </pre>
+     * <pre>
+     *     //判断人员:张三是否是本人的leader关系
+     *     relations.has("leader","person","张三",REVERSE);
+     * </pre>
+     * <pre>
+     *     //判断人员:张三和本人是否相互为friend关系
+     *     relations.has("friend","person","张三",ALL);
+     * </pre>
+     *
+     * @param relation 关系
+     * @param type     关系类型
+     * @param to       目标
+     * @return 是否存在关系
+     */
+    default boolean has(String relation, String type, String to, Relation.Direction direction) {
+        return getAllRelations().stream().anyMatch(rel ->
+                rel.getRelation().equals(relation)
+                        && rel.getType().equals(type)
+                        && rel.getTarget().equals(to)
+                        && rel.matchDirection(direction));
+    }
+
+    /**
+     * @see this#has(String, String, String, Relation.Direction)
+     */
+    default boolean has(String relation, String type, String target, String direction) {
+        return has(relation, type, target, Relation.Direction.fromString(direction));
+    }
+
+    /**
+     * use {@link Relation.Direction#POSITIVE}
+     *
+     * @see this#has(String, String, String, Relation.Direction)
+     */
+    default boolean has(String relation, String type, String target) {
+        return has(relation, type, target, Relation.Direction.POSITIVE);
+    }
+
+    
+    default List<Relation> getRelations(String relation, String type) {
+        return getAllRelations().stream()
+                .filter(rel -> rel.getRelation().equals(relation) && rel.getType().equals(type))
+                .collect(Collectors.toList());
+    }
+
+    default List<Relation> getRelations(String relation, Relation.Direction direction) {
+        return getAllRelations().stream()
+                .filter(rel -> rel.getRelation().equals(relation) && rel.matchDirection(direction))
+                .collect(Collectors.toList());
+    }
+
+    List<Relation> getAllRelations();
+}