package com.yan.entity;
import java.util.List;
public class SoftwareTree {
private Integer id;
private String name;
private Integer pid;
private List<SoftwareTree> children;
}
package com.yan.utils;
import java.util.ArrayList;
import java.util.List;
import com.yan.entity.SoftwareTree;
public class MenuTree {
private List<SoftwareTree> SoftwareTreeList = new ArrayList<SoftwareTree>();
public MenuTree(List<SoftwareTree> SoftwareTreeList) {
this.SoftwareTreeList=SoftwareTreeList;
}
public List<SoftwareTree> builTree(){
List<SoftwareTree> treeSoftwareTrees =new ArrayList<SoftwareTree>();
for(SoftwareTree SoftwareTreeNode : getRootNode()) {
SoftwareTreeNode=buildChilTree(SoftwareTreeNode);
treeSoftwareTrees.add(SoftwareTreeNode);
}
return treeSoftwareTrees;
}
private SoftwareTree buildChilTree(SoftwareTree pNode){
List<SoftwareTree> chilSoftwareTrees =new ArrayList<SoftwareTree>();
for(SoftwareTree SoftwareTreeNode : SoftwareTreeList) {
if(SoftwareTreeNode.getPid() == pNode.getId()) {
chilSoftwareTrees.add(buildChilTree(SoftwareTreeNode));
}
}
pNode.setChildren(chilSoftwareTrees);
return pNode;
}
private List<SoftwareTree> getRootNode() {
List<SoftwareTree> rootSoftwareTreeLists =new ArrayList<SoftwareTree>();
for(SoftwareTree SoftwareTreeNode : SoftwareTreeList) {
if(SoftwareTreeNode.getPid() == 0) {
rootSoftwareTreeLists.add(SoftwareTreeNode);
}
}
return rootSoftwareTreeLists;
}
}
public List<SoftwareTree> getTree(SoftwareTree s) throws ParseException{
List<SoftwareTree> list = borrowingDao.getByPage(s);
MenuTree menuTree =new MenuTree(list);
List<SoftwareTree> builTree = menuTree.builTree();
return builTree;
}