/** * 设置ProductCategory的treepath和grade属性。例如根级分类的treepath为',',grade为‘0’表示顶级分类, * 次级分类的treepath为‘,1,’,其中这个1表示根级分类中第一个元素,grade为'1'表示一级分类,以此类推。 * * @param paramProductCategory 某个等级的实例 * @return */ private void setProductCategoryOfTreepathAndGrade(ProductCategory paramProductCategory) { if (paramProductCategory == null) return; ProductCategory localProductCategory = paramProductCategory.getParent(); if (localProductCategory != null) paramProductCategory.setTreePath( localProductCategory.getTreePath() + localProductCategory.getId() + ","); else paramProductCategory.setTreePath(","); paramProductCategory.setGrade(Integer.valueOf(paramProductCategory.getTreePaths().size())); }
@Override public List<ProductCategory> findParents(ProductCategory productCategory, Integer count) { if ((productCategory == null) || (productCategory.getParent() == null)) return Collections.emptyList(); String str = "select productCategory from ProductCategory productCategory where productCategory.id in (:ids) order by productCategory.grade asc"; TypedQuery<ProductCategory> localTypedQuery = this.getEntityManager() .createQuery(str, ProductCategory.class) .setFlushMode(FlushModeType.COMMIT) .setParameter("ids", productCategory.getTreePaths()); if (count != null) localTypedQuery.setMaxResults(count.intValue()); return localTypedQuery.getResultList(); }
/** * 完成子级分类的封装,把ProductCategory的孩子都逐级封装成list,例如传入list为所有等级id, * ProductCategory为某个根等级,则把该根等级下的所有子等级依次封装成树形结构的list,并返回该list * * @param paramList 所有等待封装的ProductCategory的list * @param paramProductCategory 某个等级的实例 * @return 该等级下的所有子等级list */ private List<ProductCategory> packTheChildren( List<ProductCategory> paramList, ProductCategory paramProductCategory) { ArrayList<ProductCategory> localArrayList = new ArrayList<ProductCategory>(); if (paramList != null) { Iterator<ProductCategory> localIterator = paramList.iterator(); while (localIterator.hasNext()) { ProductCategory localProductCategory = (ProductCategory) localIterator.next(); if (localProductCategory.getParent() != paramProductCategory) continue; localArrayList.add(localProductCategory); localArrayList.addAll(packTheChildren(paramList, localProductCategory)); } } return localArrayList; }