From f1561504cd2d621c446696236cd222b1552bd65a Mon Sep 17 00:00:00 2001 From: MichalKinas Date: Wed, 29 Mar 2023 16:22:48 +0200 Subject: [PATCH] ACS-4966 Add option to include path while fetching Categories --- .../rest/api/impl/CategoriesImpl.java | 34 +++++++++++++++---- .../org/alfresco/rest/api/model/Category.java | 24 +++++++++++-- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/remote-api/src/main/java/org/alfresco/rest/api/impl/CategoriesImpl.java b/remote-api/src/main/java/org/alfresco/rest/api/impl/CategoriesImpl.java index 53f9eddab8..5e25f27ed4 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/impl/CategoriesImpl.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/impl/CategoriesImpl.java @@ -69,6 +69,7 @@ import org.apache.commons.lang3.StringUtils; public class CategoriesImpl implements Categories { static final String INCLUDE_COUNT_PARAM = "count"; + static final String INCLUDE_PATH_PARAM = "path"; static final String NOT_A_VALID_CATEGORY = "Node id does not refer to a valid category"; static final String NO_PERMISSION_TO_MANAGE_A_CATEGORY = "Current user does not have permission to manage a category"; static final String NO_PERMISSION_TO_READ_CONTENT = "Current user does not have read permission to content"; @@ -111,6 +112,11 @@ public class CategoriesImpl implements Categories category.setCount(categoriesCount.getOrDefault(category.getId(), 0)); } + if (parameters.getInclude().contains(INCLUDE_PATH_PARAM)) + { + category.setPath(nodeService.getPath(nodeRef).toDisplayPath(nodeService, permissionService)); + } + return category; } @@ -136,11 +142,18 @@ public class CategoriesImpl implements Categories public List getCategoryChildren(final StoreRef storeRef, final String parentCategoryId, final Parameters parameters) { final NodeRef parentNodeRef = getCategoryNodeRef(storeRef, parentCategoryId); - final List categories = nodeService.getChildAssocs(parentNodeRef).stream() - .filter(ca -> ContentModel.ASSOC_SUBCATEGORIES.equals(ca.getTypeQName())) - .map(ChildAssociationRef::getChildRef) - .map(this::mapToCategory) - .collect(Collectors.toList()); + final List categories = nodeService.getChildAssocs(parentNodeRef) + .stream() + .filter(ca -> ContentModel.ASSOC_SUBCATEGORIES.equals(ca.getTypeQName())) + .map(ChildAssociationRef::getChildRef) + .map(this::mapToCategory) + .peek(category -> { + if (parameters.getInclude().contains(INCLUDE_PATH_PARAM)) + { + category.setPath(nodeService.getPath(nodes.getNode(category.getId()).getNodeRef()).toDisplayPath(nodeService, permissionService)); + } + }) + .collect(Collectors.toList()); if (parameters.getInclude().contains(INCLUDE_COUNT_PARAM)) { @@ -200,7 +213,16 @@ public class CategoriesImpl implements Categories } final Collection actualCategories = DefaultTypeConverter.INSTANCE.getCollection(NodeRef.class, currentCategories); - return actualCategories.stream().map(this::mapToCategory).collect(Collectors.toList()); + return actualCategories + .stream() + .map(this::mapToCategory) + .peek(category -> { + if (parameters.getInclude().contains(INCLUDE_PATH_PARAM)) + { + category.setPath(nodeService.getPath(nodes.getNode(category.getId()).getNodeRef()).toDisplayPath(nodeService, permissionService)); + } + }) + .collect(Collectors.toList()); } @Override diff --git a/remote-api/src/main/java/org/alfresco/rest/api/model/Category.java b/remote-api/src/main/java/org/alfresco/rest/api/model/Category.java index 812d362a96..d65b22b8a8 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/model/Category.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/model/Category.java @@ -35,6 +35,7 @@ public class Category private String parentId; private boolean hasChildren; private Integer count; + private String path; public String getId() { @@ -91,6 +92,14 @@ public class Category this.count = count; } + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + @Override public boolean equals(Object o) { @@ -100,19 +109,20 @@ public class Category return false; Category category = (Category) o; return hasChildren == category.hasChildren && Objects.equals(id, category.id) && Objects.equals(name, category.name) && Objects.equals(parentId, category.parentId) - && Objects.equals(count, category.count); + && Objects.equals(count, category.count) && Objects.equals(path, category.path); } @Override public int hashCode() { - return Objects.hash(id, name, parentId, hasChildren, count); + return Objects.hash(id, name, parentId, hasChildren, count, path); } @Override public String toString() { - return "Category{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", parentId='" + parentId + '\'' + ", hasChildren=" + hasChildren + ", count=" + count + '}'; + return "Category{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", parentId='" + parentId + '\'' + ", hasChildren=" + hasChildren + + ", count=" + count + ", path=" + path + '}'; } public static Builder builder() @@ -127,6 +137,7 @@ public class Category private String parentId; private boolean hasChildren; private Integer count; + private String path; public Builder id(String id) { @@ -158,6 +169,12 @@ public class Category return this; } + public Builder path(String path) + { + this.path = path; + return this; + } + public Category create() { final Category category = new Category(); @@ -166,6 +183,7 @@ public class Category category.setParentId(parentId); category.setHasChildren(hasChildren); category.setCount(count); + category.setPath(path); return category; } }