From 243e1d35f234ceb33b1f18f3d2c4f948bb8ccd12 Mon Sep 17 00:00:00 2001 From: Sofien Haj Chedhli Date: Fri, 5 Jan 2024 11:12:58 +0100 Subject: [PATCH] fix: Enable the import documents notification plugin by default after migrating to version 6.5 - EXO-68085 (#182) Prior to this change, after upgrading from version 6.4 to version 6.5, the import documents notification was disabled by default. This upgrade will enable the import documents notification by default after migrating to version 6.5. --- ...ortDocumentsNotificationUpgradePlugin.java | 130 ++++++++++++++++++ .../resources/conf/portal/configuration.xml | 39 ++++++ ...ocumentsNotificationUpgradePluginTest.java | 85 ++++++++++++ 3 files changed, 254 insertions(+) create mode 100644 data-upgrade-notifications/src/main/java/org/exoplatform/portal/upgrade/notification/ImportDocumentsNotificationUpgradePlugin.java create mode 100644 data-upgrade-notifications/src/test/java/org/exoplatform/portal/upgrade/notification/ImportDocumentsNotificationUpgradePluginTest.java diff --git a/data-upgrade-notifications/src/main/java/org/exoplatform/portal/upgrade/notification/ImportDocumentsNotificationUpgradePlugin.java b/data-upgrade-notifications/src/main/java/org/exoplatform/portal/upgrade/notification/ImportDocumentsNotificationUpgradePlugin.java new file mode 100644 index 000000000..3dc5821e4 --- /dev/null +++ b/data-upgrade-notifications/src/main/java/org/exoplatform/portal/upgrade/notification/ImportDocumentsNotificationUpgradePlugin.java @@ -0,0 +1,130 @@ +package org.exoplatform.portal.upgrade.notification; + +import java.util.List; +import java.util.Optional; + +import org.exoplatform.commons.api.notification.model.PluginInfo; +import org.exoplatform.commons.api.notification.model.UserSetting; +import org.exoplatform.commons.api.notification.service.setting.PluginSettingService; +import org.exoplatform.commons.api.notification.service.setting.UserSettingService; +import org.exoplatform.commons.api.settings.SettingService; +import org.exoplatform.commons.api.settings.data.Context; +import org.exoplatform.commons.persistence.impl.EntityManagerService; +import org.exoplatform.commons.upgrade.UpgradePluginExecutionContext; +import org.exoplatform.commons.upgrade.UpgradeProductPlugin; +import org.exoplatform.container.ExoContainer; +import org.exoplatform.container.ExoContainerContext; +import org.exoplatform.container.xml.InitParams; +import org.exoplatform.services.log.ExoLogger; +import org.exoplatform.services.log.Log; + +public class ImportDocumentsNotificationUpgradePlugin extends UpgradeProductPlugin { + + private static final Log LOG = ExoLogger.getLogger(ImportDocumentsNotificationUpgradePlugin.class); + + private static final String NOTIFICATION_PLUGIN = "notification.plugin.type"; + + private final SettingService settingService; + + private final UserSettingService userSettingService; + + private final PluginSettingService pluginSettingService; + + private final EntityManagerService entityManagerService; + + private String notificationPlugin; + + public ImportDocumentsNotificationUpgradePlugin(SettingService settingService, + UserSettingService userSettingService, + PluginSettingService pluginSettingService, + EntityManagerService entityManagerService, + InitParams initParams) { + super(settingService, initParams); + this.settingService = settingService; + this.userSettingService = userSettingService; + this.pluginSettingService = pluginSettingService; + this.entityManagerService = entityManagerService; + if (initParams.containsKey(NOTIFICATION_PLUGIN)) { + notificationPlugin = initParams.getValueParam(NOTIFICATION_PLUGIN).getValue(); + } + } + + @Override + public boolean shouldProceedToUpgrade(String newVersion, + String previousGroupVersion, + UpgradePluginExecutionContext previousUpgradePluginExecution) { + int executionCount = previousUpgradePluginExecution == null ? 0 : previousUpgradePluginExecution.getExecutionCount(); + return !isExecuteOnlyOnce() || executionCount == 0; + } + + @Override + public void processUpgrade(String s, String s1) { + if (notificationPlugin == null || notificationPlugin.isEmpty()) { + LOG.error("Couldn't process upgrade, the parameter '{}' is mandatory", NOTIFICATION_PLUGIN); + return; + } + ExoContainer currentContainer = ExoContainerContext.getCurrentContainer(); + int pageSize = 20; + int current = 0; + try { + LOG.info("=== Start initialisation of {} settings", notificationPlugin); + LOG.info(" Starting activating {} Notifications for users", notificationPlugin); + + Optional optionalPluginTypeConfig = + pluginSettingService.getAllPlugins() + .stream() + .filter(pluginInfo -> pluginInfo.getType().equals(notificationPlugin)) + .findFirst(); + PluginInfo pluginTypeConfig; + if (optionalPluginTypeConfig.isPresent()) { + pluginTypeConfig = optionalPluginTypeConfig.get(); + } else { + LOG.error("Couldn't process upgrade, the '{}' plugin is missing or not found", notificationPlugin); + return; + } + List usersContexts; + entityManagerService.startRequest(currentContainer); + long startTime = System.currentTimeMillis(); + do { + LOG.info(" Progression of users {} Notifications settings initialisation : {} users", notificationPlugin, current); + // Get all users who already update their notification settings + usersContexts = settingService.getContextNamesByType(Context.USER.getName(), current, pageSize); + if (usersContexts != null) { + for (String userName : usersContexts) { + try { + entityManagerService.endRequest(currentContainer); + entityManagerService.startRequest(currentContainer); + UserSetting userSetting = this.userSettingService.get(userName); + if (userSetting != null) { + updateSetting(userSetting, pluginTypeConfig); + userSettingService.save(userSetting); + } + } catch (Exception e) { + LOG.error(" Error while activating {} Notifications for user {} ", notificationPlugin, userName, e); + } + } + current += usersContexts.size(); + } + } while (usersContexts != null && !usersContexts.isEmpty()); + long endTime = System.currentTimeMillis(); + LOG.info(" Users {} Notifications settings initialised in {} ms", notificationPlugin, (endTime - startTime)); + } catch (Exception e) { + LOG.error("Error while initialisation of users {} Notifications settings - Cause :", notificationPlugin, e.getMessage(), e); + } finally { + entityManagerService.endRequest(currentContainer); + } + LOG.info("=== {} users with modified notifications settings have been found and processed successfully", current); + LOG.info("=== End initialisation of {} Notifications settings", notificationPlugin); + } + private void updateSetting(UserSetting userSetting, PluginInfo config) { + for (String defaultConf : config.getDefaultConfig()) { + for (String channelId : userSetting.getChannelActives()) { + if (UserSetting.FREQUENCY.getFrequecy(defaultConf) == UserSetting.FREQUENCY.INSTANTLY) { + userSetting.addChannelPlugin(channelId, config.getType()); + } else { + userSetting.addPlugin(config.getType(), UserSetting.FREQUENCY.getFrequecy(defaultConf)); + } + } + } + } +} diff --git a/data-upgrade-notifications/src/main/resources/conf/portal/configuration.xml b/data-upgrade-notifications/src/main/resources/conf/portal/configuration.xml index 1b7a501bf..2e3959ded 100644 --- a/data-upgrade-notifications/src/main/resources/conf/portal/configuration.xml +++ b/data-upgrade-notifications/src/main/resources/conf/portal/configuration.xml @@ -65,6 +65,45 @@ + + + ImportDocumentsNotificationUpgradePlugin + addUpgradePlugin + org.exoplatform.portal.upgrade.notification.ImportDocumentsNotificationUpgradePlugin + Import Documents Notification Upgrade Plugin + + + product.group.id + The groupId of the product + org.exoplatform.platform + + + plugin.execution.order + The plugin execution order + 1 + + + plugin.upgrade.execute.once + Execute this upgrade plugin only once + true + + + plugin.upgrade.target.version + Target version of the plugin + 6.6.0 + + + plugin.upgrade.async.execution + Execute this upgrade asynchronously + true + + + notification.plugin.type + the notification plugin type + ImportDocumentsPlugin + + + diff --git a/data-upgrade-notifications/src/test/java/org/exoplatform/portal/upgrade/notification/ImportDocumentsNotificationUpgradePluginTest.java b/data-upgrade-notifications/src/test/java/org/exoplatform/portal/upgrade/notification/ImportDocumentsNotificationUpgradePluginTest.java new file mode 100644 index 000000000..79c60b4c2 --- /dev/null +++ b/data-upgrade-notifications/src/test/java/org/exoplatform/portal/upgrade/notification/ImportDocumentsNotificationUpgradePluginTest.java @@ -0,0 +1,85 @@ +package org.exoplatform.portal.upgrade.notification; + +import static org.mockito.Mockito.*; + +import java.util.*; + +import org.junit.AfterClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.junit.MockitoJUnitRunner; + +import org.exoplatform.commons.api.notification.model.PluginInfo; +import org.exoplatform.commons.api.notification.model.UserSetting; +import org.exoplatform.commons.api.notification.service.setting.PluginSettingService; +import org.exoplatform.commons.api.notification.service.setting.UserSettingService; +import org.exoplatform.commons.api.settings.SettingService; +import org.exoplatform.commons.api.settings.data.Context; +import org.exoplatform.commons.persistence.impl.EntityManagerService; +import org.exoplatform.container.ExoContainerContext; +import org.exoplatform.container.PortalContainer; +import org.exoplatform.container.xml.InitParams; +import org.exoplatform.container.xml.ValueParam; + +@RunWith(MockitoJUnitRunner.class) +public class ImportDocumentsNotificationUpgradePluginTest { + + private static final MockedStatic EXO_CONTAINER_CONTEXT = mockStatic(ExoContainerContext.class); + + private static final MockedStatic PORTAL_CONTAINER = mockStatic(PortalContainer.class); + + @Mock + private EntityManagerService entityManagerService; + + @Mock + private SettingService settingService; + + @Mock + private UserSettingService userSettingService; + + @Mock + private PluginSettingService pluginSettingService; + + @AfterClass + public static void afterRunBare() throws Exception { // NOSONAR + EXO_CONTAINER_CONTEXT.close(); + PORTAL_CONTAINER.close(); + } + + @Test + public void processUpgrade() throws Exception { + InitParams initParams = new InitParams(); + ValueParam valueParam = new ValueParam(); + valueParam.setName("product.group.id"); + valueParam.setValue("org.exoplatform.platform"); + initParams.addParameter(valueParam); + valueParam.setName("notification.plugin.type"); + valueParam.setValue("ImportDocumentsPlugin"); + initParams.addParam(valueParam); + PortalContainer container = mock(PortalContainer.class); + EXO_CONTAINER_CONTEXT.when(() -> ExoContainerContext.getCurrentContainer()).thenReturn(container); + PluginInfo pluginTypeConfig = mock(PluginInfo.class); + when(pluginSettingService.getAllPlugins()).thenReturn(Collections.singletonList(pluginTypeConfig)); + when(settingService.getContextNamesByType(Context.USER.getName(), 0, 20)).thenReturn(List.of("userTest")); + UserSetting userSetting = mock(UserSetting.class); + Set channelActives = new HashSet<>(); + channelActives.add("MAIL_CHANNEL"); + when(userSetting.getChannelActives()).thenReturn(channelActives); + when(userSettingService.get("userTest")).thenReturn(userSetting); + when(pluginTypeConfig.getDefaultConfig()).thenReturn(Arrays.asList("daily", "Instantly")); + when(pluginTypeConfig.getType()).thenReturn("ImportDocumentsPlugin"); + ImportDocumentsNotificationUpgradePlugin notificationUpgradePlugin = + new ImportDocumentsNotificationUpgradePlugin(settingService, + userSettingService, + pluginSettingService, + entityManagerService, + initParams); + notificationUpgradePlugin.processUpgrade(null, null); + // + verify(userSetting, times(1)).addChannelPlugin("MAIL_CHANNEL", pluginTypeConfig.getType()); + verify(userSetting, times(1)).addPlugin(pluginTypeConfig.getType(), UserSetting.FREQUENCY.getFrequecy("daily")); + verify(userSettingService, times(1)).save(userSetting); + } +}