Skip to content

Commit

Permalink
#28894 Changes input from indeos to keys
Browse files Browse the repository at this point in the history
  • Loading branch information
freddyDOTCMS committed Jul 12, 2024
1 parent e754b6a commit 0876f2c
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.categories.business.CategoryAPI;
import com.dotmarketing.portlets.categories.business.PaginatedCategories;
import com.dotmarketing.portlets.categories.model.Category;
import com.dotmarketing.portlets.categories.model.HierarchyShortCategory;
import com.dotmarketing.util.ActivityLogger;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.PageMode;
import com.dotmarketing.util.PaginatedArrayList;
import com.dotmarketing.util.UtilMethods;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -291,7 +288,7 @@ public final Response getChildren(@Context final HttpServletRequest httpRequest,
*
* <code>
* {
* "inodes": ["352ca17e238357ce12e9dff10afc8516", "c0ab974897f8d37c98afa4bba74ef9f1"]
* "keys": ["key_1", "key_2"]
* }
* </code>
*
Expand All @@ -301,25 +298,29 @@ public final Response getChildren(@Context final HttpServletRequest httpRequest,
* {
* entity: [
* {
* "inode": "352ca17e238357ce12e9dff10afc8516",
* "inode": "1",
* "key": "key_1",
* "name": "Name_1",
* "parentList": [
* {
* 'categoryName': 'Grand Parent Name',
* 'name': 'Grand Parent Name',
* 'key': 'Grand Parent Key',
* 'inode': 'Grand Parent inode'
* },
* {
* 'categoryName': 'Parent Name',
* 'name': 'Parent Name',
* 'key': 'Parent Key',
* 'inode': 'Parent inode'
* }
* ]
* },
* {
* "inode": "c0ab974897f8d37c98afa4bba74ef9f1",
* "inode": "2",
* "key": "key_2",
* "name": "Name_2",
* "parentList": [
* {
* 'categoryName': 'Category name value',
* 'name': 'Category name value',
* 'key': 'Key value',
* 'inode': 'inode value'
* }
Expand All @@ -346,15 +347,15 @@ public final Response getChildren(@Context final HttpServletRequest httpRequest,
@Produces({MediaType.APPLICATION_JSON})
public final Response getHierarchy(@Context final HttpServletRequest httpRequest,
@Context final HttpServletResponse httpResponse,
final CategoryInodesForm form) throws DotDataException, DotSecurityException {
final CategoryKeysForm form) throws DotDataException, DotSecurityException {

Logger.debug(this, () -> "Getting the List of Parents for the follow categories: " +
form.getInodes().stream().collect(Collectors.joining(",")));
String.join(",", form.getKeys()));

webResource.init(null, httpRequest, httpResponse, true, null);

return Response.ok(
new HierarchyShortCategoriesResponseView(categoryAPI.findHierarchy(form.getInodes()))).build();
new HierarchyShortCategoriesResponseView(categoryAPI.findHierarchy(form.getKeys()))).build();
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.dotcms.rest.api.v1.categories;

import com.dotcms.rest.api.Validated;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import java.util.List;

/**
* Category Input Form
*/
@JsonDeserialize(builder = CategoryKeysForm.Builder.class)
public class CategoryKeysForm extends Validated {

private List<String> keys;

private CategoryKeysForm(final Builder builder) {

this.keys = builder.keys;
}

public List<String> getKeys() {
return keys;
}

public static final class Builder {

@JsonProperty
private List<String> keys;

public void setKeys(List<String> keys) {
this.keys = keys;
}

public CategoryKeysForm build() {

return new CategoryKeysForm(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,11 @@ PaginatedCategories findAll(final CategorySearchCriteria searchCriteria, final U
* | 2 | Child | child | Top Category |
* | 3 | Grand Child | grand_child | Child |
*
* And you search by inode 3 then you got:
* And you search by key 'grand_child' then you got:
*
* Inode: 3
* key: 'grand_child'
* categoryName: 'Grand Child'
* parentList <code>[
* {
* 'categoryName':'Top Category',
Expand All @@ -528,9 +530,9 @@ PaginatedCategories findAll(final CategorySearchCriteria searchCriteria, final U
* }
* ]</code>
*
* @param inodes List of inodes to search
* @param keys List of keys to search
* @return
* @throws DotDataException
*/
List<HierarchyShortCategory> findHierarchy(final Collection<String> inodes) throws DotDataException;
List<HierarchyShortCategory> findHierarchy(final Collection<String> keys) throws DotDataException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -976,15 +976,15 @@ public PaginatedCategories findAll(final CategorySearchCriteria searchCriteria,
/**
* Default implementation of {@link CategoryAPI}
*
* @param inodes List of inodes to search
* @param keys List of keys to search
* @return
*
* @throws DotDataException
*/
@CloseDBIfOpened
@Override
public List<HierarchyShortCategory> findHierarchy(final Collection<String> inodes) throws DotDataException {
Logger.debug(this, "Getting parentList for the follow Categories: " + inodes);
return categoryFactory.findHierarchy(inodes);
public List<HierarchyShortCategory> findHierarchy(final Collection<String> keys) throws DotDataException {
Logger.debug(this, "Getting parentList for the follow Categories: " + keys);
return categoryFactory.findHierarchy(keys);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public abstract class CategoryFactory {
public abstract List<HierarchedCategory> findAll(final CategorySearchCriteria searchCriteria) throws DotDataException;

/**
* Find Categories by inodes and calculate its Hierarchy.
* Find Categories by keys and calculate its Hierarchy.
*
* For Example if you have the follows categories:
*
Expand All @@ -301,9 +301,11 @@ public abstract class CategoryFactory {
* | 2 | Child | child | Top Category |
* | 3 | Grand Child | grand_child | Child |
*
* And you search by inode 3 then you got:
* And you search by key 'grand_child' then you got:
*
* Inode: 3
* key: 'grand_child'
* categoryName: 'Grand Child'
* parentList <code>[
* {
* 'categoryName':'Top Category',
Expand All @@ -317,9 +319,9 @@ public abstract class CategoryFactory {
* }
* ]</code>
*
* @param inodes List of inodes to search
* @param keys List of keys to search
* @return
* @throws DotDataException
*/
public abstract List<HierarchyShortCategory> findHierarchy(final Collection<String> inodes) throws DotDataException;
public abstract List<HierarchyShortCategory> findHierarchy(final Collection<String> keys) throws DotDataException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class CategoryFactoryImpl extends CategoryFactory {

public static final String INODE = "inode";
public static final String CATEGORY_NAME = "category_name";
public static final String CATEGORY_KEY = "category_key";
CategoryCache catCache;
final CategorySQL categorySQL;

Expand Down Expand Up @@ -670,7 +671,7 @@ private static List<ShortCategory> getShortCategories(String parentsASJsonArray)
return ((List<Map<String, String>>) JsonUtil.getObjectFromJson(parentsASJsonArray, List.class))
.stream()
.map(map -> new ShortCategory.Builder()
.setCategoryName(map.get("categoryName"))
.setName(map.get("categoryName"))
.setKey(map.get("key"))
.setInode(map.get(INODE))
.build()
Expand Down Expand Up @@ -976,14 +977,14 @@ private void updateCache(List<? extends Category> categories) throws DotDataExce
/**
* Default implementation of {@link CategoryFactory#findHierarchy(Collection)}
*
* @param inodes list of inodes to search
* @param keys list of keys to search
* @return
* @throws DotDataException
*/
@Override
public List<HierarchyShortCategory> findHierarchy(final Collection<String> inodes) throws DotDataException {
public List<HierarchyShortCategory> findHierarchy(final Collection<String> keys) throws DotDataException {

if (inodes == null || inodes.isEmpty()) {
if (keys == null || keys.isEmpty()) {
return Collections.emptyList();
}

Expand All @@ -992,37 +993,45 @@ public List<HierarchyShortCategory> findHierarchy(final Collection<String> inode
"c.inode AS root_inode," +
"c.category_name," +
"c.category_name AS root_category_name," +
"c.category_key," +
"c.category_key AS root_category_key," +
"1 AS level," +
"json_build_object('inode', c.inode, 'categoryName', c.category_name, 'key', c.category_key)::varchar AS path " +
"FROM Category c " +
"WHERE c.inode IN (%s) " +
"WHERE c.category_key IN (%s) " +
"UNION ALL " +
"SELECT " +
"c.inode, " +
"ch.root_inode AS root_inode, " +
"c.category_name, " +
"ch.root_category_name AS root_category_name, " +
"c.category_key, " +
"ch.root_category_key AS root_category_key, " +
"ch.level + 1 AS level, " +
"CONCAT(json_build_object('inode', c.inode, 'categoryName', c.category_name, 'key', c.category_key)::varchar, ',', ch.path) AS path " +
"FROM Category c JOIN tree t ON c.inode = t.parent JOIN CategoryHierarchy ch ON t.child = ch.inode " +
")," +
"MaxLevels AS (SELECT root_inode, MAX(level) AS max_level FROM CategoryHierarchy GROUP BY root_inode) " +
"SELECT " +
"ch.root_inode as inode, ch.root_category_name as category_name, " +
"ch.root_inode as inode, ch.root_category_name as category_name, ch.root_category_key as category_key," +
"CONCAT('[', path, ']')::jsonb as path " +
"FROM CategoryHierarchy ch JOIN MaxLevels ml ON ch.root_inode = ml.root_inode AND ch.level = ml.max_level;";


final DotConnect dc = new DotConnect()
.setSQL(String.format(queryTemplate, DotConnect.createParametersPlaceholder(inodes.size())));
.setSQL(String.format(queryTemplate, DotConnect.createParametersPlaceholder(keys.size())));

inodes.forEach(dc::addParam);
keys.forEach(dc::addParam);

return dc.loadObjectResults().stream().map(row -> {
final List<ShortCategory> parentList = getShortCategories(row.get("path").toString());

return new HierarchyShortCategory(row.get(INODE).toString(), row.get(CATEGORY_NAME).toString(),
parentList.subList(0, parentList.size() - 1));
return new HierarchyShortCategory.Builder()
.setInode(row.get(INODE).toString())
.setName(row.get(CATEGORY_NAME).toString())
.setKey(row.get(CATEGORY_KEY).toString())
.setParentList(parentList.subList(0, parentList.size() - 1))
.build();
}).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
*/
public class HierarchyShortCategory implements Serializable {
private final String inode;
private final String categoryName;
private final String name;
private final String key;
private final List<ShortCategory> parentList;

public HierarchyShortCategory(final String inode, final String categoryName, final List<ShortCategory> parentList) {
this.inode = inode;
this.parentList = parentList;
this.categoryName = categoryName;
public HierarchyShortCategory(final Builder builder) {
this.inode = builder.inode;
this.parentList = builder.parentList;
this.name = builder.name;
this.key = builder.key;
}

public String getInode() {
Expand All @@ -25,7 +27,43 @@ public List<ShortCategory> getParentList() {
return parentList;
}

public String getCategoryName() {
return categoryName;
public String getName() {
return name;
}

public String getKey() {
return key;
}

public static class Builder {
private String inode;
private String name;

private String key;
private List<ShortCategory> parentList;

public Builder setInode(String inode) {
this.inode = inode;
return this;
}

public Builder setName(String name) {
this.name = name;
return this;
}

public Builder setKey(String key) {
this.key = key;
return this;
}

public Builder setParentList(List<ShortCategory> parentList) {
this.parentList = parentList;
return this;
}

public HierarchyShortCategory build(){
return new HierarchyShortCategory(this);
}
}
}
Loading

0 comments on commit 0876f2c

Please sign in to comment.