Skip to content

Commit

Permalink
#28896 Create Factory method to find categories in all levels
Browse files Browse the repository at this point in the history
  • Loading branch information
freddyDOTCMS committed Jun 26, 2024
1 parent 2181d4a commit 6c956f3
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.portlets.categories.model.Category;
import com.liferay.portal.model.User;

/**
*
Expand Down Expand Up @@ -273,5 +274,14 @@ public abstract class CategoryFactory {
* @throws DotDataException
*/
abstract protected List<Category> getAllChildren(Categorizable parent) throws DotDataException;

/**
* Return a list of Categories regardless of their levels.
*
* @param filter Value used to filter the Category by, returning only Categories that contain this value in their key, name, or variable name
*
* @return List of Category filtered
*/
public abstract List<Category> findAll(final String filter) throws DotDataException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,7 @@ protected List<Category> findAll() throws DotDataException {
.loadObjectResults();

List<Category> categories = convertForCategories(result);
for(final Category category : categories) {
//Updating the cache since we are already loading all the categories
if(catCache.get(category.getInode()) == null)
try {
catCache.put(category);
} catch (DotCacheException e) {
throw new DotDataException(e.getMessage(), e);
}
}
updateCache(categories);
return categories;
}

Expand Down Expand Up @@ -843,4 +835,40 @@ protected String suggestVelocityVarName(final String categoryVelVarName) throws
throw new DotDataException("Unable to suggest a variable name. Got to:" + var);
}

/**
* Default Implementation for {@link CategoryFactory#findAll(String)}
* @param filter Value used to filter the Category by, returning only Categories that contain this value in their key, name, or variable name
*
* @return
*/
public List<Category> findAll(final String filter) throws DotDataException {

final DotConnect dc = new DotConnect()
.setSQL("SELECT * FROM category " +
"WHERE LOWER(category.category_name) LIKE ? OR " +
"LOWER(category.category_key) LIKE ? OR " +
"LOWER(category.category_velocity_var_name) LIKE ?");

if ( UtilMethods.isSet(filter) ) {
dc.addObject("%" + filter.toLowerCase() + "%");
dc.addObject("%" + filter.toLowerCase() + "%");
dc.addObject("%" + filter.toLowerCase() + "%");
}

List<Category> categories = convertForCategories(dc.loadObjectResults());
updateCache(categories);
return categories;
}

private void updateCache(List<Category> categories) throws DotDataException {
for(final Category category : categories) {
if(catCache.get(category.getInode()) == null)
try {
catCache.put(category);
} catch (DotCacheException e) {
throw new DotDataException(e.getMessage(), e);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.dotmarketing.portlets.categories.business;

import static com.dotcms.util.CollectionsUtils.list;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.dotcms.IntegrationTestBase;
import com.dotcms.datagen.CategoryDataGen;
import com.dotcms.util.IntegrationTestInitService;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.FactoryLocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.categories.model.Category;
import java.util.List;
import java.util.stream.Collectors;

import com.liferay.util.StringUtil;
import net.bytebuddy.utility.RandomString;
import org.jetbrains.annotations.NotNull;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -25,6 +33,7 @@ public class CategoryFactoryTest extends IntegrationTestBase {

@BeforeClass
public static void prepare() throws Exception {
IntegrationTestInitService.getInstance().init();
categoryFactory = FactoryLocator.getCategoryFactory();
}

Expand Down Expand Up @@ -276,4 +285,92 @@ public void Test_Has_Dependencies() throws DotDataException {
assertTrue(categoryFactory.hasDependencies(root));
}

/**
* Method to test: {@link CategoryFactoryImpl#findAll(String)}
* When: Create several Categories some of them with the word 'INCLUDE' on its name. key or variable name
* and call the method with filter equals to 'INCLUDE'
* Should: Return all the Category with the word include on any place
*/
@Test
public void getAllCategoriesFiltered() throws DotDataException {

final String stringToFilterBy = new RandomString().nextString();

final Category topLevelCategory_1 = new CategoryDataGen().setCategoryName("Top Level Category " + stringToFilterBy)
.setKey("top_level_categoria")
.setCategoryVelocityVarName("top_level_categoria")
.nextPersisted();

final Category childCategory_1 = new CategoryDataGen().setCategoryName("Child Category 1")
.setKey("child_category_1 " + stringToFilterBy)
.setCategoryVelocityVarName("child_category_1")
.parent(topLevelCategory_1)
.nextPersisted();

final Category childCategory_2 = new CategoryDataGen().setCategoryName("Child Category 2")
.setKey("child_category_2")
.setCategoryVelocityVarName("child_category_2 " + stringToFilterBy)
.parent(topLevelCategory_1)
.nextPersisted();

final Category topLevelCategory_2 = new CategoryDataGen().setCategoryName("Top Level Category " + stringToFilterBy)
.setKey("top_level_categoria")
.setCategoryVelocityVarName("top_level_categoria")
.nextPersisted();

final Category childCategory_3 = new CategoryDataGen().setCategoryName("Child Category 3 " + stringToFilterBy)
.setKey("child_category_3")
.setCategoryVelocityVarName("child_category_3")
.parent(topLevelCategory_2)
.nextPersisted();

final Category childCategory_4 = new CategoryDataGen().setCategoryName("Child Category 4")
.setKey("child_category_4")
.setCategoryVelocityVarName("child_category_4")
.parent(topLevelCategory_2)
.nextPersisted();

final Category topLevelCategory_3 = new CategoryDataGen().setCategoryName("Top Level Category")
.setKey("top_level_categoria")
.setCategoryVelocityVarName("top_level_categoria " + stringToFilterBy)
.nextPersisted();

final Category childCategory_5 = new CategoryDataGen().setCategoryName("Child Category 5")
.setKey("child_category_5")
.setCategoryVelocityVarName("child_category_5")
.parent(topLevelCategory_3)
.nextPersisted();

final Category topLevelCategory_4 = new CategoryDataGen().setCategoryName("Top Level Category")
.setKey("top_level_categoria")
.setCategoryVelocityVarName("top_level_categoria")
.parent(topLevelCategory_3)
.nextPersisted();

final Category childCategory_6 = new CategoryDataGen().setCategoryName("Child Category 6")
.setKey("child_category_6")
.setCategoryVelocityVarName("child_category_6")
.parent(topLevelCategory_3)
.nextPersisted();

final Category grandchildCategory_1 = new CategoryDataGen().setCategoryName("Grand Child Category 6 " + stringToFilterBy)
.setKey("grand_child_category_6")
.setCategoryVelocityVarName("grand_child_category_6")
.parent(childCategory_6)
.nextPersisted();

List<String> categoriesExpected = list(topLevelCategory_1, childCategory_1, childCategory_2, childCategory_3,
topLevelCategory_3, grandchildCategory_1).stream().map(Category::getInode).collect(Collectors.toList());

final List<String> categories_1 = FactoryLocator.getCategoryFactory().findAll(stringToFilterBy.toUpperCase())
.stream().map(Category::getInode).collect(Collectors.toList());
assertEquals(categoriesExpected.size(), categories_1.size());
assertTrue(categories_1.containsAll(categoriesExpected));

final List<String> categories_2 = FactoryLocator.getCategoryFactory().findAll(stringToFilterBy.toLowerCase())
.stream().map(Category::getInode).collect(Collectors.toList());
assertEquals(categoriesExpected.size(), categories_2.size());
assertTrue(categories_2.containsAll(categoriesExpected));
}

}

0 comments on commit 6c956f3

Please sign in to comment.