Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Meeds-io/MIPs#131 - Dev Meed #51

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion layout-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<rest.api.doc.version>1.0</rest.api.doc.version>
<rest.api.doc.description>Layout Rest endpoints</rest.api.doc.description>

<exo.test.coverage.ratio>0.60</exo.test.coverage.ratio>
<exo.test.coverage.ratio>0.65</exo.test.coverage.ratio>
</properties>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@
import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.portal.config.model.PortalConfig;
import org.exoplatform.portal.mop.SiteKey;
import org.exoplatform.social.rest.entity.SiteEntity;

import io.meeds.layout.model.PermissionUpdateModel;
import io.meeds.layout.model.SiteCreateModel;
import io.meeds.layout.model.SiteUpdateModel;
import io.meeds.layout.rest.model.SiteRestEntity;
import io.meeds.layout.rest.util.RestEntityBuilder;
import io.meeds.layout.service.PageLayoutService;
import io.meeds.layout.service.SiteLayoutService;

import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -67,24 +66,21 @@ public class SiteLayoutRest {
@Autowired
private SiteLayoutService siteLayoutService;

@Autowired
private PageLayoutService pageLayoutService;

@GetMapping("{siteId}")
@Operation(summary = "Gets a specific site by its id", description = "Gets site by id", method = "GET")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Request fulfilled"),
@ApiResponse(responseCode = "403", description = "Forbidden"),
@ApiResponse(responseCode = "500", description = "Internal server error"),
})
public ResponseEntity<SiteRestEntity> getSiteById(
HttpServletRequest request,
@Parameter(description = "site id")
@PathVariable("siteId")
long siteId,
@Parameter(description = "Language used to retrieve names", required = false)
@RequestParam(name = "lang", required = false)
String lang) throws Exception {
public ResponseEntity<SiteEntity> getSiteById(
HttpServletRequest request,
@Parameter(description = "site id")
@PathVariable("siteId")
long siteId,
@Parameter(description = "Language used to retrieve names", required = false)
@RequestParam(name = "lang", required = false)
String lang) throws Exception {
try {
PortalConfig site = siteLayoutService.getSite(siteId, request.getRemoteUser());
Locale locale;
Expand All @@ -93,10 +89,9 @@ public ResponseEntity<SiteRestEntity> getSiteById(
} else {
locale = Locale.forLanguageTag(lang);
}
SiteRestEntity siteEntity = RestEntityBuilder.toSiteEntity(pageLayoutService,
site,
request,
locale);
SiteEntity siteEntity = RestEntityBuilder.toSiteEntity(site,
request,
locale);
return ResponseEntity.ok()
.eTag(String.valueOf(Objects.hash(siteEntity, locale)))
.body(siteEntity);
Expand All @@ -114,17 +109,17 @@ public ResponseEntity<SiteRestEntity> getSiteById(
@ApiResponse(responseCode = "403", description = "Forbidden"),
@ApiResponse(responseCode = "500", description = "Internal server error"),
})
public ResponseEntity<SiteRestEntity> getSite(
HttpServletRequest request,
@Parameter(description = "site type")
@RequestParam("siteType")
String siteType,
@Parameter(description = "site name")
@RequestParam("siteName")
String siteName,
@Parameter(description = "Language used to retrieve names", required = false)
@RequestParam(name = "lang", required = false)
String lang) throws Exception {
public ResponseEntity<SiteEntity> getSite(
HttpServletRequest request,
@Parameter(description = "site type")
@RequestParam("siteType")
String siteType,
@Parameter(description = "site name")
@RequestParam("siteName")
String siteName,
@Parameter(description = "Language used to retrieve names", required = false)
@RequestParam(name = "lang", required = false)
String lang) throws Exception {
try {
PortalConfig site = siteLayoutService.getSite(new SiteKey(siteType, siteName), request.getRemoteUser());
Locale locale;
Expand All @@ -133,10 +128,9 @@ public ResponseEntity<SiteRestEntity> getSite(
} else {
locale = Locale.forLanguageTag(lang);
}
SiteRestEntity siteEntity = RestEntityBuilder.toSiteEntity(pageLayoutService,
site,
request,
locale);
SiteEntity siteEntity = RestEntityBuilder.toSiteEntity(site,
request,
locale);
return ResponseEntity.ok()
.eTag(String.valueOf(Objects.hash(siteEntity, locale)))
.body(siteEntity);
Expand Down Expand Up @@ -214,17 +208,16 @@ public void updateSitePermissions(
@Operation(summary = "create a site", method = "POST", description = "This create a new site")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Request fulfilled"),
@ApiResponse(responseCode = "500", description = "Internal server error"), })
public ResponseEntity<SiteRestEntity> createSite(
HttpServletRequest request,
@Parameter(description = "site to create", required = true)
@RequestBody
SiteCreateModel createModel) throws Exception {
public ResponseEntity<SiteEntity> createSite(
HttpServletRequest request,
@Parameter(description = "site to create", required = true)
@RequestBody
SiteCreateModel createModel) throws Exception {
try {
PortalConfig site = siteLayoutService.createSite(createModel, request.getRemoteUser());
SiteRestEntity siteEntity = RestEntityBuilder.toSiteEntity(pageLayoutService,
site,
request,
request.getLocale());
SiteEntity siteEntity = RestEntityBuilder.toSiteEntity(site,
request,
request.getLocale());
return ResponseEntity.ok()
.eTag(String.valueOf(Objects.hash(siteEntity, request.getLocale())))
.body(siteEntity);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,11 @@
import org.exoplatform.portal.config.model.ModelObject;
import org.exoplatform.portal.config.model.Page;
import org.exoplatform.portal.config.model.PortalConfig;
import org.exoplatform.portal.mop.page.PageKey;
import org.exoplatform.portal.mop.rest.model.UserNodeRestEntity;
import org.exoplatform.portal.mop.service.LayoutService;
import org.exoplatform.social.rest.api.EntityBuilder;
import org.exoplatform.social.rest.entity.SiteEntity;

import io.meeds.layout.rest.model.LayoutModel;
import io.meeds.layout.rest.model.SiteRestEntity;
import io.meeds.layout.service.PageLayoutService;

import jakarta.servlet.http.HttpServletRequest;

Expand All @@ -50,39 +46,17 @@ public class RestEntityBuilder {
private RestEntityBuilder() {
}

public static SiteRestEntity toSiteEntity(PageLayoutService pageLayoutService,
PortalConfig site,
HttpServletRequest request,
Locale locale) throws Exception {
SiteEntity siteEntity = EntityBuilder.buildSiteEntity(site,
request,
true,
null,
true,
false,
false,
locale);
SiteRestEntity siteRestEntity = new SiteRestEntity(siteEntity);
List<UserNodeRestEntity> siteNavigations = siteEntity.getSiteNavigations();
computeCompatibilityWithEditor(pageLayoutService, siteRestEntity, siteNavigations);
return siteRestEntity;
}

private static void computeCompatibilityWithEditor(PageLayoutService pageLayoutService,
SiteRestEntity siteRestEntity,
List<UserNodeRestEntity> siteNavigations) {
if (CollectionUtils.isNotEmpty(siteNavigations)) {
siteNavigations.forEach(n -> {
PageKey pageKey = n.getPageKey();
if (n.isCanEditPage()) {
Page page = pageLayoutService.getPageLayout(pageKey);
siteRestEntity.getPagesCompatibility().put(pageKey.format(), page != null && isCompatibleWithEditor(page));
} else if (pageKey != null) {
siteRestEntity.getPagesCompatibility().put(pageKey.format(), false);
}
computeCompatibilityWithEditor(pageLayoutService, siteRestEntity, n.getChildren());
});
}
public static SiteEntity toSiteEntity(PortalConfig site,
HttpServletRequest request,
Locale locale) throws Exception {
return EntityBuilder.buildSiteEntity(site,
request,
true,
null,
true,
false,
false,
locale);
}

public static LayoutModel toLayoutModel(Page page, LayoutService layoutService, String expand) {
Expand All @@ -106,10 +80,10 @@ private static void computeApplicationContentId(LayoutService layoutService,
if (layoutModel instanceof Container container) {
computeApplicationContentId(layoutService, container.getChildren(), contentIds);
} else if (layoutModel instanceof Application application) { // NOSONAR
String storageId = application.getStorageId();
if (StringUtils.isNotBlank(storageId)) {
String portletId = application.getStorageId() == null ? application.getId() : application.getStorageId();
if (StringUtils.isNotBlank(portletId)) {
String contentId = layoutService.getId(application.getState());
contentIds.put(storageId, contentId);
contentIds.put(portletId, contentId);
}
}
}
Expand All @@ -133,43 +107,4 @@ public static Page fromLayoutModel(LayoutModel layoutModel) {
return layoutModel.toPage();
}

private static boolean isCompatibleWithEditor(Page page) { // NOSONAR
ArrayList<ModelObject> children = page.getChildren();
if (CollectionUtils.isEmpty(children)) {
return true;
} else {
if (children.size() != 1) {
return false;
}
ModelObject parentContainer = children.get(0);
if (parentContainer != null
&& parentContainer instanceof Container appContainer
&& StringUtils.contains(appContainer.getTemplate(), "UIPageLayout.gtmpl")) {
return isChildrenOfTypeSection(appContainer);
} else if (parentContainer == null
|| !(parentContainer instanceof Container vAppContainer)
|| !StringUtils.contains(vAppContainer.getCssClass(), "VuetifyApp")) {
return false;
} else if (CollectionUtils.isEmpty(vAppContainer.getChildren())) {
return true;
} else {
parentContainer = vAppContainer.getChildren().get(0);
if (parentContainer == null
|| !(parentContainer instanceof Container appContainer)
|| !StringUtils.contains(appContainer.getCssClass(), "v-application")) {
return false;
}
return isChildrenOfTypeSection(appContainer);
}
}
}

private static boolean isChildrenOfTypeSection(Container appContainer) {
return appContainer.getChildren()
.stream()
.allMatch(c -> c instanceof Container container
&& (StringUtils.equals("GridContainer", container.getTemplate())
|| StringUtils.equals("FlexContainer", container.getTemplate())));
}

}
Loading
Loading