Example usage for com.liferay.portal.kernel.backgroundtask BackgroundTaskManagerUtil amendBackgroundTask

List of usage examples for com.liferay.portal.kernel.backgroundtask BackgroundTaskManagerUtil amendBackgroundTask

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.backgroundtask BackgroundTaskManagerUtil amendBackgroundTask.

Prototype

public static BackgroundTask amendBackgroundTask(long backgroundTaskId,
            Map<String, Serializable> taskContextMap, int status, ServiceContext serviceContext) 

Source Link

Usage

From source file:com.liferay.exportimport.internal.background.task.BaseStagingBackgroundTaskExecutor.java

License:Open Source License

protected void markBackgroundTask(long backgroundTaskId, String backgroundTaskState) {

    BackgroundTask backgroundTask = BackgroundTaskManagerUtil.fetchBackgroundTask(backgroundTaskId);

    if ((backgroundTask == null) || Validator.isNull(backgroundTaskState)) {
        return;// w w  w  . j av a 2  s . c om
    }

    Map<String, Serializable> taskContextMap = backgroundTask.getTaskContextMap();

    if (taskContextMap == null) {
        taskContextMap = new HashMap<>();
    }

    taskContextMap.put(backgroundTaskState, Boolean.TRUE);

    backgroundTask.setTaskContextMap(taskContextMap);

    BackgroundTaskManagerUtil.amendBackgroundTask(backgroundTask.getBackgroundTaskId(), taskContextMap,
            backgroundTask.getStatus(), new ServiceContext());
}

From source file:com.liferay.exportimport.internal.background.task.LayoutStagingBackgroundTaskExecutor.java

License:Open Source License

@Override
public BackgroundTaskResult execute(BackgroundTask backgroundTask) throws PortalException {

    ExportImportConfiguration exportImportConfiguration = getExportImportConfiguration(backgroundTask);

    Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

    long userId = MapUtil.getLong(settingsMap, "userId");
    long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
    long sourceGroupId = MapUtil.getLong(settingsMap, "sourceGroupId");

    clearBackgroundTaskStatus(backgroundTask);

    File file = null;/*w  w  w  .  j  av  a 2  s. com*/
    MissingReferences missingReferences = null;

    try {
        ExportImportThreadLocal.setLayoutStagingInProcess(true);

        Group targetGroup = GroupLocalServiceUtil.fetchGroup(targetGroupId);

        if (targetGroup == null) {
            throw new NoSuchGroupException(
                    "Target group does not exists with the primary key " + targetGroupId);
        }

        Group sourceGroup = GroupLocalServiceUtil.getGroup(sourceGroupId);

        if (sourceGroup.hasStagingGroup()) {
            Group stagingGroup = sourceGroup.getStagingGroup();

            if (stagingGroup.getGroupId() == targetGroupId) {
                ExportImportThreadLocal.setInitialLayoutStagingInProcess(true);

                TrashEntryLocalServiceUtil.deleteEntries(sourceGroupId, true);
            }
        }

        ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(EVENT_PUBLICATION_LAYOUT_LOCAL_STARTED,
                PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS,
                String.valueOf(exportImportConfiguration.getExportImportConfigurationId()),
                exportImportConfiguration);

        boolean privateLayout = MapUtil.getBoolean(settingsMap, "privateLayout");

        initThreadLocals(sourceGroupId, privateLayout);

        file = ExportImportLocalServiceUtil.exportLayoutsAsFile(exportImportConfiguration);

        markBackgroundTask(backgroundTask.getBackgroundTaskId(), "exported");

        missingReferences = TransactionInvokerUtil.invoke(transactionConfig,
                new LayoutStagingImportCallable(backgroundTask.getBackgroundTaskId(), exportImportConfiguration,
                        file, sourceGroupId, targetGroupId, userId));

        ExportImportThreadLocal.setInitialLayoutStagingInProcess(false);
        ExportImportThreadLocal.setLayoutStagingInProcess(false);

        ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
                EVENT_PUBLICATION_LAYOUT_LOCAL_SUCCEEDED, PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS,
                String.valueOf(exportImportConfiguration.getExportImportConfigurationId()),
                exportImportConfiguration);
    } catch (Throwable t) {
        ExportImportThreadLocal.setInitialLayoutStagingInProcess(false);
        ExportImportThreadLocal.setLayoutStagingInProcess(false);

        ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(EVENT_PUBLICATION_LAYOUT_LOCAL_FAILED,
                PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS,
                String.valueOf(exportImportConfiguration.getExportImportConfigurationId()),
                exportImportConfiguration, t);

        if (_log.isDebugEnabled()) {
            _log.debug(t, t);
        } else if (_log.isWarnEnabled()) {
            _log.warn("Unable to publish layout: " + t.getMessage());
        }

        Group sourceGroup = GroupLocalServiceUtil.getGroup(sourceGroupId);

        if (sourceGroup.hasStagingGroup()) {
            ServiceContext serviceContext = new ServiceContext();

            serviceContext.setUserId(userId);

            StagingLocalServiceUtil.disableStaging(sourceGroup, serviceContext);

            List<BackgroundTask> queuedBackgroundTasks = BackgroundTaskManagerUtil.getBackgroundTasks(
                    sourceGroupId, LayoutStagingBackgroundTaskExecutor.class.getName(),
                    BackgroundTaskConstants.STATUS_QUEUED);

            for (BackgroundTask queuedBackgroundTask : queuedBackgroundTasks) {

                BackgroundTaskManagerUtil.amendBackgroundTask(queuedBackgroundTask.getBackgroundTaskId(), null,
                        BackgroundTaskConstants.STATUS_CANCELLED, new ServiceContext());
            }
        }

        deleteTempLarOnFailure(file);

        throw new SystemException(t);
    }

    deleteTempLarOnSuccess(file);

    return processMissingReferences(backgroundTask.getBackgroundTaskId(), missingReferences);
}