Skip to content

Commit

Permalink
feat: Allow to Save Page Template Preferences - MEED-6997 - Meeds-io/…
Browse files Browse the repository at this point in the history
…meeds#2096 (#106)

This change will allow to save portlet preferences of Page Templates
when creating it or updating it from Page Template Editor.
  • Loading branch information
boubaker authored and SaraBoutej committed Aug 29, 2024
1 parent ad3711b commit 59c21b5
Show file tree
Hide file tree
Showing 23 changed files with 272 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.springframework.web.server.ResponseStatusException;

import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.portal.config.model.Page;
import org.exoplatform.portal.config.model.ModelObject;
import org.exoplatform.portal.mop.page.PageContext;
import org.exoplatform.portal.mop.page.PageKey;
import org.exoplatform.portal.mop.service.LayoutService;
Expand Down Expand Up @@ -69,7 +69,7 @@ public class PageLayoutRest {
@GetMapping
@Operation(summary = "Retrieve pages", method = "GET", description = "This retrieves pages")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Request fulfilled"),
@ApiResponse(responseCode = "200", description = "Request fulfilled"),
})
public List<PageContext> getPages(
HttpServletRequest request,
Expand Down Expand Up @@ -104,12 +104,19 @@ public LayoutModel getPageLayout(
@Parameter(description = "page reference", required = true)
@RequestParam("pageRef")
String pageRef,
@Parameter(description = "Application Storage Id", required = false)
@RequestParam(name = "applicationId", required = false, defaultValue = "0")
long applicationId,
@Parameter(description = "expand options", required = true)
@RequestParam("expand")
String expand) {
try {
Page page = pageLayoutService.getPageLayout(PageKey.parse(pageRef), request.getRemoteUser());
return RestEntityBuilder.toLayoutModel(page, layoutService, expand);
ModelObject modelObject = applicationId > 0 ? pageLayoutService.getPageApplicationLayout(PageKey.parse(pageRef),
applicationId,
request.getRemoteUser()) :
pageLayoutService.getPageLayout(PageKey.parse(pageRef),
request.getRemoteUser());
return RestEntityBuilder.toLayoutModel(modelObject, layoutService, expand);
} catch (ObjectNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
} catch (IllegalAccessException e) {
Expand Down Expand Up @@ -187,7 +194,7 @@ public LayoutModel updatePageLayout(
RestEntityBuilder.fromLayoutModel(layoutModel),
publish.orElse(false).booleanValue(),
request.getRemoteUser());
return getPageLayout(request, pageRef, expand);
return getPageLayout(request, pageRef, 0, expand);
} catch (IllegalArgumentException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
} catch (ObjectNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
package io.meeds.layout.rest.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -173,14 +175,18 @@ private void init(ModelObject model) { // NOSONAR

@SuppressWarnings("unchecked")
ApplicationState<Portlet> state = application.getState();
if (state instanceof PersistentApplicationState<Portlet> persistentState) {
this.storageId = persistentState.getStorageId();
} else if (state instanceof CloneApplicationState<Portlet> persistentState) {
this.storageId = persistentState.getStorageId();
} else if (state instanceof TransientApplicationState<Portlet> transientState) {
this.contentId = transientState.getContentId();
} else {
throw new IllegalStateException("PortletInstance should either has a persistent or transient state");
switch (state) {
case PersistentApplicationState<Portlet> persistentState -> this.storageId = persistentState.getStorageId();
case CloneApplicationState<Portlet> persistentState -> this.storageId = persistentState.getStorageId();
case TransientApplicationState<Portlet> transientState -> {
this.contentId = transientState.getContentId();
Portlet portlet = transientState.getContentState();
this.preferences = portlet == null ? Collections.emptyList() :
StreamSupport.stream(portlet.spliterator(), false)
.map(p -> new PortletInstancePreference(p.getName(), p.getValue()))
.toList();
}
default -> throw new IllegalStateException("PortletInstance should either has a persistent or transient state");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ public static SiteEntity toSiteEntity(PortalConfig site,
locale);
}

public static LayoutModel toLayoutModel(Page page, LayoutService layoutService, String expand) {
LayoutModel layoutModel = new LayoutModel(page);
if (StringUtils.contains(expand, "contentId")) {
public static LayoutModel toLayoutModel(ModelObject modelObject,
LayoutService layoutService,
String expand) {
LayoutModel layoutModel = new LayoutModel(modelObject);
if (StringUtils.contains(expand, "contentId")
&& modelObject instanceof Container container) {
Map<String, String> contentIds = new HashMap<>();
computeApplicationContentId(layoutService, page.getChildren(), contentIds);
computeApplicationContentId(layoutService, container.getChildren(), contentIds);
applyApplicationContentId(layoutModel.getChildren(), contentIds);
}
return layoutModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
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.LocaleConfig;
import org.exoplatform.services.resources.LocaleConfigService;
import org.exoplatform.services.resources.ResourceBundleManager;
import org.exoplatform.web.WebAppController;
Expand Down Expand Up @@ -362,7 +363,8 @@ private NodeState buildNodeState(String label, // NOSONAR
private void saveNodeLabels(String nodeId, Map<String, String> labels) {
if (labels != null) {
Map<Locale, State> nodeLabels = new HashMap<>();
labels.entrySet().forEach(label -> nodeLabels.put(Locale.forLanguageTag(label.getKey()), new State(label.getValue(), null)));
labels.entrySet()
.forEach(label -> nodeLabels.put(Locale.forLanguageTag(label.getKey()), new State(label.getValue(), null)));
descriptionService.setDescriptions(nodeId, nodeLabels);
} else {
descriptionService.setDescriptions(nodeId, Collections.emptyMap());
Expand Down Expand Up @@ -426,10 +428,9 @@ public NodeLabel toNodeLabel(Map<Locale, State> nodeLabels) {
.filter(localeConfig -> !StringUtils.equals(localeConfig.getLocale()
.toLanguageTag(),
"ma"))
.collect(Collectors.toMap(c -> c.getLocale()
.toLanguageTag(),
localeConfig -> localeConfig.getLocale()
.getDisplayName()));
.map(LocaleConfig::getLocale)
.collect(Collectors.toMap(Locale::toLanguageTag,
Locale::getDisplayName));
Map<String, String> localized = new HashMap<>();
NodeLabel nodeLabel = new NodeLabel();
if (MapUtils.isNotEmpty(nodeLabels)) {
Expand All @@ -449,7 +450,8 @@ public NodeLabel toNodeLabel(Map<Locale, State> nodeLabels) {
}

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

}
Loading

0 comments on commit 59c21b5

Please sign in to comment.