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                    JSONObject jsonObject = StagingUtil.getExceptionMessagesJSONObject(
053                            getLocale(backgroundTask), e, backgroundTask.getTaskContextMap());
054    
055                    return jsonObject.toString();
056            }
057    
058            protected void clearBackgroundTaskStatus(BackgroundTask backgroundTask) {
059                    BackgroundTaskStatus backgroundTaskStatus =
060                            BackgroundTaskStatusRegistryUtil.getBackgroundTaskStatus(
061                                    backgroundTask.getBackgroundTaskId());
062    
063                    backgroundTaskStatus.clearAttributes();
064            }
065    
066            protected BackgroundTask markBackgroundTask(
067                            BackgroundTask backgroundTask, String backgroundTaskState)
068                    throws SystemException {
069    
070                    Map<String, Serializable> taskContextMap =
071                            backgroundTask.getTaskContextMap();
072    
073                    if (Validator.isNull(backgroundTaskState)) {
074                            return backgroundTask;
075                    }
076    
077                    taskContextMap.put(backgroundTaskState, Boolean.TRUE);
078    
079                    backgroundTask.setTaskContext(
080                            JSONFactoryUtil.serialize(taskContextMap));
081    
082                    return BackgroundTaskLocalServiceUtil.updateBackgroundTask(
083                            backgroundTask);
084            }
085    
086            protected BackgroundTaskResult processMissingReferences(
087                    BackgroundTask backgroundTask, MissingReferences missingReferences) {
088    
089                    BackgroundTaskResult backgroundTaskResult = new BackgroundTaskResult(
090                            BackgroundTaskConstants.STATUS_SUCCESSFUL);
091    
092                    Map<String, MissingReference> weakMissingReferences =
093                            missingReferences.getWeakMissingReferences();
094    
095                    if ((weakMissingReferences != null) &&
096                            !weakMissingReferences.isEmpty()) {
097    
098                            JSONArray jsonArray = StagingUtil.getWarningMessagesJSONArray(
099                                    getLocale(backgroundTask), weakMissingReferences,
100                                    backgroundTask.getTaskContextMap());
101    
102                            backgroundTaskResult.setStatusMessage(jsonArray.toString());
103                    }
104    
105                    return backgroundTaskResult;
106            }
107    
108    }