Skip to content

Commit

Permalink
feat: Allow to add a site or a page - MEED-7792 - Meeds-io/MIPs#159
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker authored and exo-swf committed Nov 22, 2024
1 parent 508ec12 commit dc43388
Show file tree
Hide file tree
Showing 14 changed files with 934 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,158 @@
*/
package io.meeds.social.navigation.plugin;

import static io.meeds.social.navigation.plugin.SiteSidebarPlugin.SITE_ID_PROP_NAME;
import static io.meeds.social.navigation.plugin.SiteSidebarPlugin.SITE_LABEL_FIELD_NAME;
import static io.meeds.social.navigation.plugin.SiteSidebarPlugin.SITE_NAME_PROP_NAME;
import static io.meeds.social.navigation.plugin.SiteSidebarPlugin.SITE_OBJECT_TYPE;
import static io.meeds.social.navigation.plugin.SiteSidebarPlugin.SITE_TYPE_PROP_NAME;
import static io.meeds.social.navigation.plugin.SiteSidebarPlugin.getSiteIcon;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import org.exoplatform.commons.utils.ExpressionUtil;
import org.exoplatform.portal.config.model.PortalConfig;
import org.exoplatform.portal.mop.SiteKey;
import org.exoplatform.portal.mop.State;
import org.exoplatform.portal.mop.navigation.NodeData;
import org.exoplatform.portal.mop.service.DescriptionService;
import org.exoplatform.portal.mop.service.LayoutService;
import org.exoplatform.portal.mop.service.NavigationService;
import org.exoplatform.services.resources.LocaleConfigService;
import org.exoplatform.services.resources.ResourceBundleManager;
import org.exoplatform.services.resources.ResourceBundleService;

import io.meeds.social.navigation.constant.SidebarItemType;
import io.meeds.social.navigation.model.SidebarItem;
import io.meeds.social.translation.service.TranslationService;

@Component
@Order(20)
public class PageSidebarPlugin implements SidebarPlugin {

private static final String NODE_ID_PROP_NAME = "navigationNodeId";
public static final String NODE_ID_PROP_NAME = "navigationNodeId";

public static final String SITE_DISPLAY_NAME_PROP_NAME = "siteDisplayName";

public static final String SITE_ICON_PROP_NAME = "siteIcon";

@Autowired
private NavigationService navigationService;
private LayoutService layoutService;

@Override
public SidebarItemType getType() {
return SidebarItemType.PAGE;
}
@Autowired
private NavigationService navigationService;

@Override
public SidebarItem resolveProperties(SidebarItem item, Locale locale) {
@Autowired
private TranslationService translationService;

@Autowired
private DescriptionService descriptionService;

@Autowired
private LocaleConfigService localeConfigService;

@Autowired
private ResourceBundleManager resourceBundleManager;

public static SidebarItem resolveProperties(NavigationService navigationService, // NOSONAR
LayoutService layoutService,
TranslationService translationService,
DescriptionService descriptionService,
ResourceBundleManager resourceBundleManager,
LocaleConfigService localeConfigService,
SidebarItem item,
Locale locale) {
String nodeId = item.getProperties().get(NODE_ID_PROP_NAME);
NodeData node = navigationService.getNodeById(Long.parseLong(nodeId));
if (node != null) {
item.setName(node.getName());
if (node != null && node.getState() != null) {
item.setName(getNodeLabel(navigationService,
descriptionService,
resourceBundleManager,
localeConfigService,
Long.parseLong(nodeId),
locale));
item.setTarget(node.getState().getTarget());
if (node.getState() != null && node.getState().getIcon() != null) {
item.setIcon(node.getState().getIcon());
}
SiteKey siteKey = node.getState().getSiteKey();
PortalConfig site = layoutService.getPortalConfig(siteKey);
if (site != null) {
long siteId = Long.parseLong((site.getStorageId().split("_"))[1]);
item.setProperties(new HashMap<>(item.getProperties()));
String label = translationService.getTranslationLabelOrDefault(SITE_OBJECT_TYPE,
siteId,
SITE_LABEL_FIELD_NAME,
locale);
item.getProperties().put(SITE_DISPLAY_NAME_PROP_NAME, label);
item.getProperties().put(SITE_ID_PROP_NAME, String.valueOf(siteId));
item.getProperties().put(SITE_TYPE_PROP_NAME, siteKey.getTypeName());
item.getProperties().put(SITE_NAME_PROP_NAME, siteKey.getTypeName());
item.getProperties().put(SITE_ICON_PROP_NAME, getSiteIcon(navigationService, siteKey));
}
}
return item;
}

public static String getNodeLabel(NavigationService navigationService,
DescriptionService descriptionService,
ResourceBundleManager resourceBundleManager,
LocaleConfigService localeConfigService,
long nodeId,
Locale locale) {
NodeData nodeData = navigationService.getNodeById(nodeId);
Map<Locale, State> nodeLabels = descriptionService.getDescriptions(String.valueOf(nodeId));
if (MapUtils.isEmpty(nodeLabels)) {
String label = nodeData.getState().getLabel();
if (ExpressionUtil.isResourceBindingExpression(label)) {
SiteKey siteKey = nodeData.getSiteKey();
ResourceBundle nodeLabelResourceBundle = resourceBundleManager.getNavigationResourceBundle(getLocaleName(locale),
siteKey.getTypeName(),
siteKey.getName());
if (nodeLabelResourceBundle != null) {
return ExpressionUtil.getExpressionValue(nodeLabelResourceBundle, label);
}
}
return label;
} else if (nodeLabels.containsKey(locale)) {
return nodeLabels.get(locale).getName();
} else if (nodeLabels.containsKey(localeConfigService.getDefaultLocaleConfig().getLocale())) {
return nodeLabels.get(localeConfigService.getDefaultLocaleConfig().getLocale()).getName();
} else if (nodeLabels.containsKey(ResourceBundleService.DEFAULT_CROWDIN_LOCALE)) {
return nodeLabels.get(ResourceBundleService.DEFAULT_CROWDIN_LOCALE).getName();
} else {
return nodeLabels.values().iterator().next().getName();
}
}

@Override
public SidebarItemType getType() {
return SidebarItemType.PAGE;
}

@Override
public SidebarItem resolveProperties(SidebarItem item, Locale locale) {
return resolveProperties(navigationService,
layoutService,
translationService,
descriptionService,
resourceBundleManager,
localeConfigService,
item,
locale);
}

@Override
public List<SidebarItem> getDefaultItems() {
return Collections.emptyList();
Expand All @@ -67,7 +178,12 @@ public List<SidebarItem> getDefaultItems() {
@Override
public boolean itemExists(SidebarItem item) {
String nodeId = item.getProperties().get(NODE_ID_PROP_NAME);
return navigationService.getNodeById(Long.parseLong(nodeId)) != null;
return StringUtils.isNoneBlank(nodeId) && navigationService.getNodeById(Long.parseLong(nodeId)) != null;
}

private static String getLocaleName(Locale locale) {
return locale.toLanguageTag().replace("-", "_"); // Use same name as
// localeConfigService
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
*/
package io.meeds.social.navigation.plugin;

import static io.meeds.social.navigation.plugin.PageSidebarPlugin.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand All @@ -43,10 +47,12 @@
import org.exoplatform.portal.mop.navigation.NodeContext;
import org.exoplatform.portal.mop.navigation.NodeData;
import org.exoplatform.portal.mop.navigation.NodeState;
import org.exoplatform.portal.mop.service.DescriptionService;
import org.exoplatform.portal.mop.service.LayoutService;
import org.exoplatform.portal.mop.service.NavigationService;
import org.exoplatform.services.organization.Group;
import org.exoplatform.services.organization.OrganizationService;
import org.exoplatform.services.resources.LocaleConfigService;
import org.exoplatform.services.resources.LocaleContextInfo;
import org.exoplatform.services.resources.ResourceBundleManager;
import org.exoplatform.services.resources.ResourceBundleService;
Expand All @@ -61,17 +67,17 @@
@Order(10)
public class SiteSidebarPlugin implements SidebarPlugin {

private static final String SITE_EXPAND_PAGES_PROP_NAME = "expandPages";
public static final String SITE_EXPAND_PAGES_PROP_NAME = "expandPages";

private static final String SITE_NAME_PROP_NAME = "siteName";
public static final String SITE_NAME_PROP_NAME = "siteName";

private static final String SITE_ID_PROP_NAME = "siteId";
public static final String SITE_ID_PROP_NAME = "siteId";

private static final String SITE_TYPE_PROP_NAME = "siteType";
public static final String SITE_TYPE_PROP_NAME = "siteType";

private static final String SITE_OBJECT_TYPE = "site";
public static final String SITE_OBJECT_TYPE = "site";

private static final String SITE_LABEL_FIELD_NAME = "label";
public static final String SITE_LABEL_FIELD_NAME = "label";

@Autowired
private TranslationService translationService;
Expand All @@ -88,6 +94,12 @@ public class SiteSidebarPlugin implements SidebarPlugin {
@Autowired
private ResourceBundleManager resourceBundleManager;

@Autowired
private DescriptionService descriptionService;

@Autowired
private LocaleConfigService localeConfigService;

@Override
public SidebarItemType getType() {
return SidebarItemType.SITE;
Expand Down Expand Up @@ -119,6 +131,28 @@ public SidebarItem resolveProperties(SidebarItem item, Locale locale) {
&& firstNode.getData().getState().getIcon() != null) {
item.setIcon(firstNode.getData().getState().getIcon());
}
if (StringUtils.equals(item.getProperties().get(SITE_EXPAND_PAGES_PROP_NAME), "true")) {
Collection<NodeContext<Object>> nodes = rootNode.getNodes();
item.setItems(new ArrayList<>());
nodes.forEach(node -> {
if (node.getData() != null
&& node.getData().getState() != null
&& isVisibilityEligible(node.getData().getState())) {
SidebarItem pageItem = new SidebarItem(SidebarItemType.PAGE);
pageItem.setProperties(Collections.singletonMap(NODE_ID_PROP_NAME, node.getData().getId()));
pageItem.setUrl(node.getData().getName());
PageSidebarPlugin.resolveProperties(navigationService,
layoutService,
translationService,
descriptionService,
resourceBundleManager,
localeConfigService,
pageItem,
locale);
item.getItems().add(pageItem);
}
});
}
}
return item;
}
Expand All @@ -145,26 +179,25 @@ public boolean itemExists(SidebarItem item) {
return layoutService.getPortalConfig(siteType, siteName) != null;
}

private SidebarItem toSidebarItem(SiteKey siteKey) {
protected SidebarItem toSidebarItem(SiteKey siteKey) {
return new SidebarItem(siteKey.getName(),
"/portal/" + siteKey.getName(),
null,
null,
getSiteIcon(siteKey),
SidebarItemType.SITE,
null,
buildSiteProperties(siteKey));
"/portal/" + siteKey.getName(),
null,
null,
getSiteIcon(navigationService, siteKey),
SidebarItemType.SITE,
null,
buildSiteProperties(siteKey));
}

private String getSiteIcon(SiteKey siteKey) {
public static String getSiteIcon(NavigationService navigationService, SiteKey siteKey) {
NodeContext<NodeContext<Object>> rootNode = navigationService.loadNode(siteKey);
if (rootNode != null && rootNode.getSize() > 0) {
Collection<NodeContext<Object>> nodes = rootNode.getNodes();
return nodes.stream().map(node -> {
NodeData data = node.getData();
NodeState state = data.getState();
if ((state.getVisibility() == Visibility.DISPLAYED
|| state.getVisibility() == Visibility.TEMPORAL)
if (isVisibilityEligible(state)
&& state.getPageRef() != null
&& StringUtils.isNotBlank(state.getIcon())) {
return state.getIcon();
Expand All @@ -184,8 +217,10 @@ private Map<String, String> buildSiteProperties(SiteKey siteKey) {
Map<String, String> properties = new HashMap<>();
properties.put(SITE_TYPE_PROP_NAME, siteKey.getTypeName());
properties.put(SITE_NAME_PROP_NAME, siteKey.getName());
properties.put(SITE_EXPAND_PAGES_PROP_NAME, String.valueOf(isMetaSite));
properties.put(SITE_ID_PROP_NAME, String.valueOf(siteId));
if (isMetaSite) {
properties.put(SITE_EXPAND_PAGES_PROP_NAME, "true");
}
return properties;
}

Expand Down Expand Up @@ -231,4 +266,14 @@ private ResourceBundle getBundle(String siteType, String siteName, Locale locale
siteName);
}

private static boolean isVisibilityEligible(NodeState state) {
if (state.getVisibility() == Visibility.DISPLAYED) {
return true;
} else if (state.getVisibility() == Visibility.TEMPORAL) {
return (state.getEndPublicationTime() == 0 || state.getEndPublicationTime() < System.currentTimeMillis())
&& (state.getStartPublicationTime() == 0 || state.getStartPublicationTime() > System.currentTimeMillis());
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,22 @@ generalSettings.sidebarSeparator=Separator
generalSettings.navigationSettingsUpdatedSuccessfully=Navigation Setttings updated successfully
generalSettings.editSideBarItem=Edit item
generalSettings.removeSideBarItem=Remove item
generalSettings.addSideBarItem=Add item
generalSettings.addSideBarItemSite=Site
generalSettings.addSideBarItemSpaces=Spaces
generalSettings.addSideBarItemLink=Link
generalSettings.addSideBarItemSeparator=Separator
generalSettings.add=Add
generalSettings.update=Update
generalSettings.addSideBarItemSite.drawerTitle=Add Site or Page
generalSettings.updateSideBarItemSite.drawerTitle=Update Site or Page
generalSettings.addSideBarItemSpaces.drawerTitle=Add Spaces
generalSettings.updateSideBarItemSpaces.drawerTitle=Edit Spaces
generalSettings.addSideBarItemLink.drawerTitle=Add a Link
generalSettings.updateSideBarItemLink.drawerTitle=Edit a Link
generalSettings.siteOption=Site
generalSettings.pageOption=Page
generalSettings.selectASite=Select a site
generalSettings.selectAPage=Select a page
generalSettings.selectASitePlaceholder=Start typing to search a site
generalSettings.anyPage=Any page
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
</span>
</v-btn>
</div>
<portal-general-settings-navigation-settings-sidebar-add-link-drawer />
<portal-general-settings-navigation-settings-sidebar-add-site-drawer />
<portal-general-settings-navigation-settings-sidebar-add-spaces-drawer />
</div>
</template>
<script>
Expand Down
Loading

0 comments on commit dc43388

Please sign in to comment.