001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.transaction.Propagation;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.model.BackgroundTask;
032    import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
033    import com.liferay.portal.spring.transaction.TransactionAttributeBuilder;
034    
035    import java.io.Serializable;
036    
037    import java.util.Map;
038    
039    import org.springframework.transaction.interceptor.TransactionAttribute;
040    
041    /**
042     * @author Mate Thurzo
043     */
044    public abstract class BaseStagingBackgroundTaskExecutor
045            extends BaseBackgroundTaskExecutor {
046    
047            public BaseStagingBackgroundTaskExecutor() {
048                    setBackgroundTaskStatusMessageTranslator(
049                            new DefaultExportImportBackgroundTaskStatusMessageTranslator());
050    
051                    setSerial(true);
052            }
053    
054            @Override
055            public String handleException(BackgroundTask backgroundTask, Exception e) {
056                    JSONObject jsonObject = StagingUtil.getExceptionMessagesJSONObject(
057                            getLocale(backgroundTask), e, backgroundTask.getTaskContextMap());
058    
059                    return jsonObject.toString();
060            }
061    
062            protected void clearBackgroundTaskStatus(BackgroundTask backgroundTask) {
063                    BackgroundTaskStatus backgroundTaskStatus =
064                            BackgroundTaskStatusRegistryUtil.getBackgroundTaskStatus(
065                                    backgroundTask.getBackgroundTaskId());
066    
067                    backgroundTaskStatus.clearAttributes();
068            }
069    
070            protected BackgroundTask markBackgroundTask(
071                            BackgroundTask backgroundTask, String backgroundTaskState)
072                    throws SystemException {
073    
074                    Map<String, Serializable> taskContextMap =
075                            backgroundTask.getTaskContextMap();
076    
077                    if (Validator.isNull(backgroundTaskState)) {
078                            return backgroundTask;
079                    }
080    
081                    taskContextMap.put(backgroundTaskState, Boolean.TRUE);
082    
083                    backgroundTask.setTaskContext(
084                            JSONFactoryUtil.serialize(taskContextMap));
085    
086                    return BackgroundTaskLocalServiceUtil.updateBackgroundTask(
087                            backgroundTask);
088            }
089    
090            protected BackgroundTaskResult processMissingReferences(
091                    BackgroundTask backgroundTask, MissingReferences missingReferences) {
092    
093                    BackgroundTaskResult backgroundTaskResult = new BackgroundTaskResult(
094                            BackgroundTaskConstants.STATUS_SUCCESSFUL);
095    
096                    Map<String, MissingReference> weakMissingReferences =
097                            missingReferences.getWeakMissingReferences();
098    
099                    if ((weakMissingReferences != null) &&
100                            !weakMissingReferences.isEmpty()) {
101    
102                            JSONArray jsonArray = StagingUtil.getWarningMessagesJSONArray(
103                                    getLocale(backgroundTask), weakMissingReferences,
104                                    backgroundTask.getTaskContextMap());
105    
106                            backgroundTaskResult.setStatusMessage(jsonArray.toString());
107                    }
108    
109                    return backgroundTaskResult;
110            }
111    
112            protected TransactionAttribute transactionAttribute =
113                    TransactionAttributeBuilder.build(
114                            Propagation.REQUIRED, new Class<?>[] {Exception.class});
115    
116    }