表结构:
tableName? parentTable
? A????????????????? B
? A????????????????? C
? B????????????????? D
? B??????????????????E
? C????????????????? F
? C??????????????????G
?
展示树:
??????????? A
???B?????????????? C
D???? E????? ?F?????? G
?
?
@Controller
public class ViewLevelMetaController {
???
?private Log logger = LogFactory.getLog(this.getClass());
?private StringBuffer nodeStr=new StringBuffer();
?@Autowired
?private ViewLevelMetaService viewLevelMetaService;
?@Autowired
?private TableTreeService tableTreeService;
?@RequestMapping("/metadata/getViewLevelTableCreateInfo.do")
?public String getViewLevelTableCreateInfo(ModelMap model, String table) {
??logger.info("getViewLevelTableCreateInfo");
??
??List<treeNode> nodeList = tableTreeService.getTreeNodeList();
??//当json字符串为空时才输出,防止重复输出
??if("[]".equals(modifyStr(nodeStr.toString()))){
???getTreeNode(nodeList, new treeNode("A","A"));
??}
??
??model.put("treeNodeStr",modifyStr(nodeStr.toString()));
??
??return "metadata/main";
?}
?/**
? * 修饰一下才能满足Json格式?
? * @param nodeStr
? * @return
? */
?public String modifyStr(String nodeStr){
??????? return ("["+nodeStr+"]").replaceAll(",]", "]");????
??? }?
?/**
? * 把树组装成json格式数据
? *???? A
? * B????? C
? *
? * [{tableName:'A',url:'null',children:[
? *? {tableName:'B',url:'null',children:null},
? *? {tableName:'C',url:'null',children:null}
? *? ]}]
? *?
? * @param treeNodeList
? * @param node
? */
?private void getTreeNode(List<treeNode> treeNodeList, treeNode node){
?? if(hasChild(treeNodeList,node)){???
???????????? nodeStr.append("{tableName:");?
???????????? nodeStr.append("'"+node.getTableName()+"'");?
???????????? nodeStr.append(",url:");?
???????????? nodeStr.append("'"+node.getUrl()+"'");?
???????????? nodeStr.append(",children:[");???
???????????? List<treeNode> childNodeList = getChildList(treeNodeList,node);???
???????????? for(treeNode childNode:childNodeList){
???????????? ?getTreeNode(treeNodeList,childNode);
???????????? }
???????????? nodeStr.append("]},");???
???????? }else{???
???????????? nodeStr.append("{tableName:");?
???????????? nodeStr.append("'"+node.getTableName()+"'");
???????????? nodeStr.append(",url:");?
???????????? nodeStr.append("'"+node.getUrl()+"'");?
???????????? nodeStr.append(",children:null},");?
???????? }?
?}
?/**
? * 判断是否有子节点?
? * @param list
? * @param node
? * @return
? */
?private boolean hasChild(List<treeNode> list, treeNode node){
???? return getChildList(list,node).size()>0?true:false;?
? }
?/**
? * 得到子节点列表?
? * @param list
? * @param node
? * @return
? */
?private List<treeNode> getChildList(List<treeNode> list , treeNode node){?
??List<treeNode> li = new ArrayList<treeNode>();??
???? for(treeNode l:list){
???? ?if(l.getParentTable().equals(node.getTableName())){
???? ??li.add(l);
???? ?}
???? }
???? return li;????
???? }?
}
?