package com.ots.common.utils;
|
import com.ots.project.system.menu.domain.Menu;
|
import java.util.ArrayList;
|
import java.util.Iterator;
|
import java.util.List;
|
|
public class TreeUtils {
|
|
public static List<Menu> getChildPerms(List<Menu> list, int parentId) {
|
List<Menu> returnList = new ArrayList<Menu>();
|
for (Iterator<Menu> iterator = list.iterator(); iterator.hasNext(); ) {
|
Menu t = (Menu) iterator.next();
|
|
if (t.getParentId() == parentId) {
|
recursionFn(list, t);
|
returnList.add(t);
|
}
|
}
|
return returnList;
|
}
|
|
private static void recursionFn(List<Menu> list, Menu t) {
|
|
List<Menu> childList = getChildList(list, t);
|
t.setChildren(childList);
|
for (Menu tChild : childList) {
|
if (hasChild(list, tChild)) {
|
|
Iterator<Menu> it = childList.iterator();
|
while (it.hasNext()) {
|
Menu n = (Menu) it.next();
|
recursionFn(list, n);
|
}
|
}
|
}
|
}
|
|
private static List<Menu> getChildList(List<Menu> list, Menu t) {
|
List<Menu> tlist = new ArrayList<Menu>();
|
Iterator<Menu> it = list.iterator();
|
while (it.hasNext()) {
|
Menu n = (Menu) it.next();
|
if (n.getParentId().longValue() == t.getMenuId().longValue()) {
|
tlist.add(n);
|
}
|
}
|
return tlist;
|
}
|
List<Menu> returnList = new ArrayList<Menu>();
|
|
public List<Menu> getChildPerms(List<Menu> list, int typeId, String prefix) {
|
if (list == null) {
|
return null;
|
}
|
for (Iterator<Menu> iterator = list.iterator(); iterator.hasNext(); ) {
|
Menu node = (Menu) iterator.next();
|
|
if (node.getParentId() == typeId) {
|
recursionFn(list, node, prefix);
|
}
|
|
|
}
|
return returnList;
|
}
|
private void recursionFn(List<Menu> list, Menu node, String p) {
|
|
List<Menu> childList = getChildList(list, node);
|
if (hasChild(list, node)) {
|
|
returnList.add(node);
|
Iterator<Menu> it = childList.iterator();
|
while (it.hasNext()) {
|
Menu n = (Menu) it.next();
|
n.setMenuName(p + n.getMenuName());
|
recursionFn(list, n, p + p);
|
}
|
} else {
|
returnList.add(node);
|
}
|
}
|
|
private static boolean hasChild(List<Menu> list, Menu t) {
|
return getChildList(list, t).size() > 0 ? true : false;
|
}
|
}
|