diff --git a/ccm-core/src/main/java/org/libreccm/api/admin/categorization/CategoriesApi.java b/ccm-core/src/main/java/org/libreccm/api/admin/categorization/CategoriesApi.java index 3d0a56555..7965700b1 100644 --- a/ccm-core/src/main/java/org/libreccm/api/admin/categorization/CategoriesApi.java +++ b/ccm-core/src/main/java/org/libreccm/api/admin/categorization/CategoriesApi.java @@ -81,21 +81,11 @@ public class CategoriesApi { @PathParam("domainIdentifier") final String domainIdentifierParam, @PathParam("path") final String categoryPath ) { - final Domain domain = repository.findDomain(domainIdentifierParam); - final Category category = categoryRepository - .findByPath(domain, categoryPath) - .orElseThrow( - () -> new WebApplicationException( - String.format( - "No category with path %s in Domain %s found.", - categoryPath, - domain.getDomainKey() - ), - Response.Status.NOT_FOUND - ) - ); - - return new CategoryData(category); + return new CategoryData( + findCategory( + repository.findDomain(domainIdentifierParam), + categoryPath) + ); } @GET @@ -107,18 +97,7 @@ public class CategoriesApi { public CategoryData getCategory( @PathParam("categoryId") final long categoryId ) { - return new CategoryData( - categoryRepository - .findById(categoryId) - .orElseThrow( - () -> new WebApplicationException( - String.format( - "No Category with ID %d found.", - categoryId - ) - ) - ) - ); + return new CategoryData(findCategory(categoryId)); } @GET @@ -130,18 +109,7 @@ public class CategoriesApi { public CategoryData getCategory( @PathParam("categoryId") final String categoryUuid ) { - return new CategoryData( - categoryRepository - .findByUuid(categoryUuid) - .orElseThrow( - () -> new WebApplicationException( - String.format( - "No Category with UUID %s found.", - categoryUuid - ) - ) - ) - ); + return new CategoryData(findCategory(categoryUuid)); } @PUT @@ -156,18 +124,7 @@ public class CategoriesApi { final CategoryData categoryData ) { final Domain domain = repository.findDomain(domainIdentifierParam); - final Category category = categoryRepository - .findByPath(domain, categoryPath) - .orElseThrow( - () -> new WebApplicationException( - String.format( - "No category with path %s in Domain %s found.", - categoryPath, - domain.getDomainKey() - ), - Response.Status.NOT_FOUND - ) - ); + final Category category = findCategory(domain, categoryPath); return updateCategory(category, categoryData) .entity( @@ -190,16 +147,7 @@ public class CategoriesApi { @PathParam("catgoryId") final long categoryId, final CategoryData categoryData ) { - final Category category = categoryRepository - .findById(categoryId) - .orElseThrow( - () -> new WebApplicationException( - String.format( - "No Category with ID %d found.", categoryId - ), - Response.Status.NOT_FOUND - ) - ); + final Category category = findCategory(categoryId); return updateCategory(category, categoryData) .entity( @@ -220,16 +168,7 @@ public class CategoriesApi { @PathParam("catgoryUuid") final String uuid, final CategoryData categoryData ) { - final Category category = categoryRepository - .findByUuid(uuid) - .orElseThrow( - () -> new WebApplicationException( - String.format( - "No Category with UIID %s found.", uuid - ), - Response.Status.NOT_FOUND - ) - ); + final Category category = findCategory(uuid); return updateCategory(category, categoryData) .entity( @@ -306,12 +245,36 @@ public class CategoriesApi { @AuthorizationRequired @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) @Transactional(Transactional.TxType.REQUIRED) - public CategoryData deleteCategory( - @PathParam("domainIdentifier") final String domainIdentifierParam, - @PathParam("path") final String categoryPathTokens + public Response deleteCategory( + @PathParam("domainIdentifier") final String domainIdentifier, + @PathParam("path") final String categoryPath ) { - // Delete only empty categories!!! - throw new UnsupportedOperationException(); + final Domain domain = repository.findDomain(domainIdentifier); + final Category category = findCategory(domain, categoryPath); + + if (categoryManager.hasSubCategories(category) + || categoryManager.hasObjects(category)) { + return Response + .status(Response.Status.BAD_REQUEST) + .entity( + String.format( + "Category %s of Domain %s is not empty.", + categoryPath, + domain.getDomainKey() + ) + ).build(); + } + + categoryRepository.delete(category); + + return Response + .ok( + String.format( + "Category %s of Domain %s deleted successfully.", + categoryPath, + domain.getDomainKey() + ) + ).build(); } // delete update category by id @Path("/ID-{categoryId}") @@ -320,11 +283,28 @@ public class CategoriesApi { @AuthorizationRequired @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) @Transactional(Transactional.TxType.REQUIRED) - public CategoryData deleteCategory( - @PathParam("categoryId") final long categoryIdParam + public Response deleteCategory( + @PathParam("categoryId") final long categoryId ) { - // Delete only empty categories!!! - throw new UnsupportedOperationException(); + final Category category = findCategory(categoryId); + + if (categoryManager.hasSubCategories(category) + || categoryManager.hasObjects(category)) { + return Response + .status(Response.Status.BAD_REQUEST) + .entity( + String.format( + "Category %d is not empty.", categoryId + ) + ).build(); + } + + return Response + .ok( + String.format( + "Category %d deleted successfully.", categoryId + ) + ).build(); } @DELETE @@ -332,11 +312,28 @@ public class CategoriesApi { @AuthorizationRequired @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) @Transactional(Transactional.TxType.REQUIRED) - public CategoryData deleteCategory( - @PathParam("categoryId") final String categoryUuidParam + public Response deleteCategory( + @PathParam("categoryId") final String uuid ) { - // Delete only empty categories!!! - throw new UnsupportedOperationException(); + final Category category = findCategory(uuid); + + if (categoryManager.hasSubCategories(category) + || categoryManager.hasObjects(category)) { + return Response + .status(Response.Status.BAD_REQUEST) + .entity( + String.format( + "Category %s is not empty.", uuid + ) + ).build(); + } + + return Response + .ok( + String.format( + "Category %s deleted successfully.", uuid + ) + ).build(); } @GET @@ -609,4 +606,45 @@ public class CategoriesApi { throw new UnsupportedOperationException(); } + private Category findCategory(final Domain domain, final String path) { + return categoryRepository + .findByPath(domain, path) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "No category with path %s in Domain %s found.", + path, + domain.getDomainKey() + ), + Response.Status.NOT_FOUND + ) + ); + } + + private Category findCategory(final long categoryId) { + return categoryRepository + .findById(categoryId) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "No Category with ID %d found.", categoryId + ), + Response.Status.NOT_FOUND + ) + ); + } + + private Category findCategory(final String uuid) { + return categoryRepository + .findByUuid(uuid) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "No Category with UIID %s found.", uuid + ), + Response.Status.NOT_FOUND + ) + ); + } + }