Skip to content

Commit

Permalink
feat: Add Specific Order for Sidebar Space Templates Listing - MEED-7845
Browse files Browse the repository at this point in the history
 - Meeds-io/MIPs#159 (#4204)

This change will allow to define a specific order to display space
templates in Sidebar.
  • Loading branch information
boubaker authored and exo-swf committed Nov 28, 2024
1 parent 42c6db6 commit af62c27
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
public interface SidebarPlugin {

static final SidebarItem SIDEBAR_SEPARATOR = new SidebarItem(SidebarItemType.SEPARATOR);

/**
* @return {@link SidebarItemType} managed by the implementing plugin
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> defaultSpaceTemplatesSort;

@Override
public SidebarItemType getType() {
return SidebarItemType.SPACE_TEMPLATE;
Expand Down Expand Up @@ -74,11 +80,14 @@ public SidebarItem resolveProperties(SidebarItem item, String username, Locale l

@Override
public List<SidebarItem> getDefaultItems() {
return spaceTemplateService.getSpaceTemplates(null, Pageable.unpaged(), true)
.stream()
.filter(t -> t.isEnabled() && !t.isDeleted())
.map(this::toSidebarItem)
.toList();
List<SidebarItem> 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
Expand Down Expand Up @@ -106,4 +115,12 @@ private Map<String, String> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -127,18 +124,16 @@ private List<TopbarApplication> getDefaultTopbarApplications() {
*/
private List<SidebarItem> getDefaultNavigations() {
if (menuPlugins != null) {
List<SidebarItem> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public void testGetDefaultItems() {
List<SidebarItem> 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
Expand Down

0 comments on commit af62c27

Please sign in to comment.