From 42f9ee3154296d2ee54b48ef8425a29f81ee268f Mon Sep 17 00:00:00 2001 From: Marcos Martins Date: Mon, 4 Sep 2023 16:24:15 -0300 Subject: [PATCH] LRAC-14467 set experiment publishable to false on Asah when creating a new experiment over that --- ...AddSegmentsExperimentMVCActionCommand.java | 72 +++++++++++++++++-- .../internal/util/SegmentsExperimentUtil.java | 26 +++++++ 2 files changed, 93 insertions(+), 5 deletions(-) rename modules/dxp/apps/segments/{segments-experiment-web/src/main/java/com/liferay/segments/experiment/web => segments-asah-connector/src/main/java/com/liferay/segments/asah/connector}/internal/portlet/action/AddSegmentsExperimentMVCActionCommand.java (70%) diff --git a/modules/dxp/apps/segments/segments-experiment-web/src/main/java/com/liferay/segments/experiment/web/internal/portlet/action/AddSegmentsExperimentMVCActionCommand.java b/modules/dxp/apps/segments/segments-asah-connector/src/main/java/com/liferay/segments/asah/connector/internal/portlet/action/AddSegmentsExperimentMVCActionCommand.java similarity index 70% rename from modules/dxp/apps/segments/segments-experiment-web/src/main/java/com/liferay/segments/experiment/web/internal/portlet/action/AddSegmentsExperimentMVCActionCommand.java rename to modules/dxp/apps/segments/segments-asah-connector/src/main/java/com/liferay/segments/asah/connector/internal/portlet/action/AddSegmentsExperimentMVCActionCommand.java index cad5c4e5760d1a..958d4a09f48ffc 100644 --- a/modules/dxp/apps/segments/segments-experiment-web/src/main/java/com/liferay/segments/experiment/web/internal/portlet/action/AddSegmentsExperimentMVCActionCommand.java +++ b/modules/dxp/apps/segments/segments-asah-connector/src/main/java/com/liferay/segments/asah/connector/internal/portlet/action/AddSegmentsExperimentMVCActionCommand.java @@ -3,8 +3,9 @@ * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 */ -package com.liferay.segments.experiment.web.internal.portlet.action; +package com.liferay.segments.asah.connector.internal.portlet.action; +import com.liferay.analytics.settings.configuration.AnalyticsConfiguration; import com.liferay.analytics.settings.rest.manager.AnalyticsSettingsManager; import com.liferay.portal.kernel.json.JSONObject; import com.liferay.portal.kernel.json.JSONUtil; @@ -14,24 +15,36 @@ import com.liferay.portal.kernel.portlet.JSONPortletResponseUtil; import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand; import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand; +import com.liferay.portal.kernel.service.CompanyLocalService; +import com.liferay.portal.kernel.service.GroupLocalService; +import com.liferay.portal.kernel.service.LayoutLocalService; import com.liferay.portal.kernel.service.ServiceContext; import com.liferay.portal.kernel.service.ServiceContextFactory; import com.liferay.portal.kernel.theme.ThemeDisplay; import com.liferay.portal.kernel.transaction.Propagation; import com.liferay.portal.kernel.transaction.TransactionConfig; import com.liferay.portal.kernel.transaction.TransactionInvokerUtil; +import com.liferay.portal.kernel.util.Http; +import com.liferay.portal.kernel.util.LocaleUtil; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.Portal; import com.liferay.portal.kernel.util.WebKeys; +import com.liferay.segments.asah.connector.internal.client.AsahFaroBackendClient; +import com.liferay.segments.asah.connector.internal.client.AsahFaroBackendClientImpl; +import com.liferay.segments.asah.connector.internal.client.model.Experiment; +import com.liferay.segments.asah.connector.internal.client.model.util.ExperimentUtil; +import com.liferay.segments.asah.connector.internal.util.SegmentsExperimentUtil; import com.liferay.segments.constants.SegmentsExperimentConstants; import com.liferay.segments.constants.SegmentsPortletKeys; import com.liferay.segments.exception.DuplicateSegmentsExperimentException; -import com.liferay.segments.experiment.web.internal.util.SegmentsExperimentUtil; import com.liferay.segments.model.SegmentsExperiment; import com.liferay.segments.model.SegmentsExperimentRel; +import com.liferay.segments.service.SegmentsEntryLocalService; +import com.liferay.segments.service.SegmentsExperienceLocalService; import com.liferay.segments.service.SegmentsExperimentRelService; import com.liferay.segments.service.SegmentsExperimentService; +import java.util.Map; import java.util.concurrent.Callable; import javax.portlet.ActionRequest; @@ -39,7 +52,9 @@ import javax.servlet.http.HttpServletResponse; +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; /** @@ -55,6 +70,17 @@ public class AddSegmentsExperimentMVCActionCommand extends BaseMVCActionCommand { + @Activate + protected void activate(Map properties) { + _asahFaroBackendClient = new AsahFaroBackendClientImpl( + _analyticsSettingsManager, _http); + } + + @Deactivate + protected void deactivate() { + _asahFaroBackendClient = null; + } + @Override protected void doProcessAction( ActionRequest actionRequest, ActionResponse actionResponse) @@ -98,6 +124,10 @@ private JSONObject _addSegmentsExperiment(ActionRequest actionRequest) ServiceContext serviceContext = ServiceContextFactory.getInstance( actionRequest); + AnalyticsConfiguration analyticsConfiguration = + _analyticsSettingsManager.getAnalyticsConfiguration( + serviceContext.getCompanyId()); + long segmentsExperienceId = ParamUtil.getLong( actionRequest, "segmentsExperienceId"); long plid = ParamUtil.getLong(actionRequest, "plid"); @@ -110,6 +140,19 @@ private JSONObject _addSegmentsExperiment(ActionRequest actionRequest) if (segmentsExperiment.getStatus() == SegmentsExperimentConstants.STATUS_TERMINATED) { + Experiment experiment = ExperimentUtil.toExperiment( + _companyLocalService, + analyticsConfiguration.liferayAnalyticsDataSourceId(), + _groupLocalService, _layoutLocalService, + LocaleUtil.getSiteDefault(), _portal, + _segmentsEntryLocalService, _segmentsExperienceLocalService, + segmentsExperiment); + + experiment.setPublishable(false); + + _asahFaroBackendClient.updateExperiment( + segmentsExperiment.getCompanyId(), experiment); + segmentsExperiment.setStatus( SegmentsExperimentConstants.STATUS_DELETED_ON_DXP_ONLY); @@ -139,9 +182,8 @@ private JSONObject _addSegmentsExperiment(ActionRequest actionRequest) return JSONUtil.put( "segmentsExperiment", SegmentsExperimentUtil.toSegmentsExperimentJSONObject( - _analyticsSettingsManager.getAnalyticsConfiguration( - segmentsExperiment.getCompanyId()), - themeDisplay.getLocale(), segmentsExperiment) + analyticsConfiguration, themeDisplay.getLocale(), + segmentsExperiment) ).put( "segmentsExperimentRel", SegmentsExperimentUtil.toSegmentsExperimentRelJSONObject( @@ -159,12 +201,32 @@ private JSONObject _addSegmentsExperiment(ActionRequest actionRequest) @Reference private AnalyticsSettingsManager _analyticsSettingsManager; + private AsahFaroBackendClient _asahFaroBackendClient; + + @Reference + private CompanyLocalService _companyLocalService; + + @Reference + private GroupLocalService _groupLocalService; + + @Reference + private Http _http; + @Reference private Language _language; + @Reference + private LayoutLocalService _layoutLocalService; + @Reference private Portal _portal; + @Reference + private SegmentsEntryLocalService _segmentsEntryLocalService; + + @Reference + private SegmentsExperienceLocalService _segmentsExperienceLocalService; + @Reference private SegmentsExperimentRelService _segmentsExperimentRelService; diff --git a/modules/dxp/apps/segments/segments-asah-connector/src/main/java/com/liferay/segments/asah/connector/internal/util/SegmentsExperimentUtil.java b/modules/dxp/apps/segments/segments-asah-connector/src/main/java/com/liferay/segments/asah/connector/internal/util/SegmentsExperimentUtil.java index 209fb4ac2dcc24..9597ec9a774d6a 100644 --- a/modules/dxp/apps/segments/segments-asah-connector/src/main/java/com/liferay/segments/asah/connector/internal/util/SegmentsExperimentUtil.java +++ b/modules/dxp/apps/segments/segments-asah-connector/src/main/java/com/liferay/segments/asah/connector/internal/util/SegmentsExperimentUtil.java @@ -141,6 +141,32 @@ public static JSONObject toSegmentsExperimentRelJSONObject( ); } + public static JSONObject toSegmentsExperimentRelJSONObject( + Locale locale, SegmentsExperimentRel segmentsExperimentRel) + throws PortalException { + + if (segmentsExperimentRel == null) { + return null; + } + + return JSONUtil.put( + "control", segmentsExperimentRel.isControl() + ).put( + "name", segmentsExperimentRel.getName(locale) + ).put( + "segmentsExperienceId", + String.valueOf(segmentsExperimentRel.getSegmentsExperienceId()) + ).put( + "segmentsExperimentId", + String.valueOf(segmentsExperimentRel.getSegmentsExperimentId()) + ).put( + "segmentsExperimentRelId", + String.valueOf(segmentsExperimentRel.getSegmentsExperimentRelId()) + ).put( + "split", segmentsExperimentRel.getSplit() + ); + } + public static JSONObject toStatusJSONObject(Locale locale, int status) { SegmentsExperimentConstants.Status segmentsExperimentConstantsStatus = SegmentsExperimentConstants.Status.parse(status);