001    /**
002     * Copyright (c) 2000-present 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.portlet.exportimport.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.exception.PortalException;
022    import com.liferay.portal.kernel.json.JSONArray;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.util.FileUtil;
026    import com.liferay.portal.kernel.util.MapUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.BackgroundTask;
029    import com.liferay.portal.model.LayoutSet;
030    import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
031    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.service.ServiceContextThreadLocal;
034    import com.liferay.portal.service.UserLocalServiceUtil;
035    import com.liferay.portal.util.PropsValues;
036    import com.liferay.portlet.exportimport.lar.MissingReference;
037    import com.liferay.portlet.exportimport.lar.MissingReferences;
038    import com.liferay.portlet.exportimport.staging.StagingUtil;
039    
040    import java.io.File;
041    import java.io.Serializable;
042    
043    import java.util.HashMap;
044    import java.util.Map;
045    
046    /**
047     * @author Mate Thurzo
048     */
049    public abstract class BaseStagingBackgroundTaskExecutor
050            extends BaseExportImportBackgroundTaskExecutor {
051    
052            public BaseStagingBackgroundTaskExecutor() {
053                    setBackgroundTaskStatusMessageTranslator(
054                            new DefaultExportImportBackgroundTaskStatusMessageTranslator());
055    
056                    // Isolation level guarantees this will be serial in a group
057    
058                    setIsolationLevel(BackgroundTaskConstants.ISOLATION_LEVEL_GROUP);
059            }
060    
061            protected void clearBackgroundTaskStatus(BackgroundTask backgroundTask) {
062                    BackgroundTaskStatus backgroundTaskStatus =
063                            BackgroundTaskStatusRegistryUtil.getBackgroundTaskStatus(
064                                    backgroundTask.getBackgroundTaskId());
065    
066                    backgroundTaskStatus.clearAttributes();
067            }
068    
069            protected void deleteTempLarOnFailure(File file) {
070                    if (PropsValues.STAGING_DELETE_TEMP_LAR_ON_FAILURE) {
071                            FileUtil.delete(file);
072                    }
073                    else if ((file != null) && _log.isErrorEnabled()) {
074                            _log.error("Kept temporary LAR file " + file.getAbsolutePath());
075                    }
076            }
077    
078            protected void deleteTempLarOnSuccess(File file) {
079                    if (PropsValues.STAGING_DELETE_TEMP_LAR_ON_SUCCESS) {
080                            FileUtil.delete(file);
081                    }
082                    else if ((file != null) && _log.isDebugEnabled()) {
083                            _log.debug("Kept temporary LAR file " + file.getAbsolutePath());
084                    }
085            }
086    
087            protected void initThreadLocals(long groupId, boolean privateLayout)
088                    throws PortalException {
089    
090                    ServiceContext serviceContext =
091                            ServiceContextThreadLocal.popServiceContext();
092    
093                    if (serviceContext == null) {
094                            serviceContext = new ServiceContext();
095                    }
096    
097                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
098                            groupId, privateLayout);
099    
100                    serviceContext.setCompanyId(layoutSet.getCompanyId());
101                    serviceContext.setSignedIn(false);
102    
103                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
104                            layoutSet.getCompanyId());
105    
106                    serviceContext.setUserId(defaultUserId);
107    
108                    ServiceContextThreadLocal.pushServiceContext(serviceContext);
109            }
110    
111            protected void markBackgroundTask(
112                    long backgroundTaskId, String backgroundTaskState) {
113    
114                    BackgroundTask backgroundTask =
115                            BackgroundTaskLocalServiceUtil.fetchBackgroundTask(
116                                    backgroundTaskId);
117    
118                    if ((backgroundTask == null) || Validator.isNull(backgroundTaskState)) {
119                            return;
120                    }
121    
122                    Map<String, Serializable> taskContextMap =
123                            backgroundTask.getTaskContextMap();
124    
125                    if (taskContextMap == null) {
126                            taskContextMap = new HashMap<>();
127                    }
128    
129                    taskContextMap.put(backgroundTaskState, Boolean.TRUE);
130    
131                    backgroundTask.setTaskContextMap(taskContextMap);
132    
133                    BackgroundTaskLocalServiceUtil.updateBackgroundTask(backgroundTask);
134            }
135    
136            protected BackgroundTaskResult processMissingReferences(
137                    long backgroundTaskId, MissingReferences missingReferences) {
138    
139                    BackgroundTaskResult backgroundTaskResult = new BackgroundTaskResult(
140                            BackgroundTaskConstants.STATUS_SUCCESSFUL);
141    
142                    if (missingReferences == null) {
143                            return backgroundTaskResult;
144                    }
145    
146                    Map<String, MissingReference> weakMissingReferences =
147                            missingReferences.getWeakMissingReferences();
148    
149                    if (MapUtil.isNotEmpty(weakMissingReferences)) {
150                            BackgroundTask backgroundTask =
151                                    BackgroundTaskLocalServiceUtil.fetchBackgroundTask(
152                                            backgroundTaskId);
153    
154                            JSONArray jsonArray = StagingUtil.getWarningMessagesJSONArray(
155                                    getLocale(backgroundTask), weakMissingReferences,
156                                    backgroundTask.getTaskContextMap());
157    
158                            backgroundTaskResult.setStatusMessage(jsonArray.toString());
159                    }
160    
161                    return backgroundTaskResult;
162            }
163    
164            private static final Log _log = LogFactoryUtil.getLog(
165                    BaseStagingBackgroundTaskExecutor.class);
166    
167    }