/** * 设置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())); }
/** * 完成子级分类的封装,把ProductCategory的孩子都逐级封装成list,例如传入list为数据库表中的所有等级, ProductCategory为某个根等级 * ,则把该根等级下的所有子等级依次封装成树形结构的list,并返回list中的前count个;若传入null和null ,则把数据库中所有的等级组装后全部返回。 * 例子:(a,b)中a表示数据库表中的id,b表示其parent的id。 (1,0),(2,0),(3,0) * ,(4,0),(5,0),(6,1),(7,1),(8,1),(9,2),(10,2),(11,3),(12,4),(13,10) 该树形结构为 1 2 3 4 5 6 7 8 9 10 * 11 12 13 形成的list序列为:1,6,7,8,2,9,10,13,3,11,4,12,5 * * @param count 返回list中的结果数目 * @param paramProductCategory 某个等级的实例 * @return 该等级下的所有子等级list */ @Override public List<ProductCategory> findChildren(ProductCategory productCategory, Integer count) { String str; TypedQuery<ProductCategory> localTypedQuery; if (productCategory != null) { str = "select productCategory from ProductCategory productCategory where productCategory.treePath like :treePath order by productCategory.order asc"; localTypedQuery = this.getEntityManager() .createQuery(str, ProductCategory.class) .setFlushMode(FlushModeType.COMMIT) .setParameter("treePath", "%," + productCategory.getId() + "," + "%"); } else { str = "select productCategory from ProductCategory productCategory order by productCategory.order asc"; localTypedQuery = this.getEntityManager() .createQuery(str, ProductCategory.class) .setFlushMode(FlushModeType.COMMIT); } if (count != null) localTypedQuery.setMaxResults(count.intValue()); return packTheChildren(localTypedQuery.getResultList(), productCategory); }