diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java index 1018149472a7..41d8d4ed498b 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java @@ -4,6 +4,7 @@ import com.dotmarketing.exception.DotDataException; import com.dotmarketing.portlets.categories.model.Category; +import com.liferay.portal.model.User; /** * @@ -273,5 +274,14 @@ public abstract class CategoryFactory { * @throws DotDataException */ abstract protected List 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 findAll(final String filter) throws DotDataException; } 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 537ed5d8982e..5623fa47e807 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 @@ -165,15 +165,7 @@ protected List findAll() throws DotDataException { .loadObjectResults(); List 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; } @@ -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 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 categories = convertForCategories(dc.loadObjectResults()); + updateCache(categories); + return categories; + } + + private void updateCache(List 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); + } + } + } + } diff --git a/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java b/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java index 0a9f6b924ea3..f80b3f32e963 100644 --- a/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java +++ b/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java @@ -1,5 +1,6 @@ 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; @@ -7,11 +8,18 @@ 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; @@ -25,6 +33,7 @@ public class CategoryFactoryTest extends IntegrationTestBase { @BeforeClass public static void prepare() throws Exception { + IntegrationTestInitService.getInstance().init(); categoryFactory = FactoryLocator.getCategoryFactory(); } @@ -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 categoriesExpected = list(topLevelCategory_1, childCategory_1, childCategory_2, childCategory_3, + topLevelCategory_3, grandchildCategory_1).stream().map(Category::getInode).collect(Collectors.toList()); + + final List 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 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)); + } + }