diff --git a/layout-service/src/main/java/io/meeds/layout/listener/SiteCreatedFromTemplateListener.java b/layout-service/src/main/java/io/meeds/layout/listener/SiteCreatedFromTemplateListener.java new file mode 100644 index 00000000..34005e13 --- /dev/null +++ b/layout-service/src/main/java/io/meeds/layout/listener/SiteCreatedFromTemplateListener.java @@ -0,0 +1,53 @@ +/** + * This file is part of the Meeds project (https://meeds.io/). + * + * Copyright (C) 2020 - 2024 Meeds Association contact@meeds.io + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * 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.listener; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import org.exoplatform.portal.config.UserPortalConfigService; +import org.exoplatform.portal.mop.SiteKey; +import org.exoplatform.services.listener.Event; +import org.exoplatform.services.listener.ListenerBase; +import org.exoplatform.services.listener.ListenerService; + +import io.meeds.layout.service.PageLayoutService; + +import jakarta.annotation.PostConstruct; + +@Component +public class SiteCreatedFromTemplateListener implements ListenerBase { + + @Autowired + private ListenerService listenerService; + + @Autowired + private PageLayoutService pageLayoutService; + + @PostConstruct + public void init() { + listenerService.addListener(UserPortalConfigService.SITE_TEMPLATE_INSTANTIATED, this); + } + + @Override + public void onEvent(Event event) throws Exception { + pageLayoutService.impersonateSite(event.getData()); + } + +} diff --git a/layout-service/src/main/java/io/meeds/layout/service/PageLayoutService.java b/layout-service/src/main/java/io/meeds/layout/service/PageLayoutService.java index 880dccf3..9b342722 100644 --- a/layout-service/src/main/java/io/meeds/layout/service/PageLayoutService.java +++ b/layout-service/src/main/java/io/meeds/layout/service/PageLayoutService.java @@ -139,6 +139,20 @@ public Application getPageApplicationLayout(PageKey pageKey, return application; } + public void impersonateSite(SiteKey siteKey) { + PortalConfig portalConfig = layoutService.getPortalConfig(siteKey); + impersonateModel(portalConfig.getPortalLayout()); + List pages = layoutService.findPages(siteKey); + if (CollectionUtils.isNotEmpty(pages)) { + pages.forEach(page -> impersonatePage(page.getKey())); + } + } + + public void impersonatePage(PageKey pageKey) { + Page page = getPageLayout(pageKey); + impersonateModel(page); + } + public Page getPageLayout(PageKey pageKey, String username) throws ObjectNotFoundException, IllegalAccessException { Page page = getPageLayout(pageKey); if (page == null) { @@ -462,6 +476,21 @@ private void computeApplicationPreferences(Application application, String usern application.setState(applicationState); } + @SneakyThrows + private void impersonateModel(ModelObject object) { + if (object instanceof Container container) { + ArrayList children = container.getChildren(); + if (CollectionUtils.isNotEmpty(children)) { + children.forEach(this::impersonateModel); + } + } else if (object instanceof Application application) { + Portlet preferences = portletInstanceService.exportApplicationPreferences(application); + if (preferences != null) { + layoutService.save(application.getState(), preferences); + } + } + } + private Portlet getApplicationPreferences(long applicationId, String username) throws IllegalAccessException, ObjectNotFoundException { List preferences = portletInstanceService.getApplicationPreferences(applicationId, username); diff --git a/layout-service/src/main/java/io/meeds/layout/service/PortletInstanceService.java b/layout-service/src/main/java/io/meeds/layout/service/PortletInstanceService.java index 79e86fd5..40dde921 100644 --- a/layout-service/src/main/java/io/meeds/layout/service/PortletInstanceService.java +++ b/layout-service/src/main/java/io/meeds/layout/service/PortletInstanceService.java @@ -25,6 +25,7 @@ import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; @@ -36,6 +37,7 @@ import org.exoplatform.portal.config.UserACL; import org.exoplatform.portal.config.model.Application; import org.exoplatform.portal.pom.spi.portlet.Portlet; +import org.exoplatform.portal.pom.spi.portlet.Preference; import org.exoplatform.services.listener.ListenerService; import org.exoplatform.services.log.ExoLogger; import org.exoplatform.services.log.Log; @@ -347,6 +349,23 @@ public List getPortletInstances() { return portletInstanceStorage.getPortletInstances(); } + public Portlet exportApplicationPreferences(Application application) { + String portletName = portletInstanceLayoutStorage.getApplicationPortletName(application); + if (preferencePlugins.containsKey(portletName)) { + PortletInstancePreferencePlugin plugin = preferencePlugins.get(portletName); + Portlet preferences = portletInstanceLayoutStorage.getApplicationPreferences(Long.parseLong(application.getStorageId())); + List exportedPreferences = plugin.generatePreferences(application, preferences); + Map preferencesMap = exportedPreferences.stream() + .collect(Collectors.toMap(PortletInstancePreference::getName, + p -> new Preference(p.getName(), + p.getValue(), + false))); + return new Portlet(preferencesMap); + } else { + return null; + } + } + private void deletePortletInstanceFromStore(long id) throws ObjectNotFoundException { try { attachmentService.deleteAttachments(PortletInstanceAttachmentPlugin.OBJECT_TYPE, String.valueOf(id));