-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Upgrade Plugin for application reference update - MEED-7019 -
- Loading branch information
Showing
4 changed files
with
276 additions
and
1 deletion.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
layout-service/src/main/java/io/meeds/layout/model/ApplicationReferenceUpgrade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
...src/main/java/io/meeds/layout/plugin/upgrade/LayoutApplicationReferenceUpgradePlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
} |
114 changes: 114 additions & 0 deletions
114
...test/java/io/meeds/layout/plugin/upgrade/LayoutApplicationReferenceUpgradePluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters