Search in sources :

Example 1 with CategoryVO

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());
        }
    }
}
Also used : CategoryVO(run.halo.app.model.vo.CategoryVO) ArrayDeque(java.util.ArrayDeque)

Example 2 with CategoryVO

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) PostCategory(run.halo.app.model.entity.PostCategory) CategoryVO(run.halo.app.model.vo.CategoryVO)

Example 3 with CategoryVO

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;
}
Also used : Category(run.halo.app.model.entity.Category) LinkedList(java.util.LinkedList) CategoryVO(run.halo.app.model.vo.CategoryVO) ArrayDeque(java.util.ArrayDeque) NonNull(org.springframework.lang.NonNull)

Example 4 with CategoryVO

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;
}
Also used : CategoryVO(run.halo.app.model.vo.CategoryVO) NonNull(org.springframework.lang.NonNull)

Example 5 with 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());
    }
}
Also used : CategoryVO(run.halo.app.model.vo.CategoryVO)

Aggregations

CategoryVO (run.halo.app.model.vo.CategoryVO)19 NonNull (org.springframework.lang.NonNull)8 Category (run.halo.app.model.entity.Category)8 PostCategory (run.halo.app.model.entity.PostCategory)8 ArrayDeque (java.util.ArrayDeque)6 LinkedList (java.util.LinkedList)5 ArrayList (java.util.ArrayList)4 Objects (com.google.common.base.Objects)3 Collection (java.util.Collection)3 Collections (java.util.Collections)3 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3 Slf4j (lombok.extern.slf4j.Slf4j)3 StringUtils (org.apache.commons.lang3.StringUtils)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Lazy (org.springframework.context.annotation.Lazy)3 Page (org.springframework.data.domain.Page)3