Skip to content

Commit

Permalink
Merge Site Layout Editor - Meeds-io/MIPs#175 (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker authored Feb 6, 2025
2 parents 8b5cf25 + 5a7c2f5 commit d8af2b3
Show file tree
Hide file tree
Showing 178 changed files with 8,154 additions and 1,954 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.layout.rest.model;
package io.meeds.layout.model;

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

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -40,12 +38,15 @@
import org.exoplatform.portal.config.model.ModelObject;
import org.exoplatform.portal.config.model.ModelStyle;
import org.exoplatform.portal.config.model.Page;
import org.exoplatform.portal.config.model.PageBody;
import org.exoplatform.portal.config.model.PersistentApplicationState;
import org.exoplatform.portal.config.model.PortalConfig;
import org.exoplatform.portal.config.model.TransientApplicationState;
import org.exoplatform.portal.mop.page.PageKey;
import org.exoplatform.portal.pom.spi.portlet.Portlet;

import io.meeds.layout.model.PortletInstancePreference;
import io.meeds.layout.service.PortletInstanceService;
import io.meeds.layout.util.EntityMapper;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down Expand Up @@ -196,10 +197,14 @@ public class LayoutModel {
private String link;

public LayoutModel(ModelObject model) {
init(model);
this(model, null);
}

public LayoutModel(ModelObject model, PortletInstanceService portletInstanceService) {
init(model, portletInstanceService);
}

private void init(ModelObject model) { // NOSONAR
private void init(ModelObject model, PortletInstanceService portletInstanceService) { // NOSONAR
ModelStyle cssStyle = model.getCssStyle();
if (cssStyle != null) {
this.borderColor = cssStyle.getBorderColor();
Expand Down Expand Up @@ -240,7 +245,14 @@ private void init(ModelObject model) { // NOSONAR
this.textSubtitleFontStyle = cssStyle.getTextSubtitleFontStyle();
}

if (model instanceof Container container) {
if (model instanceof PageBody pageBody) {
this.storageId = pageBody.getStorageId();
this.storageName = pageBody.getStorageName();
this.width = pageBody.getWidth();
this.height = pageBody.getHeight();
this.cssClass = pageBody.getCssClass();
this.template = EntityMapper.PAGE_BODY_TEMPLATE;
} else if (model instanceof Container container) {
this.id = container.getId();
this.storageId = container.getStorageId();
this.storageName = container.getStorageName();
Expand All @@ -255,7 +267,10 @@ private void init(ModelObject model) { // NOSONAR
this.cssClass = container.getCssClass();
this.profiles = container.getProfiles();
this.accessPermissions = container.getAccessPermissions();
this.children = container.getChildren().stream().map(LayoutModel::new).toList();
this.children = container.getChildren()
.stream()
.map(c -> new LayoutModel(c, portletInstanceService))
.toList();

ApplicationBackgroundStyle appCssStyle = container.getAppBackgroundStyle();
if (appCssStyle != null) {
Expand Down Expand Up @@ -292,10 +307,20 @@ private void init(ModelObject model) { // NOSONAR
this.accessPermissions = application.getAccessPermissions();

ApplicationState state = application.getState();
if (portletInstanceService != null) {
portletInstanceService.expandPortletPreferences(application);
TransientApplicationState transientState = (TransientApplicationState) application.getState();
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();
}
switch (state) {
case PersistentApplicationState persistentState -> this.storageId = persistentState.getStorageId();
case CloneApplicationState persistentState -> this.storageId = persistentState.getStorageId();
case TransientApplicationState transientState -> {
case TransientApplicationState transientState when portletInstanceService == null -> {
this.contentId = transientState.getContentId();
Portlet portlet = transientState.getContentState();
this.preferences = portlet == null ? Collections.emptyList() :
Expand All @@ -322,148 +347,26 @@ public Page toPage() {
Page page = new Page(storageId);
ArrayList<ModelObject> pageContainers = this.children == null ? new ArrayList<>() :
this.children.stream()
.map(LayoutModel::toModelObject)
.map(EntityMapper::toModelObject)
.filter(Objects::nonNull)
.collect(Collectors.toCollection(ArrayList::new));
page.setChildren(pageContainers);
return page;
}

public static ModelObject toModelObject(LayoutModel layoutModel) { // NOSONAR
ModelStyle cssStyle = mapToStyle(layoutModel);

if (StringUtils.isNotBlank(layoutModel.template)) {
Container container = new Container(layoutModel.getStorageId());
container.setId(layoutModel.getId());
container.setStorageName(layoutModel.getStorageName());
container.setName(layoutModel.getName());
container.setIcon(layoutModel.getIcon());
container.setTemplate(layoutModel.getTemplate());
container.setFactoryId(layoutModel.getFactoryId());
container.setTitle(layoutModel.getTitle());
container.setDescription(layoutModel.getDescription());
container.setWidth(layoutModel.getWidth());
container.setHeight(layoutModel.getHeight());
container.setCssClass(layoutModel.getCssClass());
container.setProfiles(layoutModel.getProfiles());
container.setAccessPermissions(layoutModel.getAccessPermissions());
container.setCssStyle(cssStyle);
container.setAppBackgroundStyle(mapToAppStyle(layoutModel));
if (layoutModel.getChildren() != null) {
container.setChildren(layoutModel.getChildren()
.stream()
.map(LayoutModel::toModelObject)
.collect(Collectors.toCollection(ArrayList::new)));
}
return container;
} else { // NOSONAR
Application application = new Application(layoutModel.getStorageId());
application.setId(layoutModel.getId());
application.setStorageName(layoutModel.getStorageName());
application.setIcon(layoutModel.getIcon());
application.setTitle(layoutModel.getTitle());
application.setDescription(layoutModel.getDescription());
application.setWidth(layoutModel.getWidth());
application.setHeight(layoutModel.getHeight());
application.setCssClass(layoutModel.getCssClass());
application.setShowInfoBar(layoutModel.isShowInfoBar());
application.setShowApplicationState(layoutModel.isShowApplicationState());
application.setShowApplicationMode(layoutModel.isShowApplicationMode());
application.setAccessPermissions(layoutModel.getAccessPermissions());
application.setCssStyle(cssStyle);

ApplicationState state;
if (StringUtils.isNotBlank(layoutModel.getStorageId())) {
state = new PersistentApplicationState(layoutModel.getStorageId());
} else if (StringUtils.isNotBlank(layoutModel.getContentId())) {
TransientApplicationState transientState = new TransientApplicationState(layoutModel.getContentId());
transientState.setOwnerId(layoutModel.getOwnerId());
transientState.setOwnerType(layoutModel.getOwnerType());
if (CollectionUtils.isNotEmpty(layoutModel.getPreferences())) {
Portlet portlet = new Portlet();
layoutModel.getPreferences()
.forEach(p -> portlet.setValue(p.getName(), p.getValue()));
transientState.setContentState(portlet);
}
state = transientState;
} else {
throw new IllegalStateException("PortletInstance should either has a storageId or a contentId");
}
application.setState(state);
return application;
}
}

private static ModelStyle mapToStyle(LayoutModel layoutModel) {
ModelStyle cssStyle = null;
boolean hasStyle = StringUtils.isNotBlank(layoutModel.getBorderColor())
|| layoutModel.getRadiusTopRight() != null
|| layoutModel.getRadiusTopLeft() != null
|| layoutModel.getRadiusBottomLeft() != null
|| layoutModel.getRadiusBottomRight() != null
|| layoutModel.getMarginTop() != null
|| layoutModel.getMarginBottom() != null
|| layoutModel.getMarginLeft() != null
|| layoutModel.getMarginRight() != null
|| StringUtils.isNotBlank(layoutModel.getBorderSize())
|| StringUtils.isNotBlank(layoutModel.getBoxShadow())
|| StringUtils.isNotBlank(layoutModel.getBackgroundColor())
|| StringUtils.isNotBlank(layoutModel.getBackgroundImage())
|| StringUtils.isNotBlank(layoutModel.getTextTitleColor())
|| StringUtils.isNotBlank(layoutModel.getTextColor())
|| StringUtils.isNotBlank(layoutModel.getTextHeaderColor())
|| StringUtils.isNotBlank(layoutModel.getTextSubtitleColor());
if (hasStyle) {
cssStyle = new ModelStyle();
cssStyle.setBorderColor(layoutModel.getBorderColor());
cssStyle.setBorderSize(layoutModel.getBorderSize());
cssStyle.setBoxShadow(layoutModel.getBoxShadow());
cssStyle.setMarginTop(layoutModel.getMarginTop());
cssStyle.setMarginBottom(layoutModel.getMarginBottom());
cssStyle.setMarginRight(layoutModel.getMarginRight());
cssStyle.setMarginLeft(layoutModel.getMarginLeft());
cssStyle.setRadiusTopRight(layoutModel.getRadiusTopRight());
cssStyle.setRadiusTopLeft(layoutModel.getRadiusTopLeft());
cssStyle.setRadiusBottomRight(layoutModel.getRadiusBottomRight());
cssStyle.setRadiusBottomLeft(layoutModel.getRadiusBottomLeft());
cssStyle.setBackgroundColor(layoutModel.getBackgroundColor());
cssStyle.setBackgroundImage(layoutModel.getBackgroundImage());
cssStyle.setBackgroundEffect(layoutModel.getBackgroundEffect());
cssStyle.setBackgroundPosition(layoutModel.getBackgroundPosition());
cssStyle.setBackgroundSize(layoutModel.getBackgroundSize());
cssStyle.setBackgroundRepeat(layoutModel.getBackgroundRepeat());
cssStyle.setTextTitleColor(layoutModel.getTextTitleColor());
cssStyle.setTextTitleFontSize(layoutModel.getTextTitleFontSize());
cssStyle.setTextTitleFontWeight(layoutModel.getTextTitleFontWeight());
cssStyle.setTextTitleFontStyle(layoutModel.getTextTitleFontStyle());
cssStyle.setTextHeaderColor(layoutModel.getTextHeaderColor());
cssStyle.setTextHeaderFontSize(layoutModel.getTextHeaderFontSize());
cssStyle.setTextHeaderFontWeight(layoutModel.getTextHeaderFontWeight());
cssStyle.setTextHeaderFontStyle(layoutModel.getTextHeaderFontStyle());
cssStyle.setTextColor(layoutModel.getTextColor());
cssStyle.setTextFontSize(layoutModel.getTextFontSize());
cssStyle.setTextFontWeight(layoutModel.getTextFontWeight());
cssStyle.setTextFontStyle(layoutModel.getTextFontStyle());
cssStyle.setTextSubtitleColor(layoutModel.getTextSubtitleColor());
cssStyle.setTextSubtitleFontSize(layoutModel.getTextSubtitleFontSize());
cssStyle.setTextSubtitleFontWeight(layoutModel.getTextSubtitleFontWeight());
cssStyle.setTextSubtitleFontStyle(layoutModel.getTextSubtitleFontStyle());
}
return cssStyle;
}

private static ApplicationBackgroundStyle mapToAppStyle(LayoutModel layoutModel) {
ApplicationBackgroundStyle cssStyle = null;
if (StringUtils.isNotBlank(layoutModel.getAppBackgroundColor())
|| StringUtils.isNotBlank(layoutModel.getAppBackgroundImage())) {
cssStyle = new ApplicationBackgroundStyle();
cssStyle.setBackgroundColor(layoutModel.getAppBackgroundColor());
cssStyle.setBackgroundImage(layoutModel.getAppBackgroundImage());
cssStyle.setBackgroundEffect(layoutModel.getAppBackgroundEffect());
cssStyle.setBackgroundPosition(layoutModel.getAppBackgroundPosition());
cssStyle.setBackgroundSize(layoutModel.getAppBackgroundSize());
cssStyle.setBackgroundRepeat(layoutModel.getAppBackgroundRepeat());
public PortalConfig toSite() {
PortalConfig site = new PortalConfig(storageId);
ModelObject modelObject = this.children == null ? new PageBody() :
EntityMapper.toModelObject(this);
if (modelObject instanceof Container container) {
site.setPortalLayout(container);
} else {
Container container = new Container();
container.setChildren(new ArrayList<>());
container.getChildren().add(modelObject);
site.setPortalLayout(container);
}
return cssStyle;
return site;
}

}
Loading

0 comments on commit d8af2b3

Please sign in to comment.