From 9c5ae24aecd5400e73cc7df256326e46226b4bd1 Mon Sep 17 00:00:00 2001 From: Boubaker Khanfir Date: Wed, 27 Nov 2024 14:23:20 +0100 Subject: [PATCH] feat: Add Specific Order for Sidebar Space Templates Listing - MEED-7845 - Meeds-io/MIPs#159 This change will allow to define a specific order to display space templates in Sidebar. --- .../navigation/plugin/SidebarPlugin.java | 2 ++ .../plugin/SpaceTemplateSidebarPlugin.java | 27 +++++++++++++++---- .../NavigationConfigurationInitService.java | 27 ++++++++----------- .../SpaceTemplateSidebarPluginTest.java | 5 ++-- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/component/api/src/main/java/io/meeds/social/navigation/plugin/SidebarPlugin.java b/component/api/src/main/java/io/meeds/social/navigation/plugin/SidebarPlugin.java index bc400f5faa1..f9a2a7e0e57 100644 --- a/component/api/src/main/java/io/meeds/social/navigation/plugin/SidebarPlugin.java +++ b/component/api/src/main/java/io/meeds/social/navigation/plugin/SidebarPlugin.java @@ -30,6 +30,8 @@ */ public interface SidebarPlugin { + static final SidebarItem SIDEBAR_SEPARATOR = new SidebarItem(SidebarItemType.SEPARATOR); + /** * @return {@link SidebarItemType} managed by the implementing plugin */ diff --git a/component/core/src/main/java/io/meeds/social/navigation/plugin/SpaceTemplateSidebarPlugin.java b/component/core/src/main/java/io/meeds/social/navigation/plugin/SpaceTemplateSidebarPlugin.java index 1c585067453..12365be878b 100644 --- a/component/core/src/main/java/io/meeds/social/navigation/plugin/SpaceTemplateSidebarPlugin.java +++ b/component/core/src/main/java/io/meeds/social/navigation/plugin/SpaceTemplateSidebarPlugin.java @@ -18,12 +18,15 @@ */ package io.meeds.social.navigation.plugin; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import org.apache.commons.lang3.ArrayUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.annotation.Order; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Component; @@ -44,6 +47,9 @@ public class SpaceTemplateSidebarPlugin extends AbstractSpaceSidebarPlugin { @Autowired private SpaceTemplateService spaceTemplateService; + @Value("#{'${social.spaceTemplates.sidebar.defaultSort:announcement,community,project,circle}'.split(',')}") + private List defaultSpaceTemplatesSort; + @Override public SidebarItemType getType() { return SidebarItemType.SPACE_TEMPLATE; @@ -74,11 +80,14 @@ public SidebarItem resolveProperties(SidebarItem item, String username, Locale l @Override public List getDefaultItems() { - return spaceTemplateService.getSpaceTemplates(null, Pageable.unpaged(), true) - .stream() - .filter(t -> t.isEnabled() && !t.isDeleted()) - .map(this::toSidebarItem) - .toList(); + List spaceTemplateItems = spaceTemplateService.getSpaceTemplates(null, Pageable.unpaged(), true) + .stream() + .filter(t -> t.isEnabled() && !t.isDeleted()) + .sorted(this::sortDefaultSpaceTemplates) + .map(this::toSidebarItem) + .toList(); + return Arrays.asList(ArrayUtils.addFirst(spaceTemplateItems.toArray(new SidebarItem[0]), + SIDEBAR_SEPARATOR)); } @Override @@ -106,4 +115,12 @@ private Map buildSpaceTemplateProperties(SpaceTemplate spaceTemp return properties; } + private int sortDefaultSpaceTemplates(SpaceTemplate t1, SpaceTemplate t2) { + return getDefaultTemplateOrder(t1) - getDefaultTemplateOrder(t2); + } + + private int getDefaultTemplateOrder(SpaceTemplate spaceTemplate) { + return spaceTemplate.isSystem() ? defaultSpaceTemplatesSort.indexOf(spaceTemplate.getLayout()) : 100; + } + } diff --git a/component/core/src/main/java/io/meeds/social/navigation/service/NavigationConfigurationInitService.java b/component/core/src/main/java/io/meeds/social/navigation/service/NavigationConfigurationInitService.java index 78f8bbff23b..da1e2b8e1ee 100644 --- a/component/core/src/main/java/io/meeds/social/navigation/service/NavigationConfigurationInitService.java +++ b/component/core/src/main/java/io/meeds/social/navigation/service/NavigationConfigurationInitService.java @@ -32,12 +32,11 @@ import org.exoplatform.portal.config.model.TransientApplicationState; import io.meeds.common.ContainerTransactional; -import io.meeds.social.navigation.constant.SidebarItemType; import io.meeds.social.navigation.constant.SidebarMode; import io.meeds.social.navigation.constant.TopbarItemType; +import io.meeds.social.navigation.model.NavigationConfiguration; import io.meeds.social.navigation.model.SidebarConfiguration; import io.meeds.social.navigation.model.SidebarItem; -import io.meeds.social.navigation.model.NavigationConfiguration; import io.meeds.social.navigation.model.TopbarApplication; import io.meeds.social.navigation.model.TopbarConfiguration; import io.meeds.social.navigation.plugin.SidebarPlugin; @@ -52,8 +51,6 @@ public class NavigationConfigurationInitService { private static final String TOP_NAVIGATION_ADDON_CONTAINER = "middle-topNavigation-container"; - private static final SidebarItem SIDEBAR_SEPARATOR = new SidebarItem(SidebarItemType.SEPARATOR); - @Autowired private NavigationConfigurationService navigationConfigurationService; @@ -127,18 +124,16 @@ private List getDefaultTopbarApplications() { */ private List getDefaultNavigations() { if (menuPlugins != null) { - List list = menuPlugins.stream() - .map(SidebarPlugin::getDefaultItems) - .flatMap(items -> { - if (CollectionUtils.isEmpty(items)) { - return Stream.empty(); - } else { - return Stream.concat(items.stream(), Stream.of(SIDEBAR_SEPARATOR)); - } - }) - .toList(); - // Delete last separator - return list.isEmpty() ? list : list.stream().limit(list.size() - 1l).toList(); + return menuPlugins.stream() + .map(SidebarPlugin::getDefaultItems) + .flatMap(items -> { + if (CollectionUtils.isEmpty(items)) { + return Stream.empty(); + } else { + return items.stream(); + } + }) + .toList(); } else { return Collections.emptyList(); } diff --git a/component/core/src/test/java/io/meeds/social/navigation/plugin/SpaceTemplateSidebarPluginTest.java b/component/core/src/test/java/io/meeds/social/navigation/plugin/SpaceTemplateSidebarPluginTest.java index fa755e15177..af7d5bc71a4 100644 --- a/component/core/src/test/java/io/meeds/social/navigation/plugin/SpaceTemplateSidebarPluginTest.java +++ b/component/core/src/test/java/io/meeds/social/navigation/plugin/SpaceTemplateSidebarPluginTest.java @@ -62,8 +62,9 @@ public void testGetDefaultItems() { List defaultItems = spaceTemplateSidebarPlugin.getDefaultItems(); assertNotNull(defaultItems); assertTrue(defaultItems.stream() - .anyMatch(item -> StringUtils.equals(item.getProperties().get(SPACE_TEMPLATE_ID_PROP_NAME), - String.valueOf(spaceTemplate.getId())))); + .anyMatch(item -> item.getProperties() != null + && StringUtils.equals(item.getProperties().get(SPACE_TEMPLATE_ID_PROP_NAME), + String.valueOf(spaceTemplate.getId())))); } @Test