001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.lar.backgroundtask;
016    
017    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskConstants;
018    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskResult;
019    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskStatus;
020    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskStatusRegistryUtil;
021    import com.liferay.portal.kernel.backgroundtask.BaseBackgroundTaskExecutor;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.json.JSONArray;
024    import com.liferay.portal.kernel.json.JSONFactoryUtil;
025    import com.liferay.portal.kernel.json.JSONObject;
026    import com.liferay.portal.kernel.lar.MissingReference;
027    import com.liferay.portal.kernel.lar.MissingReferences;
028    import com.liferay.portal.kernel.staging.StagingUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.model.BackgroundTask;
031    import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
032    
033    import java.io.Serializable;
034    
035    import java.util.Map;
036    
037    /**
038     * @author Mate Thurzo
039     */
040    public abstract class BaseStagingBackgroundTaskExecutor
041            extends BaseBackgroundTaskExecutor {
042    
043            public BaseStagingBackgroundTaskExecutor() {
044                    setBackgroundTaskStatusMessageTranslator(
045                            new DefaultExportImportBackgroundTaskStatusMessageTranslator());
046    
047                    setSerial(true);
048            }
049    
050            @Override
051            public String handleException(BackgroundTask backgroundTask, Exception e)
052                    throws SystemException {
053    
054                    JSONObject jsonObject = StagingUtil.getExceptionMessagesJSONObject(
055                            getLocale(backgroundTask), e, backgroundTask.getTaskContextMap());
056    
057                    return jsonObject.toString();
058            }
059    
060            protected void clearBackgroundTaskStatus(BackgroundTask backgroundTask) {
061                    BackgroundTaskStatus backgroundTaskStatus =
062                            BackgroundTaskStatusRegistryUtil.getBackgroundTaskStatus(
063                                    backgroundTask.getBackgroundTaskId());
064    
065                    backgroundTaskStatus.clearAttributes();
066            }
067    
068            protected BackgroundTask markBackgroundTask(
069                            BackgroundTask backgroundTask, String backgroundTaskState)
070                    throws SystemException {
071    
072                    Map<String, Serializable> taskContextMap =
073                            backgroundTask.getTaskContextMap();
074    
075                    if (Validator.isNull(backgroundTaskState)) {
076                            return backgroundTask;
077                    }
078    
079                    taskContextMap.put(backgroundTaskState, Boolean.TRUE);
080    
081                    backgroundTask.setTaskContext(
082                            JSONFactoryUtil.serialize(taskContextMap));
083    
084                    return BackgroundTaskLocalServiceUtil.updateBackgroundTask(
085                            backgroundTask);
086            }
087    
088            protected BackgroundTaskResult processMissingReferences(
089                    BackgroundTask backgroundTask, MissingReferences missingReferences) {
090    
091                    BackgroundTaskResult backgroundTaskResult = new BackgroundTaskResult(
092                            BackgroundTaskConstants.STATUS_SUCCESSFUL);
093    
094                    Map<String, MissingReference> weakMissingReferences =
095                            missingReferences.getWeakMissingReferences();
096    
097                    if ((weakMissingReferences != null) &&
098                            !weakMissingReferences.isEmpty()) {
099    
100                            JSONArray jsonArray = StagingUtil.getWarningMessagesJSONArray(
101                                    getLocale(backgroundTask), weakMissingReferences,
102                                    backgroundTask.getTaskContextMap());
103    
104                            backgroundTaskResult.setStatusMessage(jsonArray.toString());
105                    }
106    
107                    return backgroundTaskResult;
108            }
109    
110    }