From 086a8622de548ecae27333da65d0e36ee8331c79 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Fri, 19 Feb 2021 17:36:08 +0100 Subject: [PATCH] Some bugfixes for category ordering Former-commit-id: 9de74d77291d945df502383710a1b5cffbbc86e6 --- .../contentsections/CategoriesController.java | 12 +++++- .../categorization/CategoryManager.java | 38 ++++++++++++++++--- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoriesController.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoriesController.java index 710e2e093..413ed72c6 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoriesController.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoriesController.java @@ -830,11 +830,19 @@ public class CategoriesController { break; } + final String parentCategoryPath = categoryManager + .getCategoryPath(parentCategory); + final String pathFragment; + if ("/".equals(parentCategoryPath)) { + pathFragment = ""; + } else { + pathFragment = parentCategoryPath; + } return String.format( - "redirect:/%s/categorysystems/%s/categories/%s", + "redirect:/%s/categorysystems/%s/categories/%s#subcategories-section", sectionIdentifier, context, - categoryManager.getCategoryPath(parentCategory) + pathFragment ); } else { return result.getResponseTemplate(); diff --git a/ccm-core/src/main/java/org/libreccm/categorization/CategoryManager.java b/ccm-core/src/main/java/org/libreccm/categorization/CategoryManager.java index 91c752963..8b6eddb3d 100644 --- a/ccm-core/src/main/java/org/libreccm/categorization/CategoryManager.java +++ b/ccm-core/src/main/java/org/libreccm/categorization/CategoryManager.java @@ -789,6 +789,8 @@ public class CategoryManager implements Serializable { categoryRepo.save(subCategory); categoryRepo.save(nextCategory); }); + + fixSubCategoryOrder(parentCategory); } /** @@ -864,6 +866,30 @@ public class CategoryManager implements Serializable { categoryRepo.save(subCategory); categoryRepo.save(prevCategory); }); + + fixSubCategoryOrder(parentCategory); + } + + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public void fixSubCategoryOrder(final Category category) { + Objects.requireNonNull(category); + + long order = 1; + final List subCategories = new ArrayList<>( + category.getSubCategories() + ); + subCategories.sort( + (c1, c2) -> { + return Long.compare(c1.getCategoryOrder(), c2.getCategoryOrder()); + }); + for (final Category subCategory : subCategories) { + subCategory.setCategoryOrder(order); + shiro.getSystemUser().execute(() -> { + categoryRepo.save(subCategory); + }); + order++; + } } /** @@ -878,22 +904,22 @@ public class CategoryManager implements Serializable { */ @Transactional(Transactional.TxType.REQUIRED) public List getCategoriesInPath(final Category ofCategory) { - + Objects.requireNonNull(ofCategory); - + List categories = new ArrayList<>(); - + Category current = categoryRepo.findById(ofCategory.getObjectId()) .orElseThrow(() -> new IllegalArgumentException(String.format( "No category with ID %d in the database. Where did that ID come from?", ofCategory.getObjectId()))); - - while(current.getParentCategory() != null) { + + while (current.getParentCategory() != null) { categories.add(current); current = current.getParentCategory(); } Collections.reverse(categories); - + return categories; }