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 Contribute Site Review - Meeds-io/MIPs#155 #209

Merged
merged 3 commits into from
Sep 5, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* 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.model;

import org.apache.commons.lang3.StringUtils;

import lombok.Data;

@Data
public class ApplicationReferenceUpgrade {

private String modificationType = ApplicationReferenceModificationType.UPDATE.name();

private String oldContentId;

private String newContentId;

public boolean isModification() {
return StringUtils.equalsIgnoreCase(ApplicationReferenceModificationType.UPDATE.name(), modificationType);
}

public boolean isRemoval() {
return StringUtils.equalsIgnoreCase(ApplicationReferenceModificationType.REMOVE.name(), modificationType);
}

public enum ApplicationReferenceModificationType {
REMOVE, UPDATE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* 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.model;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PortletPreferenceList {

private List<PortletInstancePreference> preferences;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* 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.plugin.upgrade;

import java.util.List;

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

import org.exoplatform.commons.api.settings.SettingService;
import org.exoplatform.commons.upgrade.UpgradeProductPlugin;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.portal.mop.dao.WindowDAO;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;

import io.meeds.common.ContainerTransactional;
import io.meeds.layout.model.ApplicationReferenceUpgrade;
import io.meeds.layout.model.PortletInstance;
import io.meeds.layout.service.PortletInstanceService;

import lombok.SneakyThrows;

public class LayoutApplicationReferenceUpgradePlugin extends UpgradeProductPlugin {

private static final String ENABLED_PARAM = "enabled";

private static final Log LOG = ExoLogger.getLogger(LayoutApplicationReferenceUpgradePlugin.class);

private PortletInstanceService portletInstanceService;

private WindowDAO windowDAO;

private List<ApplicationReferenceUpgrade> upgrades;

private boolean enabled;

public LayoutApplicationReferenceUpgradePlugin(SettingService settingService,
PortletInstanceService portletInstanceService,
WindowDAO windowDAO,
InitParams initParams) {
super(settingService, initParams);
this.portletInstanceService = portletInstanceService;
this.windowDAO = windowDAO;
this.upgrades = initParams.getObjectParamValues(ApplicationReferenceUpgrade.class);
this.enabled = !initParams.containsKey(ENABLED_PARAM)
|| Boolean.parseBoolean(initParams.getValueParam(ENABLED_PARAM).getValue());
}

@Override
public void processUpgrade(String oldVersion, String newVersion) {
long start = System.currentTimeMillis();
LOG.info("Start:: Upgrade Application ContentId {}", getName());
upgradeApplications();
LOG.info("End:: Upgrade Application ContentId {} in {} ms", getName(), System.currentTimeMillis() - start);
}

@Override
public boolean shouldProceedToUpgrade(String newVersion, String previousVersion) {
return enabled && CollectionUtils.isNotEmpty(upgrades);
}

@SneakyThrows
@ContainerTransactional
public void upgradeApplications() {
List<PortletInstance> portletInstances = portletInstanceService.getPortletInstances();

for (ApplicationReferenceUpgrade applicationModification : upgrades) {
String oldContentId = applicationModification.getOldContentId();
String newContentId = applicationModification.getNewContentId();
PortletInstance portletInstance = portletInstances.stream()
.filter(p -> StringUtils.equals(oldContentId, p.getContentId()))
.findFirst()
.orElse(null);

int modifiedLines = 0;
if (applicationModification.isModification()) {
modifiedLines = windowDAO.updateContentId(oldContentId, newContentId);
if (portletInstance != null) {
portletInstance.setContentId(newContentId);
portletInstanceService.updatePortletInstance(portletInstance);
}
} else if (applicationModification.isRemoval()) {
modifiedLines = windowDAO.deleteByContentId(oldContentId);
if (portletInstance != null) {
portletInstanceService.deletePortletInstance(portletInstance.getId());
}
}

if (modifiedLines > 0) {
LOG.info("| -- UPDATE::Application Reference: {} items migrated from '{}' to '{}' successfully",
modifiedLines,
oldContentId,
newContentId);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import io.meeds.layout.model.PageCreateModel;
import io.meeds.layout.model.PermissionUpdateModel;
import io.meeds.layout.model.PortletPreferenceList;
import io.meeds.layout.rest.model.LayoutModel;
import io.meeds.layout.rest.util.RestEntityBuilder;
import io.meeds.layout.service.PageLayoutService;
Expand Down Expand Up @@ -254,4 +255,32 @@ public void updatePagePermissions(
}
}

@PatchMapping("application/preferences")
@Secured("users")
@Operation(summary = "Update a page application preferences", method = "PATCH",
description = "This updates a given application preferences added in an existing page")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Request fulfilled"),
@ApiResponse(responseCode = "403", description = "Forbidden"),
@ApiResponse(responseCode = "404", description = "Not found"),
})
public void updatePageApplicationPreferences(
HttpServletRequest request,
@Parameter(description = "Page reference", required = true)
@RequestParam("pageRef")
String pageRef,
@Parameter(description = "Application storage identifier", required = true)
@RequestParam("applicationId")
long applicationId,
@RequestBody
PortletPreferenceList portletPreferenceList) {
try {
pageLayoutService.updatePageApplicationPreferences(PageKey.parse(pageRef), applicationId, portletPreferenceList.getPreferences(), request.getRemoteUser());
} catch (ObjectNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
} catch (IllegalAccessException e) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN, e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,27 @@ public PageKey clonePage(PageKey pageKey,
return page.getPageKey();
}

public void updatePageApplicationPreferences(PageKey pageKey,
long applicationId,
List<PortletInstancePreference> preferences,
String username) throws IllegalAccessException, ObjectNotFoundException {
Page page = layoutService.getPage(pageKey);
if (page == null) {
throw new ObjectNotFoundException(String.format(PAGE_NOT_EXISTS_MESSAGE, pageKey.format()));
} else if (!aclService.canEditPage(pageKey, username)) {
throw new IllegalAccessException(String.format(PAGE_NOT_EDITABLE_MESSAGE, pageKey.format(), username));
}
Application<Portlet> application = findApplication(page, applicationId);
if (application == null) {
throw new ObjectNotFoundException(String.format("Application with id %s wasn't found in page %s",
applicationId,
pageKey));
}
Portlet portletPreferences = new Portlet();
preferences.forEach(preference -> portletPreferences.setValue(preference.getName(), preference.getValue()));
layoutService.save(application.getState(), portletPreferences);
}

public PageContext updatePageLayout(String pageRef,
Page page,
boolean publish,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* 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.plugin.upgrade;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import org.exoplatform.commons.api.settings.SettingService;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.portal.mop.dao.WindowDAO;

import io.meeds.layout.model.ApplicationReferenceUpgrade;
import io.meeds.layout.model.PortletInstance;
import io.meeds.layout.service.PortletInstanceService;

import lombok.SneakyThrows;

@ExtendWith(MockitoExtension.class)
public class LayoutApplicationReferenceUpgradePluginTest {

@Mock
private SettingService settingService;

@Mock
private PortletInstanceService portletInstanceService;

@Mock
private WindowDAO windowDAO;

@Mock
private InitParams initParams;

@Mock
private ApplicationReferenceUpgrade applicationModification;

@Mock
private PortletInstance portletInstance;

private String oldContentId = "oldApp/oldContentId";

private String newContentId = "newApp/newContentId";

@Test
@SneakyThrows
public void processUpgradeWithModification() {
when(initParams.getObjectParamValues(ApplicationReferenceUpgrade.class)).thenReturn(Collections.singletonList(applicationModification));
when(portletInstanceService.getPortletInstances()).thenReturn(Collections.singletonList(portletInstance));
when(portletInstance.getContentId()).thenReturn(oldContentId);
when(applicationModification.isModification()).thenReturn(true);
when(applicationModification.getOldContentId()).thenReturn(oldContentId);
when(applicationModification.getNewContentId()).thenReturn(newContentId);

LayoutApplicationReferenceUpgradePlugin applicationReferenceUpgradePlugin =
new LayoutApplicationReferenceUpgradePlugin(settingService,
portletInstanceService,
windowDAO,
initParams);
assertTrue(applicationReferenceUpgradePlugin.shouldProceedToUpgrade(null, null));

applicationReferenceUpgradePlugin.processUpgrade(null, null);

verify(windowDAO).updateContentId(oldContentId, newContentId);
verify(portletInstanceService).updatePortletInstance(portletInstance);
}

@Test
@SneakyThrows
public void processUpgradeWithRemoval() {
when(initParams.getObjectParamValues(ApplicationReferenceUpgrade.class)).thenReturn(Collections.singletonList(applicationModification));
when(portletInstanceService.getPortletInstances()).thenReturn(Collections.singletonList(portletInstance));
when(portletInstance.getContentId()).thenReturn(oldContentId);
when(applicationModification.isRemoval()).thenReturn(true);
when(applicationModification.getOldContentId()).thenReturn(oldContentId);
when(applicationModification.getNewContentId()).thenReturn(newContentId);
when(portletInstance.getId()).thenReturn(2l);

LayoutApplicationReferenceUpgradePlugin applicationReferenceUpgradePlugin =
new LayoutApplicationReferenceUpgradePlugin(settingService,
portletInstanceService,
windowDAO,
initParams);
assertTrue(applicationReferenceUpgradePlugin.shouldProceedToUpgrade(null, null));

applicationReferenceUpgradePlugin.processUpgrade(null, null);

verify(windowDAO).deleteByContentId(oldContentId);
verify(portletInstanceService).deletePortletInstance(2l);
}

}
3 changes: 2 additions & 1 deletion layout-webapp/src/main/resources/layout.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

spring.liquibase.change-log=classpath:changelog/layout-rdbms.db.changelog-master.xml
spring.liquibase.change-log=classpath:changelog/layout-rdbms.db.changelog-master.xml
meeds.portlets.import.version=3
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
<template #label>
<div class="d-flex full-width align-center">
<span class="text-font-size">{{ $t('layout.fixedHeightCustom') }}</span>
<layout-editor-number-input
<number-input
v-model="height"
v-if="height === customHeight"
:label="$t('layout.fixedHeight')"
Expand Down
Loading