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 Catalog saveCatalog(Catalog catalog) {
    Catalog sameAliasCatalog = findByAlias(catalog.getModule(), catalog.getAlias());
    if ((catalog.getId() == null && sameAliasCatalog != null)
        || (catalog.getId() != null
            && sameAliasCatalog != null
            && !sameAliasCatalog.getId().equals(catalog.getId())))
      throw new AppException(ErrorCode.ENTITY_ALREADY_EXIST, "访问别名已被使用,请换一个!");

    Date now = new Date();
    Catalog entity = null;
    if (catalog.getId() == null) {
      entity = new Catalog();
      entity.setUpdateAt(now);
      entity.setCreateAt(now);
    } else {
      entity = catalogDao.findOne(catalog.getId());
      if (entity == null) throw new AppException(ErrorCode.ENTITY_NOT_FOUND, "要更新的栏目没有找到!");
      entity.setUpdateAt(now);
    }
    entity.setAlias(catalog.getAlias());
    entity.setClickDisplay(catalog.getClickDisplay());
    entity.setLevel(getSetLevel(catalog));
    entity.setModule(catalog.getModule());
    entity.setNavDisplay(catalog.getNavDisplay());
    entity.setNewWindowOpen(
        catalog.getNewWindowOpen() != null ? catalog.getNewWindowOpen() : false);
    entity.setPid(
        catalog.getPid() == null ? null : (catalog.getPid() == 0 ? null : catalog.getPid()));
    entity.setRank(catalog.getRank() != null ? catalog.getRank() : 0);
    entity.setSubTitle(catalog.getSubTitle());
    entity.setTitle(catalog.getTitle());

    if (entity.getModule().ordinal() == Module.outlink.ordinal()) {
      if (entity.getAttributes() == null) entity.setAttributes(new HashMap<String, String>());
      entity.getAttributes().put(Catalog.ATTR_KEY_OUTLINK_URL, catalog.getOutlinkUrl());
    }
    return catalogDao.save(entity);
  }
  @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;
  }