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.model.BackgroundTask;
030    import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
031    
032    import java.io.Serializable;
033    
034    import java.util.Map;
035    
036    /**
037     * @author Mate Thurzo
038     */
039    public abstract class BaseStagingBackgroundTaskExecutor
040            extends BaseBackgroundTaskExecutor {
041    
042            public BaseStagingBackgroundTaskExecutor() {
043                    setBackgroundTaskStatusMessageTranslator(
044                            new DefaultExportImportBackgroundTaskStatusMessageTranslator());
045    
046                    setSerial(true);
047            }
048    
049            @Override
050            public String handleException(BackgroundTask backgroundTask, Exception e) {
051                    JSONObject jsonObject = StagingUtil.getExceptionMessagesJSONObject(
052                            getLocale(backgroundTask), e, backgroundTask.getTaskContextMap());
053    
054                    return jsonObject.toString();
055            }
056    
057            protected void clearBackgroundTaskStatus(BackgroundTask backgroundTask) {
058                    BackgroundTaskStatus backgroundTaskStatus =
059                            BackgroundTaskStatusRegistryUtil.getBackgroundTaskStatus(
060                                    backgroundTask.getBackgroundTaskId());
061    
062                    backgroundTaskStatus.clearAttributes();
063            }
064    
065            protected BackgroundTask markValidatedBackgroundTask(
066                            BackgroundTask backgroundTask)
067                    throws SystemException {
068    
069                    Map<String, Serializable> taskContextMap =
070                            backgroundTask.getTaskContextMap();
071    
072                    taskContextMap.put("validated", Boolean.TRUE);
073    
074                    backgroundTask.setTaskContext(
075                            JSONFactoryUtil.serialize(taskContextMap));
076    
077                    return BackgroundTaskLocalServiceUtil.updateBackgroundTask(
078                            backgroundTask);
079            }
080    
081            protected BackgroundTaskResult processMissingReferences(
082                    BackgroundTask backgroundTask, MissingReferences missingReferences) {
083    
084                    BackgroundTaskResult backgroundTaskResult = new BackgroundTaskResult(
085                            BackgroundTaskConstants.STATUS_SUCCESSFUL);
086    
087                    Map<String, MissingReference> weakMissingReferences =
088                            missingReferences.getWeakMissingReferences();
089    
090                    if ((weakMissingReferences != null) &&
091                            !weakMissingReferences.isEmpty()) {
092    
093                            JSONArray jsonArray = StagingUtil.getWarningMessagesJSONArray(
094                                    getLocale(backgroundTask), weakMissingReferences,
095                                    backgroundTask.getTaskContextMap());
096    
097                            backgroundTaskResult.setStatusMessage(jsonArray.toString());
098                    }
099    
100                    return backgroundTaskResult;
101            }
102    
103    }