private CatalogLevel getSetLevel(Catalog catalog) { if (catalog.getPid() != null && catalog.getPid() > 0) { Long pid = catalog.getPid(); Catalog pcatalog = catalogDao.findOne(pid); if (pcatalog.getLevel().ordinal() == CatalogLevel.FIRST.ordinal()) { return CatalogLevel.SECOND; } else if (pcatalog.getLevel().ordinal() == CatalogLevel.SECOND.ordinal()) { return CatalogLevel.THIRD; } else { throw new AppException(ErrorCode.ILLEGAL_PARAM, "父栏目非法!"); } } else { return CatalogLevel.FIRST; } }
@Override public List<CatalogItem> listCatalogItems() { List<Catalog> catalogs = findAll(); if (CollectionUtils.isEmpty(catalogs)) return new ArrayList<CatalogItem>(); Map<Long, List<CatalogItem>> thirdChildrenMap = new LinkedHashMap<Long, List<CatalogItem>>(); for (Catalog c : catalogs) { if (c.getLevel().ordinal() == CatalogLevel.THIRD.ordinal()) { if (!thirdChildrenMap.containsKey(c.getPid())) thirdChildrenMap.put(c.getPid(), new ArrayList<CatalogItem>()); CatalogItem ct = new CatalogItem(); ct.setCatalog(c); thirdChildrenMap.get(c.getPid()).add(ct); } } Map<Long, List<CatalogItem>> secondChildrenMap = new LinkedHashMap<Long, List<CatalogItem>>(); for (Catalog c : catalogs) { if (c.getLevel().ordinal() == CatalogLevel.SECOND.ordinal()) { if (!secondChildrenMap.containsKey(c.getPid())) secondChildrenMap.put(c.getPid(), new ArrayList<CatalogItem>()); CatalogItem ct = new CatalogItem(); ct.setCatalog(c); ct.setSubCatalog(thirdChildrenMap.get(c.getId())); secondChildrenMap.get(c.getPid()).add(ct); } } List<CatalogItem> list = new ArrayList<CatalogItem>(); for (Catalog c : catalogs) { if (c.getLevel().ordinal() == CatalogLevel.FIRST.ordinal()) { CatalogItem ct = new CatalogItem(); ct.setCatalog(c); ct.setSubCatalog(secondChildrenMap.get(c.getId())); list.add(ct); } } return list; }