From 6e7b6b8dd814649faed7ea554dc2572e2d374fba Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Sun, 22 Nov 2020 10:46:11 +0100 Subject: [PATCH] Finishing UI for category management --- .../categories/CategoriesController.java | 122 +++++++++++++++++- .../categories/CategoryFormController.java | 6 +- .../admin/categories/category-details.xhtml | 35 ++++- .../categories/categorysystem-details.xhtml | 32 ++++- .../org/libreccm/ui/AdminBundle.properties | 4 + .../org/libreccm/ui/AdminBundle_de.properties | 4 + 6 files changed, 193 insertions(+), 10 deletions(-) diff --git a/ccm-core/src/main/java/org/libreccm/ui/admin/categories/CategoriesController.java b/ccm-core/src/main/java/org/libreccm/ui/admin/categories/CategoriesController.java index 794122db0..b970811af 100644 --- a/ccm-core/src/main/java/org/libreccm/ui/admin/categories/CategoriesController.java +++ b/ccm-core/src/main/java/org/libreccm/ui/admin/categories/CategoriesController.java @@ -23,6 +23,8 @@ import org.libreccm.api.IdentifierParser; import org.libreccm.categorization.Category; import org.libreccm.categorization.CategoryManager; import org.libreccm.categorization.CategoryRepository; +import org.libreccm.categorization.Domain; +import org.libreccm.categorization.DomainRepository; import org.libreccm.core.CoreConstants; import org.libreccm.security.AuthorizationRequired; import org.libreccm.security.RequiresPrivilege; @@ -30,8 +32,6 @@ import org.libreccm.ui.Message; import org.libreccm.ui.MessageType; import org.libreccm.ui.admin.AdminMessages; -import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Locale; import java.util.Optional; @@ -40,16 +40,13 @@ import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.mvc.Controller; import javax.mvc.Models; -import javax.servlet.http.HttpServletRequest; import javax.transaction.Transactional; import javax.ws.rs.Consumes; -import javax.ws.rs.Encoded; import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; -import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; /** @@ -73,6 +70,9 @@ public class CategoriesController { @Inject private CategoryRepository categoryRepository; + @Inject + private DomainRepository domainRepository; + @Inject private IdentifierParser identifierParser; @@ -635,4 +635,116 @@ public class CategoriesController { } } + @POST + @Path("/{categoryIdentifier}/subcategories/{subCategoryIdentifier}/reorder") + @AuthorizationRequired + @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) + @Transactional(Transactional.TxType.REQUIRED) + public String reorderSubCategory( + @PathParam("categoryIdentifier") final String categoryIdentifierParam, + @PathParam("subCategoryIdentifier") final String subCategoryIdentifierParam, + @FormParam("direction") final String direction + ) { + final Identifier categoryIdentifier = identifierParser.parseIdentifier( + categoryIdentifierParam + ); + final Identifier subCategoryIdentifier = identifierParser + .parseIdentifier(subCategoryIdentifierParam); + + final Optional categoryResult; + switch (categoryIdentifier.getType()) { + case ID: + categoryResult = categoryRepository.findById( + Long.parseLong(categoryIdentifier.getIdentifier()) + ); + break; + default: + categoryResult = categoryRepository.findByUuid( + categoryIdentifier.getIdentifier() + ); + break; + } + final Category category; + if (categoryResult.isPresent()) { + category = categoryResult.get(); + } else { + categoryDetailsModel.addMessage( + new Message( + adminMessages.getMessage( + "categories.not_found.message", + Arrays.asList(categoryIdentifierParam) + ), MessageType.WARNING + ) + ); + return "org/libreccm/ui/admin/categories/category-not-found.xhtml"; + } + + final Optional subCategoryResult; + switch (subCategoryIdentifier.getType()) { + case ID: + subCategoryResult = categoryRepository.findById( + Long.parseLong(subCategoryIdentifier.getIdentifier()) + ); + break; + default: + subCategoryResult = categoryRepository.findByUuid( + subCategoryIdentifier.getIdentifier() + ); + break; + } + final Category subCategory; + if (subCategoryResult.isPresent()) { + subCategory = subCategoryResult.get(); + } else { + categoryDetailsModel.addMessage( + new Message( + adminMessages.getMessage( + "categories.not_found.message", + Arrays.asList(subCategoryIdentifierParam) + ), MessageType.WARNING + ) + ); + return "org/libreccm/ui/admin/categories/category-not-found.xhtml"; + } + + switch (direction) { + case "DECREASE": + categoryManager.decreaseCategoryOrder(subCategory, category); + break; + case "INCREASE": + categoryManager.increaseCategoryOrder(subCategory, category); + break; + default: + categoryDetailsModel.addMessage( + new Message( + adminMessages.getMessage( + "categories.invalid_direction.message", + Arrays.asList(direction)), + MessageType.WARNING + ) + ); + } + + if (category.getParentCategory() == null) { + final Optional categorySystem = domainRepository + .findByRootCategory(category); + if (categorySystem.isPresent()) { + return String.format( + "redirect:categorymanager/categorysystems/ID-%d/details", + categorySystem.get().getObjectId() + ); + } else { + return String.format( + "redirect:categorymanager/categories/ID-%d", + category.getObjectId() + ); + } + } else { + return String.format( + "redirect:categorymanager/categories/ID-%d", + category.getObjectId() + ); + } + } + } diff --git a/ccm-core/src/main/java/org/libreccm/ui/admin/categories/CategoryFormController.java b/ccm-core/src/main/java/org/libreccm/ui/admin/categories/CategoryFormController.java index 36e88016f..7582cca52 100644 --- a/ccm-core/src/main/java/org/libreccm/ui/admin/categories/CategoryFormController.java +++ b/ccm-core/src/main/java/org/libreccm/ui/admin/categories/CategoryFormController.java @@ -135,13 +135,13 @@ public class CategoryFormController { ); } else { return String.format( - "redirect:categorymanager/categories/ID-%d/details", + "redirect:categorymanager/categories/ID-%d", parentCategory.getObjectId() ); } } else { return String.format( - "redirect:categorymanager/categories/ID-%d/details", + "redirect:categorymanager/categories/ID-%d", parentCategory.getObjectId() ); } @@ -197,7 +197,7 @@ public class CategoryFormController { categoryRepository.save(category); return String.format( - "redirect:categorymanager/categories/ID-%s", + "redirect:categorymanager/categories/ID-%d", category.getObjectId() ); } else { diff --git a/ccm-core/src/main/resources/WEB-INF/views/org/libreccm/ui/admin/categories/category-details.xhtml b/ccm-core/src/main/resources/WEB-INF/views/org/libreccm/ui/admin/categories/category-details.xhtml index d9e039a18..aa3dafd87 100644 --- a/ccm-core/src/main/resources/WEB-INF/views/org/libreccm/ui/admin/categories/category-details.xhtml +++ b/ccm-core/src/main/resources/WEB-INF/views/org/libreccm/ui/admin/categories/category-details.xhtml @@ -208,6 +208,9 @@ #{AdminMessages['categories.details.subcategories.table.headings.abstract']} + + #{AdminMessages['categories.details.subcategories.table.headings.actions']} + @@ -249,6 +252,36 @@ + + +
+ + +
+
+ + + +
+ + +
+
+ - + diff --git a/ccm-core/src/main/resources/WEB-INF/views/org/libreccm/ui/admin/categories/categorysystem-details.xhtml b/ccm-core/src/main/resources/WEB-INF/views/org/libreccm/ui/admin/categories/categorysystem-details.xhtml index 13bd073c3..5d32e6eb2 100644 --- a/ccm-core/src/main/resources/WEB-INF/views/org/libreccm/ui/admin/categories/categorysystem-details.xhtml +++ b/ccm-core/src/main/resources/WEB-INF/views/org/libreccm/ui/admin/categories/categorysystem-details.xhtml @@ -336,7 +336,7 @@ #{AdminMessages['categorysystems.details.categories.table.headings.abstract']} - + #{AdminMessages['categorysystems.details.categories.table.headings.actions']} @@ -380,6 +380,36 @@ + + +
+ + +
+
+ + + +
+ + +
+
+