Skip to content

Commit

Permalink
feat: Add Upgrade Plugin for application reference update - MEED-7019 -
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker committed Sep 5, 2024
1 parent 3dde6a6 commit 1eb2594
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 1 deletion.
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,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
@@ -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

0 comments on commit 1eb2594

Please sign in to comment.