use of run.halo.app.model.vo.CategoryVO in project halo by ruibaby.
the class PostCategoryServiceImpl method walkCategoryTree.
private void walkCategoryTree(List<CategoryVO> categoryTree, Consumer<CategoryVO> consumer) {
Queue<CategoryVO> queue = new ArrayDeque<>(categoryTree);
while (!queue.isEmpty()) {
CategoryVO category = queue.poll();
consumer.accept(category);
if (HaloUtils.isNotEmpty(category.getChildren())) {
queue.addAll(category.getChildren());
}
}
}
use of run.halo.app.model.vo.CategoryVO in project halo by ruibaby.
the class PostCategoryServiceImpl method populatePostIds.
private void populatePostIds(List<CategoryVO> categoryTree) {
Assert.notNull(categoryTree, "The categoryTree must not be null.");
Map<Integer, Set<Integer>> categoryPostIdsMap = postCategoryRepository.findAll().stream().collect(Collectors.groupingBy(PostCategory::getCategoryId, Collectors.mapping(PostCategory::getPostId, Collectors.toSet())));
walkCategoryTree(categoryTree, category -> {
// Set post count
Set<Integer> postIds = categoryPostIdsMap.getOrDefault(category.getId(), new LinkedHashSet<>());
category.setPostIds(postIds);
});
CategoryVO categoryTreeRootNode = new CategoryVO();
categoryTreeRootNode.setChildren(categoryTree);
mergePostIdsFromBottomToTop(categoryTreeRootNode);
}
use of run.halo.app.model.vo.CategoryVO in project halo by ruibaby.
the class CategoryServiceImpl method walkCategoryTree.
/**
* Walk a category tree with root node.
*
* @param root a root node of category tree.
* @return a flattened category list
*/
@NonNull
private List<Category> walkCategoryTree(CategoryVO root) {
Assert.notNull(root, "The category 'root' must not be null");
List<Category> categories = new LinkedList<>();
Queue<CategoryVO> queue = new ArrayDeque<>();
queue.add(root);
while (!queue.isEmpty()) {
CategoryVO categoryNode = queue.poll();
Category category = new Category();
BeanUtils.updateProperties(categoryNode, category);
categories.add(category);
if (HaloUtils.isNotEmpty(categoryNode.getChildren())) {
queue.addAll(categoryNode.getChildren());
}
}
return categories;
}
use of run.halo.app.model.vo.CategoryVO in project halo by ruibaby.
the class CategoryServiceImpl method convertToCategoryVo.
@NonNull
private CategoryVO convertToCategoryVo(Category category) {
Assert.notNull(category, "The category must not be null.");
CategoryVO categoryVo = new CategoryVO().convertFrom(category);
categoryVo.setFullPath(buildCategoryFullPath(categoryVo.getSlug()));
return categoryVo;
}
use of run.halo.app.model.vo.CategoryVO in project halo by ruibaby.
the class PostCategoryServiceImpl method mergePostIdsFromBottomToTop.
private void mergePostIdsFromBottomToTop(CategoryVO root) {
if (root == null) {
return;
}
List<CategoryVO> children = root.getChildren();
if (CollectionUtils.isEmpty(children)) {
return;
}
for (CategoryVO category : children) {
mergePostIdsFromBottomToTop(category);
if (root.getPostIds() == null) {
root.setPostIds(new LinkedHashSet<>());
}
// merge post ids.
root.getPostIds().addAll(category.getPostIds());
}
}
Aggregations