Skip to content

Commit

Permalink
优化关系
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Jul 26, 2017
1 parent 93b1464 commit afafa9b
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hswebframework.web.organizational.authorization;

import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.organizational.authorization.relation.Relations;

import java.io.Serializable;
import java.util.List;
Expand Down Expand Up @@ -33,6 +34,12 @@ static Optional<PersonnelAuthorization> current() {
*/
Personnel getPersonnel();

/**
*
* @return 人员关系信息
*/
Relations getPersonRelations();

/**
* @return 人员所在行政区域ID, 只返回根节点, 永远不会返回{@code null}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,120 @@
import java.io.Serializable;

/**
* TODO 完成注释
* 关系,用于获取人员等关系信息
*
* @author zhouhao
* @see Relations
* @since 3.0
*/
public interface Relation extends Serializable {

/**
* @return 关系类型,如:person,department
*/
String getType();

/**
* @return 关系,如: leader,member
*/
String getRelation();

/**
* @return 关系目标表识(和谁建立关系),通常为目标的id
*/
String getTarget();

String getName();

/**
* @return 关系目标对象,用于获取建立关系对象完整信息,返回值的类型可能随着{@link this#getType()}的不同而变化
*/
Object getTargetObject();

/**
* @return 关系名称,与{@link this#getType()} 对应,如: 经理,员工
*/
String getName();

/**
* @return 关系的方向
* @see Direction
*/
Direction getDirection();

/**
* 匹配方向,如果当前的方向为ALl,则全部返回true
* <pre>
* direction=ALL;
* matchDirection(POSITIVE) -> true
* matchDirection(REVERSE) -> true
* matchDirection(ALL) -> true
* </pre>
* <p>
* <pre>
* direction=POSITIVE;
* matchDirection(POSITIVE) -> true
* matchDirection(REVERSE) -> false
* matchDirection(ALL) -> false
* </pre>
*
* @param direction 要匹配的方向枚举
* @return 匹配结果
*/
default boolean matchDirection(Direction direction) {
if (getDirection() == Direction.ALL) return true;
return getDirection() == direction;
return getDirection() == Direction.ALL || getDirection() == direction;
}

/**
* 匹配方向,如果当前的方向为ALl,则全部返回true
* <pre>
* direction=ALL;
* matchDirection("A") -> true
* matchDirection("ALL") -> true
* matchDirection("R") -> true
* matchDirection("P") -> true
* matchDirection("O") -> false
* </pre>
* <p>
* <pre>
* direction=POSITIVE;
* matchDirection("P") -> true
* matchDirection("POS") -> true
* matchDirection("A") -> false
* matchDirection("O") -> false
* </pre>
*
* @param direction 要匹配的方向字符
* @return 匹配结果
* @see Direction#fromString(String)
*/
default boolean matchDirection(String direction) {
return matchDirection(Direction.fromString(direction));
}

/**
* 关系方向,例如,我和张三建立关系,POSITIVE:我是张三的经理 ,REVERSE张三是我的经理
*
* @author zhouhao
* @since 3.0
*/
enum Direction {
POSITIVE, REVERSE, ALL;
/**
* 正向关系
*/
POSITIVE,
/**
* 反向关系
*/
REVERSE,
/**
* 双向关系
*/
ALL;

public static Direction fromString(String direction) {
if (direction == null) return null;
for (Direction dir : values()) {
if (dir.toString().startsWith(direction.toUpperCase()))
//以名称开头则认为是同一个方向
if (dir.name().startsWith(direction.toUpperCase()))
return dir;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

import java.io.Serializable;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* 人员关系信息
* 关系信息,用于获取,判断组织机构中的关系信息
*
* @author zhouhao
* @see Relation
* @since 3.0
*/
public interface Relations extends Serializable {

/**
* 判断人员与目标是否存在某个关系
* 判断与目标是否存在某个关系
* <pre>
* //判断是否是人员:张三的leader关系
* //判断是否是张三的leader关系
* relations.has("leader","person","张三",POSITIVE);
* </pre>
* <pre>
* //判断人员:张三是否是本人的leader关系
* //判断张三是否是当前的leader关系
* relations.has("leader","person","张三",REVERSE);
* </pre>
* <pre>
Expand All @@ -43,31 +45,92 @@ default boolean has(String relation, String type, String to, Relation.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));
default boolean has(String relation, String type, String to, String direction) {
return has(relation, type, to, 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 boolean has(String relation, String type, String to) {
return has(relation, type, to, 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());
/**
* 获取指定关系的全部关系信息
*
* @param relation 关系标识
* @return 关系信息集合,如果关系不存在,返回空集合
* @see this#findRelations(Predicate)
*/
default List<Relation> getRelations(String relation) {
return findRelations(rel -> rel.getRelation().equals(relation));
}

/**
* 获取指定关系和方向的关系信息
*
* @param relation 关系标识,例如: leader
* @param direction 关系方向
* @return 关系信息集合,如果关系不存在,返回空集合
*/
default List<Relation> getRelations(String relation, Relation.Direction direction) {
return getAllRelations().stream()
.filter(rel -> rel.getRelation().equals(relation) && rel.matchDirection(direction))
.collect(Collectors.toList());
return findRelations(rel -> rel.getRelation().equals(relation) && rel.matchDirection(direction));
}

/**
* 获取指定关系和类型的全部关系信息
*
* @param relation 关系标识,例如: leader
* @param type 关系类型,例如:person
* @return 关系信息集合,如果关系不存在,返回空集合
* @see this#findRelations(Predicate)
*/
default List<Relation> getRelations(String relation, String type) {
return findRelations(rel -> rel.getRelation().equals(relation) && rel.getType().equals(type));
}

/**
* 获取指定关系和类型以及方向全部关系信息
*
* @param relation 关系标识,例如: leader
* @param type 关系类型,例如:person
* @param direction 关系方向
* @return 关系信息集合,如果关系不存在,返回空集合
* @see this#findRelations(Predicate)
*/
default List<Relation> getRelations(String relation, String type, Relation.Direction direction) {
return findRelations(rel ->
rel.getRelation().equals(relation)
&& rel.getType().equals(type)
&& rel.matchDirection(direction));
}

/**
* @see this#getRelations(String, String, Relation.Direction)
*/
default List<Relation> getRelations(String relation, String type,String direction) {
return getRelations(relation,type, Relation.Direction.fromString(direction));
}

/**
* 查找关系
* <pre>
* findRelations(rel->rel.getType().equals("person"))
* </pre>
*
* @param predicate 查找的判断逻辑
* @return 满足条件的关系信息集合,如果全部不满足则返回空集合
*/
default List<Relation> findRelations(Predicate<Relation> predicate) {
return getAllRelations().stream().filter(predicate).collect(Collectors.toList());
}

/**
*
* @return 全部关系信息,如果一个也没有返回空集合
*/
List<Relation> getAllRelations();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.hswebframework.web.organizational.authorization.Personnel;
import org.hswebframework.web.organizational.authorization.PersonnelAuthorization;
import org.hswebframework.web.organizational.authorization.TreeNode;
import org.hswebframework.web.organizational.authorization.relation.Relations;

import java.util.Collections;
import java.util.Set;
Expand Down Expand Up @@ -38,6 +39,13 @@ public void setDistrictIds(Set<TreeNode<String>> districtIds) {
this.districtIds = districtIds;
}

@Override
public Relations getPersonRelations() {
// TODO: 2017/7/26
throw new UnsupportedOperationException("not support yet");

}

@Override
public Set<TreeNode<String>> getOrgIds() {
if (orgIds == null) orgIds = Collections.emptySet();
Expand Down

0 comments on commit afafa9b

Please sign in to comment.