diff --git a/modules/dxp/apps/segments/segments-experiment-web/src/main/java/com/liferay/segments/experiment/web/internal/portlet/action/EditSegmentsExperimentStatusMVCActionCommand.java b/modules/dxp/apps/segments/segments-asah-connector/src/main/java/com/liferay/segments/asah/connector/internal/portlet/action/EditSegmentsExperimentStatusMVCActionCommand.java similarity index 57% rename from modules/dxp/apps/segments/segments-experiment-web/src/main/java/com/liferay/segments/experiment/web/internal/portlet/action/EditSegmentsExperimentStatusMVCActionCommand.java rename to modules/dxp/apps/segments/segments-asah-connector/src/main/java/com/liferay/segments/asah/connector/internal/portlet/action/EditSegmentsExperimentStatusMVCActionCommand.java index d0914474093814..1d8b088ce011b4 100644 --- a/modules/dxp/apps/segments/segments-experiment-web/src/main/java/com/liferay/segments/experiment/web/internal/portlet/action/EditSegmentsExperimentStatusMVCActionCommand.java +++ b/modules/dxp/apps/segments/segments-asah-connector/src/main/java/com/liferay/segments/asah/connector/internal/portlet/action/EditSegmentsExperimentStatusMVCActionCommand.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,18 +15,31 @@ 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.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.experiment.web.internal.util.SegmentsExperimentUtil; import com.liferay.segments.model.SegmentsExperiment; +import com.liferay.segments.service.SegmentsEntryLocalService; +import com.liferay.segments.service.SegmentsExperienceLocalService; import com.liferay.segments.service.SegmentsExperimentService; +import java.util.Map; import java.util.concurrent.Callable; import javax.portlet.ActionRequest; @@ -33,7 +47,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; /** @@ -49,6 +65,17 @@ public class EditSegmentsExperimentStatusMVCActionCommand 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) @@ -97,18 +124,53 @@ private JSONObject _editSegmentsExperimentStatus( (ThemeDisplay)actionRequest.getAttribute( WebKeys.THEME_DISPLAY); + long winnerSegmentsExperienceId = ParamUtil.getLong( + actionRequest, "winnerSegmentsExperienceId", -1); + SegmentsExperiment segmentsExperiment = _segmentsExperimentService.updateSegmentsExperimentStatus( ParamUtil.getLong( actionRequest, "segmentsExperimentId"), - ParamUtil.getLong( - actionRequest, "winnerSegmentsExperienceId", -1), + winnerSegmentsExperienceId, ParamUtil.getInteger(actionRequest, "status")); - return SegmentsExperimentUtil.toSegmentsExperimentJSONObject( + AnalyticsConfiguration analyticsConfiguration = _analyticsSettingsManager.getAnalyticsConfiguration( - themeDisplay.getCompanyId()), - themeDisplay.getLocale(), segmentsExperiment); + themeDisplay.getCompanyId()); + + if (((segmentsExperiment.getStatus() == + SegmentsExperimentConstants.STATUS_COMPLETED) || + (segmentsExperiment.getStatus() == + SegmentsExperimentConstants.STATUS_TERMINATED)) && + (winnerSegmentsExperienceId != -1) && + (winnerSegmentsExperienceId == + segmentsExperiment.getWinnerSegmentsExperienceId())) { + + 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); + + _segmentsExperimentService.deleteSegmentsExperiment( + segmentsExperiment, false); + + segmentsExperiment = null; + } + + return SegmentsExperimentUtil.toSegmentsExperimentJSONObject( + analyticsConfiguration, themeDisplay.getLocale(), + segmentsExperiment); }); } @@ -122,12 +184,32 @@ private JSONObject _editSegmentsExperimentStatus( @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 SegmentsExperimentService _segmentsExperimentService;