group.rober.base.pilot.service.PilotService Maven / Gradle / Ivy
The newest version!
package group.rober.base.pilot.service;
import group.rober.base.pilot.model.*;
import group.rober.runtime.kit.BeanKit;
import group.rober.runtime.kit.JSONKit;
import group.rober.runtime.kit.StringKit;
import group.rober.runtime.lang.MapData;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.yaml.snakeyaml.Yaml;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
public class PilotService {
public StartNode getStartNode(String location) throws IOException {
//加载YAML资源
Yaml yaml = new Yaml();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource resource = resolver.getResource(location);
MapData root = yaml.loadAs(resource.getInputStream(), MapData.class);
System.out.println(JSONKit.toJsonString(root, true));
//拼装节点
//根节点
StartNode startNode = new StartNode();
BeanKit.copyProperties(root, startNode);
startNode.setChildren(null);
//菜单节点
List mapList = (List) root.get("children");
List menuNodes = new ArrayList(mapList.size());
mapList.stream()
.filter(mapItem -> "menu".equals(mapItem.get("type"))) //过滤掉根节点以下,type不是menu的节点
.forEach(mapItem -> {
//递归构建节点
MenuNode node = (MenuNode)buildMenuNodeWithRecursion(mapItem);
//放回容器
menuNodes.add(node);
});
startNode.setChildren(menuNodes);
//处理一级菜单及以后的节点
return startNode;
}
protected BaseNode buildMenuNodeWithRecursion(LinkedHashMap mapNode) {
BaseNode retNode = null;
String type = StringKit.nvl(mapNode.get("type"),"");
NodeType nodeType = NodeType.valueOf(type);
if(NodeType.menu == nodeType){
MenuNode menuNode = new MenuNode();
BeanKit.copyProperties(mapNode, menuNode);
menuNode.setChildren(null);
//处理子节点
List childMapList = (List) mapNode.get("children");
if(childMapList==null||childMapList.size()==0)return menuNode;
List childBeanNodeList = new ArrayList(childMapList.size());
childMapList.forEach(childMapNode->{
BaseNode node = buildMenuNodeWithRecursion(childMapNode);
childBeanNodeList.add(node);
});
menuNode.setChildren(childBeanNodeList);
retNode = menuNode;
}else if(NodeType.datascope == nodeType||NodeType.action == nodeType){
PermitNode permitNode = new PermitNode();
BeanKit.copyProperties(mapNode, permitNode);
retNode = permitNode;
}
return retNode;
}
}