package com.ischoolbar.programmer.util; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.ischoolbar.programmer.entity.admin.Menu; import com.ischoolbar.programmer.entity.common.ProductCategory; public class MenuUtil { public static List getAllTopMenu(List menuList){ List ret = new ArrayList(); for(Menu menu:menuList){ if(menu.getParentId() == 0){ ret.add(menu); } } return ret; } public static List getAllSecondMenu(List menuList){ List ret = new ArrayList(); List allTopMenu = getAllTopMenu(menuList); for(Menu menu:menuList){ for(Menu topMenu:allTopMenu){ if(menu.getParentId() == topMenu.getId()){ ret.add(menu); break; } } } return ret; } public static List getAllThirdMenu(List menuList,Long secondMenuId){ List ret = new ArrayList(); for(Menu menu:menuList){ if(menu.getParentId() == secondMenuId){ ret.add(menu); } } return ret; } public static List> getTreeCategory(List productCategorieList){ List> ret = new ArrayList>(); for(ProductCategory productCategory : productCategorieList){ if(productCategory.getParentId() == null){ Map top = new HashMap(); top.put("id", productCategory.getId()); top.put("text", productCategory.getName()); top.put("children", new ArrayList>()); ret.add(top); } } for(ProductCategory productCategory : productCategorieList){ if(productCategory.getParentId() != null){ for(Map map : ret){ if(productCategory.getParentId().longValue() == Long.valueOf(map.get("id")+"")){ List children = (List)map.get("children"); Map child = new HashMap(); child.put("id", productCategory.getId()); child.put("text", productCategory.getName()); child.put("children", new ArrayList>()); children.add(child); } } } } for(ProductCategory productCategory : productCategorieList){ if(productCategory.getParentId() != null){ for(Map map : ret){ List> children = (List>)map.get("children"); for(Map child : children){ if(productCategory.getParentId().longValue() == Long.valueOf(child.get("id")+"")){ List grandsons = (List)child.get("children"); Map grandson = new HashMap(); grandson.put("id", productCategory.getId()); grandson.put("text", productCategory.getName()); grandsons.add(grandson); } } } } } return ret; } }