From 10d86e7bfa854d4ed0c043932621eee586c1ab81 Mon Sep 17 00:00:00 2001 From: Valmir Junior Date: Mon, 15 Jan 2024 19:53:42 -0300 Subject: [PATCH 1/3] LPS-206371 Transform class CurrentAccountEntryManagerStore to class abstract, and apply to usages --- ... BaseCurrentAccountEntryManagerStore.java} | 47 +++++++++---------- .../CurrentAccountEntryManagerImpl.java | 18 ++----- .../contributor/AccountRoleContributor.java | 13 ++--- 3 files changed, 32 insertions(+), 46 deletions(-) rename modules/apps/account/account-service/src/main/java/com/liferay/account/internal/manager/{CurrentAccountEntryManagerStore.java => BaseCurrentAccountEntryManagerStore.java} (80%) diff --git a/modules/apps/account/account-service/src/main/java/com/liferay/account/internal/manager/CurrentAccountEntryManagerStore.java b/modules/apps/account/account-service/src/main/java/com/liferay/account/internal/manager/BaseCurrentAccountEntryManagerStore.java similarity index 80% rename from modules/apps/account/account-service/src/main/java/com/liferay/account/internal/manager/CurrentAccountEntryManagerStore.java rename to modules/apps/account/account-service/src/main/java/com/liferay/account/internal/manager/BaseCurrentAccountEntryManagerStore.java index 3114655832642f..955e02bddc22c7 100644 --- a/modules/apps/account/account-service/src/main/java/com/liferay/account/internal/manager/CurrentAccountEntryManagerStore.java +++ b/modules/apps/account/account-service/src/main/java/com/liferay/account/internal/manager/BaseCurrentAccountEntryManagerStore.java @@ -22,14 +22,12 @@ import javax.servlet.http.HttpSession; -import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; /** * @author Drew Brokke */ -@Component(service = CurrentAccountEntryManagerStore.class) -public class CurrentAccountEntryManagerStore { +public abstract class BaseCurrentAccountEntryManagerStore { public AccountEntry getAccountEntryFromHttpSession(long groupId) { HttpSession httpSession = PortalSessionThreadLocal.getHttpSession(); @@ -41,7 +39,7 @@ public AccountEntry getAccountEntryFromHttpSession(long groupId) { long currentAccountEntryId = GetterUtil.getLong( httpSession.getAttribute(_getKey(groupId))); - return _accountEntryLocalService.fetchAccountEntry( + return accountEntryLocalService.fetchAccountEntry( currentAccountEntryId); } @@ -50,7 +48,7 @@ public AccountEntry getAccountEntryFromPortalPreferences( com.liferay.portal.kernel.model.PortalPreferences modelPortalPreferences = - _portalPreferencesLocalService.fetchPortalPreferences( + portalPreferencesLocalService.fetchPortalPreferences( userId, PortletKeys.PREFS_OWNER_TYPE_USER); if (modelPortalPreferences == null) { @@ -58,7 +56,7 @@ public AccountEntry getAccountEntryFromPortalPreferences( } PortalPreferences portalPreferences = - _portalPreferenceValueLocalService.getPortalPreferences( + portalPreferenceValueLocalService.getPortalPreferences( modelPortalPreferences, false); long accountEntryId = GetterUtil.getLong( @@ -66,7 +64,7 @@ public AccountEntry getAccountEntryFromPortalPreferences( AccountEntry.class.getName(), _getKey(groupId))); if (accountEntryId > 0) { - return _accountEntryLocalService.fetchAccountEntry(accountEntryId); + return accountEntryLocalService.fetchAccountEntry(accountEntryId); } return null; @@ -114,17 +112,30 @@ public void saveInPortalPreferences( portalPreferences.setValue( AccountEntry.class.getName(), key, String.valueOf(accountEntryId)); - _portalPreferencesLocalService.updatePreferences( + portalPreferencesLocalService.updatePreferences( userId, PortletKeys.PREFS_OWNER_TYPE_USER, portalPreferences); } - public void setCurrentAccountEntry( + public void setCurrentAccountEntryManagerStore( long accountEntryId, long groupId, long userId) { saveInHttpSession(accountEntryId, groupId); saveInPortalPreferences(accountEntryId, groupId, userId); } + @Reference + protected AccountEntryLocalService accountEntryLocalService; + + @Reference + protected PortalPreferencesLocalService portalPreferencesLocalService; + + @Reference + protected PortalPreferenceValueLocalService + portalPreferenceValueLocalService; + + @Reference + protected PortletPreferencesFactory portletPreferencesFactory; + private String _getKey(long groupId) { return AccountWebKeys.CURRENT_ACCOUNT_ENTRY_ID + groupId; } @@ -134,8 +145,7 @@ private PortalPreferences _getPortalPreferences(long userId) { // LPS-156201 try { - return _portletPreferencesFactory.getPortalPreferences( - userId, true); + return portletPreferencesFactory.getPortalPreferences(userId, true); } catch (Exception exception) { if (_log.isDebugEnabled()) { @@ -147,19 +157,6 @@ private PortalPreferences _getPortalPreferences(long userId) { } private static final Log _log = LogFactoryUtil.getLog( - CurrentAccountEntryManagerStore.class); - - @Reference - private AccountEntryLocalService _accountEntryLocalService; - - @Reference - private PortalPreferencesLocalService _portalPreferencesLocalService; - - @Reference - private PortalPreferenceValueLocalService - _portalPreferenceValueLocalService; - - @Reference - private PortletPreferencesFactory _portletPreferencesFactory; + BaseCurrentAccountEntryManagerStore.class); } \ No newline at end of file diff --git a/modules/apps/account/account-service/src/main/java/com/liferay/account/internal/manager/CurrentAccountEntryManagerImpl.java b/modules/apps/account/account-service/src/main/java/com/liferay/account/internal/manager/CurrentAccountEntryManagerImpl.java index 2ca7b45cbc496d..1d1a210e7c5e04 100644 --- a/modules/apps/account/account-service/src/main/java/com/liferay/account/internal/manager/CurrentAccountEntryManagerImpl.java +++ b/modules/apps/account/account-service/src/main/java/com/liferay/account/internal/manager/CurrentAccountEntryManagerImpl.java @@ -40,6 +40,7 @@ */ @Component(service = CurrentAccountEntryManager.class) public class CurrentAccountEntryManagerImpl + extends BaseCurrentAccountEntryManagerStore implements CurrentAccountEntryManager { @Override @@ -63,9 +64,7 @@ public AccountEntry getCurrentAccountEntry(long groupId, long userId) PermissionChecker permissionChecker = _permissionCheckerFactory.create( user); - AccountEntry accountEntry = - _currentAccountEntryManagerStore.getAccountEntryFromHttpSession( - groupId); + AccountEntry accountEntry = getAccountEntryFromHttpSession(groupId); String[] allowedTypes = _getAllowedTypes(groupId); @@ -73,13 +72,10 @@ public AccountEntry getCurrentAccountEntry(long groupId, long userId) return accountEntry; } - accountEntry = - _currentAccountEntryManagerStore. - getAccountEntryFromPortalPreferences(groupId, userId); + accountEntry = getAccountEntryFromPortalPreferences(groupId, userId); if (_isValid(accountEntry, allowedTypes, permissionChecker)) { - _currentAccountEntryManagerStore.saveInHttpSession( - accountEntry.getAccountEntryId(), groupId); + saveInHttpSession(accountEntry.getAccountEntryId(), groupId); return accountEntry; } @@ -117,8 +113,7 @@ public void setCurrentAccountEntry( "type: " + accountEntry.getType()); } - _currentAccountEntryManagerStore.setCurrentAccountEntry( - accountEntryId, groupId, userId); + setCurrentAccountEntryManagerStore(accountEntryId, groupId, userId); } catch (PortalException portalException) { if (_log.isWarnEnabled()) { @@ -198,9 +193,6 @@ private boolean _isValid( private volatile ModelResourcePermission _accountEntryModelResourcePermission; - @Reference - private CurrentAccountEntryManagerStore _currentAccountEntryManagerStore; - @Reference private PermissionCheckerFactory _permissionCheckerFactory; diff --git a/modules/apps/account/account-service/src/main/java/com/liferay/account/internal/security/permission/contributor/AccountRoleContributor.java b/modules/apps/account/account-service/src/main/java/com/liferay/account/internal/security/permission/contributor/AccountRoleContributor.java index 3cb95ffc8a87e1..f3d771c306450b 100644 --- a/modules/apps/account/account-service/src/main/java/com/liferay/account/internal/security/permission/contributor/AccountRoleContributor.java +++ b/modules/apps/account/account-service/src/main/java/com/liferay/account/internal/security/permission/contributor/AccountRoleContributor.java @@ -6,7 +6,7 @@ package com.liferay.account.internal.security.permission.contributor; import com.liferay.account.constants.AccountRoleConstants; -import com.liferay.account.internal.manager.CurrentAccountEntryManagerStore; +import com.liferay.account.internal.manager.BaseCurrentAccountEntryManagerStore; import com.liferay.account.model.AccountEntry; import com.liferay.account.model.AccountRole; import com.liferay.account.service.AccountEntryUserRelLocalService; @@ -32,7 +32,8 @@ * @author Pei-Jung Lan */ @Component(service = RoleContributor.class) -public class AccountRoleContributor implements RoleContributor { +public class AccountRoleContributor + extends BaseCurrentAccountEntryManagerStore implements RoleContributor { @Override public void contribute(RoleCollection roleCollection) { @@ -53,9 +54,8 @@ public void contribute(RoleCollection roleCollection) { if (!Objects.equals( AccountEntry.class.getName(), group.getClassName())) { - AccountEntry currentAccountEntry = - _currentAccountEntryManagerStore.getCurrentAccountEntry( - roleCollection.getGroupId(), user.getUserId()); + AccountEntry currentAccountEntry = getCurrentAccountEntry( + roleCollection.getGroupId(), user.getUserId()); if ((currentAccountEntry != null) && (currentAccountEntry.getAccountEntryId() > 0)) { @@ -103,9 +103,6 @@ private void _addRoleId(RoleCollection roleCollection, String roleName) @Reference private AccountRoleLocalService _accountRoleLocalService; - @Reference - private CurrentAccountEntryManagerStore _currentAccountEntryManagerStore; - @Reference private GroupLocalService _groupLocalService; From dfd4395b50db6dc81b505b97a3d1182757f056e7 Mon Sep 17 00:00:00 2001 From: Valmir Junior Date: Tue, 16 Jan 2024 18:03:30 -0300 Subject: [PATCH 2/3] LPS-206371 Merger the class FeatureFlagBatchEngineUnitProcessor with the class BatchEngineUnitProcessorImpl --- .../unit/BatchEngineUnitProcessorImpl.java | 117 +++++++++++++++-- .../FeatureFlagBatchEngineUnitProcessor.java | 120 ------------------ 2 files changed, 103 insertions(+), 134 deletions(-) delete mode 100644 modules/apps/batch-engine/batch-engine-service/src/main/java/com/liferay/batch/engine/internal/unit/FeatureFlagBatchEngineUnitProcessor.java diff --git a/modules/apps/batch-engine/batch-engine-service/src/main/java/com/liferay/batch/engine/internal/unit/BatchEngineUnitProcessorImpl.java b/modules/apps/batch-engine/batch-engine-service/src/main/java/com/liferay/batch/engine/internal/unit/BatchEngineUnitProcessorImpl.java index 24fcfa6483b555..a64265723cbeb7 100644 --- a/modules/apps/batch-engine/batch-engine-service/src/main/java/com/liferay/batch/engine/internal/unit/BatchEngineUnitProcessorImpl.java +++ b/modules/apps/batch-engine/batch-engine-service/src/main/java/com/liferay/batch/engine/internal/unit/BatchEngineUnitProcessorImpl.java @@ -19,12 +19,15 @@ import com.liferay.batch.engine.unit.BatchEngineUnitProcessor; import com.liferay.batch.engine.unit.BatchEngineUnitThreadLocal; import com.liferay.batch.engine.unit.BundleBatchEngineUnit; +import com.liferay.petra.executor.PortalExecutorManager; +import com.liferay.petra.function.UnsafeSupplier; import com.liferay.petra.io.StreamUtil; import com.liferay.petra.io.unsync.UnsyncByteArrayOutputStream; import com.liferay.petra.reflect.ReflectionUtil; import com.liferay.petra.string.StringBundler; import com.liferay.petra.string.StringPool; import com.liferay.portal.kernel.exception.PortalException; +import com.liferay.portal.kernel.feature.flag.FeatureFlagListener; import com.liferay.portal.kernel.feature.flag.FeatureFlagManagerUtil; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; @@ -32,26 +35,35 @@ import com.liferay.portal.kernel.service.CompanyLocalService; import com.liferay.portal.kernel.service.UserLocalService; import com.liferay.portal.kernel.util.File; +import com.liferay.portal.kernel.util.MapUtil; import com.liferay.portal.kernel.util.PropsKeys; import com.liferay.portal.kernel.util.PropsUtil; import com.liferay.portal.kernel.util.StringUtil; +import com.liferay.portal.kernel.util.Tuple; import com.liferay.portal.kernel.util.Validator; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; import org.osgi.util.tracker.ServiceTracker; @@ -77,20 +89,19 @@ public CompletableFuture processBatchEngineUnits( String featureFlag = batchEngineUnitMetaInfo.getFeatureFlag(); if (_isFeatureFlagDisabled(featureFlag)) { - _featureFlagBatchEngineUnitProcessor. - registerBatchEngineUnit( - batchEngineUnitMetaInfo.getCompanyId(), featureFlag, - () -> { - CompletableFuture localCompletableFuture = - new CompletableFuture<>(); + _registerBatchEngineUnit( + batchEngineUnitMetaInfo.getCompanyId(), featureFlag, + () -> { + CompletableFuture localCompletableFuture = + new CompletableFuture<>(); - Runnable runnable = _processBatchEngineUnit( - batchEngineUnit, localCompletableFuture); + Runnable runnable = _processBatchEngineUnit( + batchEngineUnit, localCompletableFuture); - runnable.run(); + runnable.run(); - return localCompletableFuture; - }); + return localCompletableFuture; + }); continue; } @@ -128,6 +139,14 @@ public CompletableFuture processBatchEngineUnits( @Activate protected void activate(BundleContext bundleContext) { _bundleContext = bundleContext; + _serviceRegistration = bundleContext.registerService( + FeatureFlagListener.class, new FeatureFlagListenerImpl(), + MapUtil.singletonDictionary("featureFlagKey", "*")); + } + + @Deactivate + protected void deactivate() { + _serviceRegistration.unregister(); } private Runnable _execute( @@ -263,6 +282,10 @@ private String _getObjectEntryClassName( return className; } + private Tuple _getTuple(long companyId, String featureFlagKey) { + return new Tuple(companyId, featureFlagKey); + } + private boolean _isFeatureFlagDisabled(String featureFlagKey) { if (Validator.isNotNull(featureFlagKey) && !FeatureFlagManagerUtil.isEnabled(featureFlagKey)) { @@ -371,6 +394,23 @@ private Runnable _processBatchEngineUnit( completableFuture); } + private void _registerBatchEngineUnit( + long companyId, String featureFlagKey, + UnsafeSupplier, Exception> unsafeSupplier) { + + _unsafeSuppliers.compute( + _getTuple(companyId, featureFlagKey), + (key, unsafeSuppliers) -> { + if (unsafeSuppliers == null) { + unsafeSuppliers = new ArrayList<>(); + } + + unsafeSuppliers.add(unsafeSupplier); + + return unsafeSuppliers; + }); + } + private BatchEngineUnitConfiguration _updateBatchEngineUnitConfiguration( BatchEngineUnitConfiguration batchEngineUnitConfiguration) { @@ -437,13 +477,62 @@ private BatchEngineUnitConfiguration _updateBatchEngineUnitConfiguration( private CompanyLocalService _companyLocalService; @Reference - private FeatureFlagBatchEngineUnitProcessor - _featureFlagBatchEngineUnitProcessor; + private File _file; @Reference - private File _file; + private PortalExecutorManager _portalExecutorManager; + + private ServiceRegistration _serviceRegistration; + private final Map + , Exception>>> + _unsafeSuppliers = new ConcurrentHashMap<>(); @Reference private UserLocalService _userLocalService; + private class FeatureFlagListenerImpl implements FeatureFlagListener { + + @Override + public void onValue( + long companyId, String featureFlagKey, boolean enabled) { + + if (!enabled) { + return; + } + + Tuple tuple = _getTuple(companyId, featureFlagKey); + + if (!_unsafeSuppliers.containsKey(tuple)) { + return; + } + + synchronized (_unsafeSuppliers) { + List, Exception>> + unsafeSuppliers = _unsafeSuppliers.remove(tuple); + + ExecutorService executorService = + _portalExecutorManager.getPortalExecutor( + FeatureFlagListenerImpl.class.getName()); + + executorService.submit( + () -> { + for (UnsafeSupplier, Exception> + unsafeSupplier : unsafeSuppliers) { + + try { + CompletableFuture completableFuture = + unsafeSupplier.get(); + + completableFuture.get(); + } + catch (Exception exception) { + throw new RuntimeException(exception); + } + } + }); + } + } + + } + } \ No newline at end of file diff --git a/modules/apps/batch-engine/batch-engine-service/src/main/java/com/liferay/batch/engine/internal/unit/FeatureFlagBatchEngineUnitProcessor.java b/modules/apps/batch-engine/batch-engine-service/src/main/java/com/liferay/batch/engine/internal/unit/FeatureFlagBatchEngineUnitProcessor.java deleted file mode 100644 index c7dabf0fcba030..00000000000000 --- a/modules/apps/batch-engine/batch-engine-service/src/main/java/com/liferay/batch/engine/internal/unit/FeatureFlagBatchEngineUnitProcessor.java +++ /dev/null @@ -1,120 +0,0 @@ -/** - * SPDX-FileCopyrightText: (c) 2023 Liferay, Inc. https://liferay.com - * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 - */ - -package com.liferay.batch.engine.internal.unit; - -import com.liferay.petra.executor.PortalExecutorManager; -import com.liferay.petra.function.UnsafeSupplier; -import com.liferay.portal.kernel.feature.flag.FeatureFlagListener; -import com.liferay.portal.kernel.util.MapUtil; -import com.liferay.portal.kernel.util.Tuple; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutorService; - -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; -import org.osgi.service.component.annotations.Activate; -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.annotations.Deactivate; -import org.osgi.service.component.annotations.Reference; - -/** - * @author Carlos Correa - */ -@Component(service = FeatureFlagBatchEngineUnitProcessor.class) -public class FeatureFlagBatchEngineUnitProcessor { - - public void registerBatchEngineUnit( - long companyId, String featureFlagKey, - UnsafeSupplier, Exception> unsafeSupplier) { - - _unsafeSuppliers.compute( - _getTuple(companyId, featureFlagKey), - (key, unsafeSuppliers) -> { - if (unsafeSuppliers == null) { - unsafeSuppliers = new ArrayList<>(); - } - - unsafeSuppliers.add(unsafeSupplier); - - return unsafeSuppliers; - }); - } - - @Activate - protected void activate(BundleContext bundleContext) { - _serviceRegistration = bundleContext.registerService( - FeatureFlagListener.class, new FeatureFlagListenerImpl(), - MapUtil.singletonDictionary("featureFlagKey", "*")); - } - - @Deactivate - protected void deactivate() { - _serviceRegistration.unregister(); - } - - private Tuple _getTuple(long companyId, String featureFlagKey) { - return new Tuple(companyId, featureFlagKey); - } - - @Reference - private PortalExecutorManager _portalExecutorManager; - - private ServiceRegistration _serviceRegistration; - private final Map - , Exception>>> - _unsafeSuppliers = new ConcurrentHashMap<>(); - - private class FeatureFlagListenerImpl implements FeatureFlagListener { - - @Override - public void onValue( - long companyId, String featureFlagKey, boolean enabled) { - - if (!enabled) { - return; - } - - Tuple tuple = _getTuple(companyId, featureFlagKey); - - if (!_unsafeSuppliers.containsKey(tuple)) { - return; - } - - synchronized (_unsafeSuppliers) { - List, Exception>> - unsafeSuppliers = _unsafeSuppliers.remove(tuple); - - ExecutorService executorService = - _portalExecutorManager.getPortalExecutor( - FeatureFlagListenerImpl.class.getName()); - - executorService.submit( - () -> { - for (UnsafeSupplier, Exception> - unsafeSupplier : unsafeSuppliers) { - - try { - CompletableFuture completableFuture = - unsafeSupplier.get(); - - completableFuture.get(); - } - catch (Exception exception) { - throw new RuntimeException(exception); - } - } - }); - } - } - - } - -} \ No newline at end of file From 38ddb2e596fda3892c1eb0cc8f1f52607f328263 Mon Sep 17 00:00:00 2001 From: Valmir Junior Date: Mon, 22 Jan 2024 15:17:20 -0300 Subject: [PATCH 3/3] LPS-206371 Move methods from class WorkflowMetricsSLADefinitionTransformer to class WorkflowMetricsSLADefinitionTransformerSchedulerJobConfiguration --- ...nTransformerSchedulerJobConfiguration.java | 238 ++++++++++++++- ...rkflowMetricsSLADefinitionTransformer.java | 270 ------------------ 2 files changed, 234 insertions(+), 274 deletions(-) delete mode 100644 modules/dxp/apps/portal-workflow/portal-workflow-metrics-service/src/main/java/com/liferay/portal/workflow/metrics/internal/sla/transformer/WorkflowMetricsSLADefinitionTransformer.java diff --git a/modules/dxp/apps/portal-workflow/portal-workflow-metrics-service/src/main/java/com/liferay/portal/workflow/metrics/internal/scheduler/WorkflowMetricsSLADefinitionTransformerSchedulerJobConfiguration.java b/modules/dxp/apps/portal-workflow/portal-workflow-metrics-service/src/main/java/com/liferay/portal/workflow/metrics/internal/scheduler/WorkflowMetricsSLADefinitionTransformerSchedulerJobConfiguration.java index 71e1e1d22acbda..15446d640a0e03 100644 --- a/modules/dxp/apps/portal-workflow/portal-workflow-metrics-service/src/main/java/com/liferay/portal/workflow/metrics/internal/scheduler/WorkflowMetricsSLADefinitionTransformerSchedulerJobConfiguration.java +++ b/modules/dxp/apps/portal-workflow/portal-workflow-metrics-service/src/main/java/com/liferay/portal/workflow/metrics/internal/scheduler/WorkflowMetricsSLADefinitionTransformerSchedulerJobConfiguration.java @@ -7,6 +7,10 @@ import com.liferay.petra.function.UnsafeConsumer; import com.liferay.petra.function.UnsafeRunnable; +import com.liferay.petra.string.CharPool; +import com.liferay.petra.string.StringBundler; +import com.liferay.petra.string.StringPool; +import com.liferay.petra.string.StringUtil; import com.liferay.portal.configuration.metatype.bnd.util.ConfigurableUtil; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.log.Log; @@ -15,11 +19,23 @@ import com.liferay.portal.kernel.scheduler.TimeUnit; import com.liferay.portal.kernel.scheduler.TriggerConfiguration; import com.liferay.portal.kernel.service.CompanyLocalService; +import com.liferay.portal.kernel.service.ServiceContext; +import com.liferay.portal.kernel.util.ArrayUtil; +import com.liferay.portal.kernel.util.MapUtil; +import com.liferay.portal.kernel.workflow.WorkflowConstants; +import com.liferay.portal.search.aggregation.AggregationResult; +import com.liferay.portal.search.aggregation.Aggregations; +import com.liferay.portal.search.aggregation.bucket.Bucket; +import com.liferay.portal.search.aggregation.bucket.TermsAggregation; +import com.liferay.portal.search.aggregation.bucket.TermsAggregationResult; +import com.liferay.portal.search.aggregation.metrics.TopHitsAggregation; +import com.liferay.portal.search.aggregation.metrics.TopHitsAggregationResult; import com.liferay.portal.search.capabilities.SearchCapabilities; import com.liferay.portal.search.document.Document; import com.liferay.portal.search.engine.adapter.SearchEngineAdapter; import com.liferay.portal.search.engine.adapter.index.IndicesExistsIndexRequest; import com.liferay.portal.search.engine.adapter.index.IndicesExistsIndexResponse; +import com.liferay.portal.search.engine.adapter.search.SearchRequestExecutor; import com.liferay.portal.search.engine.adapter.search.SearchSearchRequest; import com.liferay.portal.search.engine.adapter.search.SearchSearchResponse; import com.liferay.portal.search.hits.SearchHit; @@ -27,10 +43,16 @@ import com.liferay.portal.search.index.IndexNameBuilder; import com.liferay.portal.search.query.BooleanQuery; import com.liferay.portal.search.query.Queries; +import com.liferay.portal.search.query.TermsQuery; import com.liferay.portal.workflow.metrics.internal.configuration.WorkflowMetricsConfiguration; -import com.liferay.portal.workflow.metrics.internal.sla.transformer.WorkflowMetricsSLADefinitionTransformer; +import com.liferay.portal.workflow.metrics.model.WorkflowMetricsSLADefinition; import com.liferay.portal.workflow.metrics.search.index.constants.WorkflowMetricsIndexNameConstants; +import com.liferay.portal.workflow.metrics.service.WorkflowMetricsSLADefinitionLocalService; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; import java.util.Map; import org.osgi.service.component.annotations.Activate; @@ -85,6 +107,116 @@ private BooleanQuery _createBooleanQuery(long companyId) { _queries.term("deleted", Boolean.FALSE)); } + private BooleanQuery _createNodeBooleanQuery( + String currentProcessVersion, String latestProcessVersion, + WorkflowMetricsSLADefinition workflowMetricsSLADefinition) { + + BooleanQuery booleanQuery = _queries.booleanQuery(); + + TermsQuery termsQuery = _queries.terms("version"); + + termsQuery.addValues(currentProcessVersion, latestProcessVersion); + + return booleanQuery.addMustQueryClauses( + _queries.term( + "companyId", workflowMetricsSLADefinition.getCompanyId()), + _queries.term( + "processId", workflowMetricsSLADefinition.getProcessId()), + termsQuery); + } + + private String _getNodeId( + String processVersion, + TermsAggregationResult versionTermsAggregationResult) { + + Bucket processVersionBucket = versionTermsAggregationResult.getBucket( + processVersion); + + TopHitsAggregationResult topHitsAggregationResult = + (TopHitsAggregationResult) + processVersionBucket.getChildAggregationResult("topHits"); + + SearchHits searchHits = topHitsAggregationResult.getSearchHits(); + + for (SearchHit searchHit : searchHits.getSearchHits()) { + return MapUtil.getString(searchHit.getSourcesMap(), "nodeId"); + } + + return StringPool.BLANK; + } + + private Map _getNodeIdMap( + String currentProcessVersion, String latestProcessVersion, + WorkflowMetricsSLADefinition workflowMetricsSLADefinition) { + + Map nodeIds = new HashMap<>(); + + SearchSearchRequest searchSearchRequest = new SearchSearchRequest(); + + TermsAggregation nameTermsAggregation = _aggregations.terms( + "name", "name"); + + nameTermsAggregation.setSize(10000); + + TermsAggregation versionTermsAggregation = _aggregations.terms( + "version", "version"); + + TopHitsAggregation topHitsAggregation = _aggregations.topHits( + "topHits"); + + topHitsAggregation.setSize(2); + + versionTermsAggregation.addChildAggregation(topHitsAggregation); + + versionTermsAggregation.setSize(10000); + + nameTermsAggregation.addChildAggregation(versionTermsAggregation); + + searchSearchRequest.addAggregation(nameTermsAggregation); + + String indexName = _indexNameBuilder.getIndexName( + workflowMetricsSLADefinition.getCompanyId()); + + searchSearchRequest.setIndexNames( + indexName + WorkflowMetricsIndexNameConstants.SUFFIX_NODE); + + searchSearchRequest.setQuery( + _createNodeBooleanQuery( + currentProcessVersion, latestProcessVersion, + workflowMetricsSLADefinition)); + searchSearchRequest.setSize(0); + + SearchSearchResponse searchSearchResponse = + _searchRequestExecutor.executeSearchRequest(searchSearchRequest); + + Map aggregationResultsMap = + searchSearchResponse.getAggregationResultsMap(); + + TermsAggregationResult nameTermsAggregationResult = + (TermsAggregationResult)aggregationResultsMap.get("name"); + + for (Bucket bucket : nameTermsAggregationResult.getBuckets()) { + TermsAggregationResult versionTermsAggregationResult = + (TermsAggregationResult)bucket.getChildAggregationResult( + "version"); + + Collection versionBuckets = + versionTermsAggregationResult.getBuckets(); + + if (versionBuckets.size() != 2) { + continue; + } + + nodeIds.put( + _getNodeId( + currentProcessVersion, versionTermsAggregationResult), + _getNodeId( + latestProcessVersion, versionTermsAggregationResult)); + } + + return nodeIds; + } + private boolean _hasIndex(long companyId) { if (!_searchCapabilities.isWorkflowMetricsSupported()) { return false; @@ -128,7 +260,7 @@ private void _transform(long companyId) { Document document = searchHit.getDocument(); try { - _workflowMetricsSLADefinitionTransformer.transform( + _transform( document.getLong("companyId"), document.getString("version"), document.getLong("processId")); @@ -139,9 +271,104 @@ private void _transform(long companyId) { } } + private void _transform( + long companyId, String latestProcessVersion, long processId) + throws PortalException { + + List workflowMetricsSLADefinitions = + _workflowMetricsSLADefinitionLocalService. + getWorkflowMetricsSLADefinitions( + companyId, true, processId, latestProcessVersion, + WorkflowConstants.STATUS_APPROVED); + + for (WorkflowMetricsSLADefinition workflowMetricsSLADefinition : + workflowMetricsSLADefinitions) { + + _transform(latestProcessVersion, workflowMetricsSLADefinition); + } + } + + private void _transform( + String latestProcessVersion, + WorkflowMetricsSLADefinition workflowMetricsSLADefinition) + throws PortalException { + + Map nodeIdMap = _getNodeIdMap( + workflowMetricsSLADefinition.getProcessVersion(), + latestProcessVersion, workflowMetricsSLADefinition); + + String[] pauseNodeKeys = _transformNodeKeys( + nodeIdMap, + StringUtil.split(workflowMetricsSLADefinition.getPauseNodeKeys())); + String[] startNodeKeys = _transformNodeKeys( + nodeIdMap, + StringUtil.split(workflowMetricsSLADefinition.getStartNodeKeys())); + String[] stopNodeKeys = _transformNodeKeys( + nodeIdMap, + StringUtil.split(workflowMetricsSLADefinition.getStopNodeKeys())); + + int status = WorkflowConstants.STATUS_APPROVED; + + if (ArrayUtil.isEmpty(startNodeKeys) || + ArrayUtil.isEmpty(stopNodeKeys)) { + + status = WorkflowConstants.STATUS_DRAFT; + } + + _workflowMetricsSLADefinitionLocalService. + updateWorkflowMetricsSLADefinition( + workflowMetricsSLADefinition. + getWorkflowMetricsSLADefinitionId(), + workflowMetricsSLADefinition.getCalendarKey(), + workflowMetricsSLADefinition.getDescription(), + workflowMetricsSLADefinition.getDuration(), + workflowMetricsSLADefinition.getName(), pauseNodeKeys, + startNodeKeys, stopNodeKeys, status, + new ServiceContext() { + { + setCompanyId( + workflowMetricsSLADefinition.getCompanyId()); + setScopeGroupId( + workflowMetricsSLADefinition.getGroupId()); + setUserId(workflowMetricsSLADefinition.getUserId()); + } + }); + } + + private String[] _transformNodeKeys( + Map nodeIdMap, List oldNodeKeys) { + + List newNodeKeys = new ArrayList<>(); + + for (String oldNodeKey : oldNodeKeys) { + List parts = StringUtil.split(oldNodeKey, CharPool.COLON); + + String oldNodeId = parts.get(0); + + if (!nodeIdMap.containsKey(oldNodeId)) { + continue; + } + + if (parts.size() == 1) { + newNodeKeys.add(nodeIdMap.get(oldNodeId)); + } + else { + newNodeKeys.add( + StringBundler.concat( + nodeIdMap.get(oldNodeId), CharPool.COLON, + parts.get(1))); + } + } + + return newNodeKeys.toArray(new String[0]); + } + private static final Log _log = LogFactoryUtil.getLog( WorkflowMetricsSLADefinitionTransformerSchedulerJobConfiguration.class); + @Reference + private Aggregations _aggregations; + @Reference private CompanyLocalService _companyLocalService; @@ -157,10 +384,13 @@ private void _transform(long companyId) { @Reference private SearchEngineAdapter _searchEngineAdapter; + @Reference + private SearchRequestExecutor _searchRequestExecutor; + private TriggerConfiguration _triggerConfiguration; @Reference - private WorkflowMetricsSLADefinitionTransformer - _workflowMetricsSLADefinitionTransformer; + private WorkflowMetricsSLADefinitionLocalService + _workflowMetricsSLADefinitionLocalService; } \ No newline at end of file diff --git a/modules/dxp/apps/portal-workflow/portal-workflow-metrics-service/src/main/java/com/liferay/portal/workflow/metrics/internal/sla/transformer/WorkflowMetricsSLADefinitionTransformer.java b/modules/dxp/apps/portal-workflow/portal-workflow-metrics-service/src/main/java/com/liferay/portal/workflow/metrics/internal/sla/transformer/WorkflowMetricsSLADefinitionTransformer.java deleted file mode 100644 index a9535d604c9771..00000000000000 --- a/modules/dxp/apps/portal-workflow/portal-workflow-metrics-service/src/main/java/com/liferay/portal/workflow/metrics/internal/sla/transformer/WorkflowMetricsSLADefinitionTransformer.java +++ /dev/null @@ -1,270 +0,0 @@ -/** - * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com - * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 - */ - -package com.liferay.portal.workflow.metrics.internal.sla.transformer; - -import com.liferay.petra.string.CharPool; -import com.liferay.petra.string.StringBundler; -import com.liferay.petra.string.StringPool; -import com.liferay.petra.string.StringUtil; -import com.liferay.portal.kernel.exception.PortalException; -import com.liferay.portal.kernel.service.ServiceContext; -import com.liferay.portal.kernel.util.ArrayUtil; -import com.liferay.portal.kernel.util.MapUtil; -import com.liferay.portal.kernel.workflow.WorkflowConstants; -import com.liferay.portal.search.aggregation.AggregationResult; -import com.liferay.portal.search.aggregation.Aggregations; -import com.liferay.portal.search.aggregation.bucket.Bucket; -import com.liferay.portal.search.aggregation.bucket.TermsAggregation; -import com.liferay.portal.search.aggregation.bucket.TermsAggregationResult; -import com.liferay.portal.search.aggregation.metrics.TopHitsAggregation; -import com.liferay.portal.search.aggregation.metrics.TopHitsAggregationResult; -import com.liferay.portal.search.engine.adapter.search.SearchRequestExecutor; -import com.liferay.portal.search.engine.adapter.search.SearchSearchRequest; -import com.liferay.portal.search.engine.adapter.search.SearchSearchResponse; -import com.liferay.portal.search.hits.SearchHit; -import com.liferay.portal.search.hits.SearchHits; -import com.liferay.portal.search.index.IndexNameBuilder; -import com.liferay.portal.search.query.BooleanQuery; -import com.liferay.portal.search.query.Queries; -import com.liferay.portal.search.query.TermsQuery; -import com.liferay.portal.workflow.metrics.model.WorkflowMetricsSLADefinition; -import com.liferay.portal.workflow.metrics.search.index.constants.WorkflowMetricsIndexNameConstants; -import com.liferay.portal.workflow.metrics.service.WorkflowMetricsSLADefinitionLocalService; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.annotations.Reference; - -/** - * @author Rafael Praxedes - */ -@Component(service = WorkflowMetricsSLADefinitionTransformer.class) -public class WorkflowMetricsSLADefinitionTransformer { - - public void transform( - long companyId, String latestProcessVersion, long processId) - throws PortalException { - - List workflowMetricsSLADefinitions = - _workflowMetricsSLADefinitionLocalService. - getWorkflowMetricsSLADefinitions( - companyId, true, processId, latestProcessVersion, - WorkflowConstants.STATUS_APPROVED); - - for (WorkflowMetricsSLADefinition workflowMetricsSLADefinition : - workflowMetricsSLADefinitions) { - - _transform(latestProcessVersion, workflowMetricsSLADefinition); - } - } - - private BooleanQuery _createNodeBooleanQuery( - String currentProcessVersion, String latestProcessVersion, - WorkflowMetricsSLADefinition workflowMetricsSLADefinition) { - - BooleanQuery booleanQuery = _queries.booleanQuery(); - - TermsQuery termsQuery = _queries.terms("version"); - - termsQuery.addValues(currentProcessVersion, latestProcessVersion); - - return booleanQuery.addMustQueryClauses( - _queries.term( - "companyId", workflowMetricsSLADefinition.getCompanyId()), - _queries.term( - "processId", workflowMetricsSLADefinition.getProcessId()), - termsQuery); - } - - private String _getNodeId( - String processVersion, - TermsAggregationResult versionTermsAggregationResult) { - - Bucket processVersionBucket = versionTermsAggregationResult.getBucket( - processVersion); - - TopHitsAggregationResult topHitsAggregationResult = - (TopHitsAggregationResult) - processVersionBucket.getChildAggregationResult("topHits"); - - SearchHits searchHits = topHitsAggregationResult.getSearchHits(); - - for (SearchHit searchHit : searchHits.getSearchHits()) { - return MapUtil.getString(searchHit.getSourcesMap(), "nodeId"); - } - - return StringPool.BLANK; - } - - private Map _getNodeIdMap( - String currentProcessVersion, String latestProcessVersion, - WorkflowMetricsSLADefinition workflowMetricsSLADefinition) { - - Map nodeIds = new HashMap<>(); - - SearchSearchRequest searchSearchRequest = new SearchSearchRequest(); - - TermsAggregation nameTermsAggregation = _aggregations.terms( - "name", "name"); - - nameTermsAggregation.setSize(10000); - - TermsAggregation versionTermsAggregation = _aggregations.terms( - "version", "version"); - - TopHitsAggregation topHitsAggregation = _aggregations.topHits( - "topHits"); - - topHitsAggregation.setSize(2); - - versionTermsAggregation.addChildAggregation(topHitsAggregation); - - versionTermsAggregation.setSize(10000); - - nameTermsAggregation.addChildAggregation(versionTermsAggregation); - - searchSearchRequest.addAggregation(nameTermsAggregation); - - String indexName = _indexNameBuilder.getIndexName( - workflowMetricsSLADefinition.getCompanyId()); - - searchSearchRequest.setIndexNames( - indexName + WorkflowMetricsIndexNameConstants.SUFFIX_NODE); - - searchSearchRequest.setQuery( - _createNodeBooleanQuery( - currentProcessVersion, latestProcessVersion, - workflowMetricsSLADefinition)); - searchSearchRequest.setSize(0); - - SearchSearchResponse searchSearchResponse = - _searchRequestExecutor.executeSearchRequest(searchSearchRequest); - - Map aggregationResultsMap = - searchSearchResponse.getAggregationResultsMap(); - - TermsAggregationResult nameTermsAggregationResult = - (TermsAggregationResult)aggregationResultsMap.get("name"); - - for (Bucket bucket : nameTermsAggregationResult.getBuckets()) { - TermsAggregationResult versionTermsAggregationResult = - (TermsAggregationResult)bucket.getChildAggregationResult( - "version"); - - Collection versionBuckets = - versionTermsAggregationResult.getBuckets(); - - if (versionBuckets.size() != 2) { - continue; - } - - nodeIds.put( - _getNodeId( - currentProcessVersion, versionTermsAggregationResult), - _getNodeId( - latestProcessVersion, versionTermsAggregationResult)); - } - - return nodeIds; - } - - private void _transform( - String latestProcessVersion, - WorkflowMetricsSLADefinition workflowMetricsSLADefinition) - throws PortalException { - - Map nodeIdMap = _getNodeIdMap( - workflowMetricsSLADefinition.getProcessVersion(), - latestProcessVersion, workflowMetricsSLADefinition); - - String[] pauseNodeKeys = _transformNodeKeys( - nodeIdMap, - StringUtil.split(workflowMetricsSLADefinition.getPauseNodeKeys())); - String[] startNodeKeys = _transformNodeKeys( - nodeIdMap, - StringUtil.split(workflowMetricsSLADefinition.getStartNodeKeys())); - String[] stopNodeKeys = _transformNodeKeys( - nodeIdMap, - StringUtil.split(workflowMetricsSLADefinition.getStopNodeKeys())); - - int status = WorkflowConstants.STATUS_APPROVED; - - if (ArrayUtil.isEmpty(startNodeKeys) || - ArrayUtil.isEmpty(stopNodeKeys)) { - - status = WorkflowConstants.STATUS_DRAFT; - } - - _workflowMetricsSLADefinitionLocalService. - updateWorkflowMetricsSLADefinition( - workflowMetricsSLADefinition. - getWorkflowMetricsSLADefinitionId(), - workflowMetricsSLADefinition.getCalendarKey(), - workflowMetricsSLADefinition.getDescription(), - workflowMetricsSLADefinition.getDuration(), - workflowMetricsSLADefinition.getName(), pauseNodeKeys, - startNodeKeys, stopNodeKeys, status, - new ServiceContext() { - { - setCompanyId( - workflowMetricsSLADefinition.getCompanyId()); - setScopeGroupId( - workflowMetricsSLADefinition.getGroupId()); - setUserId(workflowMetricsSLADefinition.getUserId()); - } - }); - } - - private String[] _transformNodeKeys( - Map nodeIdMap, List oldNodeKeys) { - - List newNodeKeys = new ArrayList<>(); - - for (String oldNodeKey : oldNodeKeys) { - List parts = StringUtil.split(oldNodeKey, CharPool.COLON); - - String oldNodeId = parts.get(0); - - if (!nodeIdMap.containsKey(oldNodeId)) { - continue; - } - - if (parts.size() == 1) { - newNodeKeys.add(nodeIdMap.get(oldNodeId)); - } - else { - newNodeKeys.add( - StringBundler.concat( - nodeIdMap.get(oldNodeId), CharPool.COLON, - parts.get(1))); - } - } - - return newNodeKeys.toArray(new String[0]); - } - - @Reference - private Aggregations _aggregations; - - @Reference - private IndexNameBuilder _indexNameBuilder; - - @Reference - private Queries _queries; - - @Reference - private SearchRequestExecutor _searchRequestExecutor; - - @Reference - private WorkflowMetricsSLADefinitionLocalService - _workflowMetricsSLADefinitionLocalService; - -} \ No newline at end of file