diff --git a/dotCMS/src/main/java/com/dotcms/util/pagination/CategoriesPaginator.java b/dotCMS/src/main/java/com/dotcms/util/pagination/CategoriesPaginator.java index 64ead7ec759c..9527128d94e0 100644 --- a/dotCMS/src/main/java/com/dotcms/util/pagination/CategoriesPaginator.java +++ b/dotCMS/src/main/java/com/dotcms/util/pagination/CategoriesPaginator.java @@ -80,7 +80,6 @@ public PaginatedArrayList getItems(final User user, final String filte .rootInode(inode) .build(); - final PaginatedCategories categories = searchInAllLevels ? searchAllLevels(user, searchingCriteria) : searchInOneLevel(user, searchingCriteria, childrenCategories); diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java index 49038ea61399..13cc3ca021d3 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java @@ -1,9 +1,6 @@ package com.dotmarketing.portlets.categories.business; -import com.dotcms.util.CloseUtils; -import com.dotcms.util.DotPreconditions; -import com.dotcms.util.JsonUtil; -import com.dotcms.util.ReflectionUtils; +import com.dotcms.util.*; import com.dotmarketing.beans.Tree; import com.dotmarketing.business.APILocator; import com.dotmarketing.business.CacheLocator; @@ -638,6 +635,7 @@ private List convertForHierarchedCategories(final List parentList = getShortCategories(parentsASJsonArray); category.setParentList(parentList.subList(0, parentList.size() - 1)); + category.setChildrenCount(ConversionUtils.toInt(row.get("childrencount"), 0)); } categories.add(category); @@ -951,7 +949,8 @@ private static String getFindAllSQLQuery(CategorySearchCriteria searchCriteria) "SELECT c.*, CONCAT(ch.path, ',', json_build_object('inode', c.inode, 'categoryName', c.category_name, 'key', c.category_key)::varchar) AS path " + "FROM Category c JOIN tree t ON c.inode = t.child JOIN CategoryHierarchy ch ON t.parent = ch.inode " + ") " + - "SELECT distinct *,path FROM CategoryHierarchy %s ORDER BY %s %s"; + "SELECT distinct *,path, (SELECT COUNT(*) FROM tree WHERE parent = ch.inode) as childrenCount " + + "FROM CategoryHierarchy ch %s ORDER BY %s %s"; final String rootCategoryFilter = UtilMethods.isSet(searchCriteria.rootInode) ? "WHERE c.inode = ?" : StringPool.BLANK; diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/model/HierarchedCategory.java b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/model/HierarchedCategory.java index dfc9f0525635..bc2469e87eff 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/model/HierarchedCategory.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/model/HierarchedCategory.java @@ -19,6 +19,7 @@ public class HierarchedCategory extends Category{ private List parentList; + private int childrenCount; public void setParentList(final List parentList) { this.parentList = parentList; @@ -28,6 +29,14 @@ public List getParentList() { return parentList; } + public void setChildrenCount(int childrenCount) { + this.childrenCount = childrenCount; + } + + public int getChildrenCount() { + return childrenCount; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/dotcms-postman/src/main/resources/postman/Category.postman_collection.json b/dotcms-postman/src/main/resources/postman/Category.postman_collection.json index 74bc24a87f99..e178d5c8773e 100644 --- a/dotcms-postman/src/main/resources/postman/Category.postman_collection.json +++ b/dotcms-postman/src/main/resources/postman/Category.postman_collection.json @@ -1,6 +1,6 @@ { "info": { - "_postman_id": "c43472ec-b1b2-41c9-8a13-3905b6e3d255", + "_postman_id": "944aa93e-7b0e-4eac-a7d5-8324663ca417", "name": "Category", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", "_exporter_id": "30436704" @@ -2541,7 +2541,8 @@ " ", " pm.expect(jsonData.entity.categoryName).to.eql(\"PostmanTest Child 1\");", "});", - "" + "", + "pm.collectionVariables.set(\"child1Inode\", jsonData.entity.inode);" ], "type": "text/javascript", "packages": {} @@ -2768,6 +2769,68 @@ }, "response": [] }, + { + "name": "Create Grandchild Category 2", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code should be ok 200\", function () {", + " pm.response.to.have.status(200);", + "});", + "", + "var jsonData = pm.response.json();", + "", + "pm.test(\"Information Saved Correctly\", function () {", + " ", + " pm.expect(jsonData.entity.categoryName).to.eql(\"Grandchild 2\");", + "});", + "", + "", + "" + ], + "type": "text/javascript", + "packages": {} + } + } + ], + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{jwt}}", + "type": "string" + } + ] + }, + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"key\":\"CGrandchild_2\",\n \"categoryName\" : \"Grandchild 2\",\n \"keywords\":\"This is a child test category\",\n \"categoryVelocityVarName\" : \"GrandcßhildTestKey_2\",\n \"parent\" : \"{{child1Inode}}\"\n}\n\n", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{serverURL}}/api/v1/categories", + "host": [ + "{{serverURL}}" + ], + "path": [ + "api", + "v1", + "categories" + ] + } + }, + "response": [] + }, { "name": "Create Second Top Level category", "event": [ @@ -2853,15 +2916,18 @@ " pm.expect('PostmanTest Child 1').to.have.equals(jsonData.entity[i].categoryName);", " pm.expect(1).to.have.equals(jsonData.entity[i].parentList.length);", " pm.expect('PostmanTest Top level Category').to.have.equals(jsonData.entity[i].parentList[0].name);", + " pm.expect(1).to.have.equals(jsonData.entity[i].childrenCount);", " } else if (i === 1) {", " pm.expect('PostmanTest Child 2').to.have.equals(jsonData.entity[i].categoryName);", " pm.expect(1).to.have.equals(jsonData.entity[i].parentList.length);", " pm.expect('PostmanTest Top level Category').to.have.equals(jsonData.entity[i].parentList[0].name);", + " pm.expect(0).to.have.equals(jsonData.entity[i].childrenCount);", " } else if (i === 2) {", " pm.expect('PostmanTest Grandchild 1').to.have.equals(jsonData.entity[i].categoryName);", " pm.expect(2).to.have.equals(jsonData.entity[i].parentList.length);", " pm.expect('PostmanTest Top level Category').to.have.equals(jsonData.entity[i].parentList[0].name);", " pm.expect('Child 3').to.have.equals(jsonData.entity[i].parentList[1].name);", + " pm.expect(0).to.have.equals(jsonData.entity[i].childrenCount);", " } ", " }", "", @@ -6205,6 +6271,10 @@ { "key": "grantChildCategoryKeyHierarchy", "value": "" + }, + { + "key": "child1Inode", + "value": "" } ] } \ No newline at end of file