Skip to content

Commit

Permalink
#28896 Create Endpoint to find category in all levels
Browse files Browse the repository at this point in the history
  • Loading branch information
freddyDOTCMS committed Jun 26, 2024
1 parent 25f7d38 commit 0ca3b06
Show file tree
Hide file tree
Showing 3 changed files with 665 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ public final Response getCategories(@Context final HttpServletRequest httpReques
@QueryParam(PaginationUtil.PER_PAGE) final int perPage,
@DefaultValue("category_name") @QueryParam(PaginationUtil.ORDER_BY) final String orderBy,
@DefaultValue("ASC") @QueryParam(PaginationUtil.DIRECTION) final String direction,
@QueryParam("showChildrenCount") final boolean showChildrenCount) {
@QueryParam("showChildrenCount") final boolean showChildrenCount,
@QueryParam("allLevels") final boolean allLevels) {

final InitDataObject initData = webResource.init(null, httpRequest, httpResponse, true,
null);
Expand All @@ -178,6 +179,7 @@ public final Response getCategories(@Context final HttpServletRequest httpReques

final Map<String, Object> extraParams = new HashMap<>();
extraParams.put("childrenCategories", false);
extraParams.put("searchInAllLevels", allLevels);

try {
response = showChildrenCount == false ? this.paginationUtil.getPage(httpRequest, user, filter, page, perPage, orderBy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,38 @@ public CategoriesPaginator(){
this(APILocator.getCategoryAPI());
}

/**
*
* Return {@link Category} by pagiunation, you can et 3 parameters to set the way the search is done:
*
* - searchInAllLevels: A Boolean value. If TRUE, the search will look for the category at any level,
* and the childrenCategories parameter is ignored.
* - childrenCategories: A Boolean value. If FALSE, the search is limited to the top-level category. If TRUE,
* the search is confined to the children of the category specified by the inode parameter.
* - inode: If set and childrenCategories is TRUE, the search is limited to the children of the category with this inode.
*
* @param user The {@link User} that is using this method.
* @param filter Allows you to add more conditions to the query via SQL code. It's very
* important that you always sanitize the code in order to avoid SQL
* injection attacks. The complexity of the allows SQL code will depend on
* how the Paginator class is implemented.
* @param limit The maximum number of returned items in the result set, for pagination
* purposes.
* @param offset The requested page number of the result set, for pagination purposes.
* @param orderby The order-by clause, which must always be sanitized as well.
* @param direction The direction of the order-by clause for the results.
* @param extraParams A {@link Map} including any extra parameters that may be required by the
* implementation class that are not part of this method's signature.
*
* @return
*/
@Override
public PaginatedArrayList<Category> getItems(final User user, final String filter, final int limit, final int offset,
final String orderby, final OrderDirection direction, final Map<String, Object> extraParams) {

boolean childrenCategories = extraParams.containsKey("childrenCategories") ? (Boolean)extraParams.get("childrenCategories") : false;
boolean searchInAllLevels = extraParams.containsKey("searchInAllLevels") ? (Boolean)extraParams.get("searchInAllLevels") : false;
String inode = extraParams.containsKey("inode") ? String.valueOf(extraParams.get("inode")) : StringPool.BLANK;

try {
String categoriesSort = null;
Expand All @@ -47,10 +74,8 @@ public PaginatedArrayList<Category> getItems(final User user, final String filte
}

final PaginatedArrayList<Category> result = new PaginatedArrayList<>();
final PaginatedCategories categories = childrenCategories == false ?
categoryAPI.findTopLevelCategories(user, false, offset, limit, filter, categoriesSort)
: categoryAPI.findChildren(user, extraParams.containsKey("inode") ? String.valueOf(extraParams.get("inode")) : StringPool.BLANK,
false, offset, limit, filter, categoriesSort);
final PaginatedCategories categories = searchInAllLevels ? searchAllLevels(user, filter, limit, offset) :
searchInOneLevel(user, filter, inode, limit, offset, childrenCategories, categoriesSort);

result.setTotalResults(categories.getTotalCount());

Expand All @@ -63,4 +88,43 @@ public PaginatedArrayList<Category> getItems(final User user, final String filte
throw new DotRuntimeException(e);
}
}

/**
* Search for categories at every level.
*
* @param user to check Permission
* @param filter Search Filter to apply in key, name or variable's name
* @param limit The maximum number of returned items in the result set, for pagination purposes.
* @param offset The requested page number of the result set, for pagination purposes.
* @return
* @throws DotDataException
* @throws DotSecurityException
*/
private PaginatedCategories searchAllLevels(User user, String filter, int limit, int offset)
throws DotDataException, DotSecurityException {
return categoryAPI.findAll(filter, user, false, limit, offset);
}

/**
* Search for a category at a single level. This could be either the top level or within the immediate children of any category.
*
* @param user to Check permission
* @param filter Search Filter to apply in key, name or variable's name
* @param inode If the parent inode is set, the search will be conducted only among the children of this category.
* Otherwise, the search will include only the top-level categories.
* @param limit The maximum number of returned items in the result set, for pagination purposes.
* @param offset The requested page number of the result set, for pagination purposes.
* @param childrenCategories If it is false then Search only on the Top Level
* @param orderby Field to order by
* @return
* @throws DotDataException
* @throws DotSecurityException
*/
private PaginatedCategories searchInOneLevel(final User user, final String filter, final String inode, final int limit,
final int offset, final boolean childrenCategories,
final String orderby) throws DotDataException, DotSecurityException {
return childrenCategories == false ?
categoryAPI.findTopLevelCategories(user, false, offset, limit, filter, orderby) :
categoryAPI.findChildren(user, inode, false, offset, limit, filter, orderby);
}
}
Loading

0 comments on commit 0ca3b06

Please sign in to comment.