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.portal.lar.backgroundtask;
016    
017    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskResult;
018    import com.liferay.portal.kernel.backgroundtask.BaseBackgroundTaskExecutor;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.lar.ExportImportDateUtil;
022    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
023    import com.liferay.portal.kernel.staging.StagingUtil;
024    import com.liferay.portal.kernel.util.DateRange;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.MapUtil;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.util.Time;
031    import com.liferay.portal.model.BackgroundTask;
032    import com.liferay.portal.model.ExportImportConfiguration;
033    import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
034    import com.liferay.portal.service.ExportImportConfigurationLocalServiceUtil;
035    import com.liferay.portal.service.LayoutLocalServiceUtil;
036    
037    import java.io.File;
038    import java.io.Serializable;
039    
040    import java.util.Map;
041    
042    /**
043     * @author Daniel Kocsis
044     * @author Michael C. Han
045     */
046    public class LayoutExportBackgroundTaskExecutor
047            extends BaseBackgroundTaskExecutor {
048    
049            public LayoutExportBackgroundTaskExecutor() {
050                    setBackgroundTaskStatusMessageTranslator(
051                            new LayoutExportImportBackgroundTaskStatusMessageTranslator());
052                    setSerial(true);
053            }
054    
055            @Override
056            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
057                    throws PortalException {
058    
059                    Map<String, Serializable> taskContextMap =
060                            backgroundTask.getTaskContextMap();
061    
062                    long exportImportConfigurationId = MapUtil.getLong(
063                            taskContextMap, "exportImportConfigurationId");
064    
065                    ExportImportConfiguration exportImportConfiguration =
066                            ExportImportConfigurationLocalServiceUtil.
067                                    getExportImportConfiguration(exportImportConfigurationId);
068    
069                    Map<String, Serializable> settingsMap =
070                            exportImportConfiguration.getSettingsMap();
071    
072                    long userId = MapUtil.getLong(settingsMap, "userId");
073                    long groupId = MapUtil.getLong(settingsMap, "sourceGroupId");
074                    boolean privateLayout = MapUtil.getBoolean(
075                            settingsMap, "privateLayout");
076                    long[] layoutIds = GetterUtil.getLongValues(
077                            settingsMap.get("layoutIds"));
078                    Map<String, String[]> parameterMap =
079                            (Map<String, String[]>)settingsMap.get("parameterMap");
080                    DateRange dateRange = ExportImportDateUtil.getDateRange(
081                            exportImportConfiguration, ExportImportDateUtil.RANGE_ALL);
082    
083                    StringBundler sb = new StringBundler(4);
084    
085                    sb.append(
086                            StringUtil.replace(
087                                    exportImportConfiguration.getName(), StringPool.SPACE,
088                                    StringPool.UNDERLINE));
089                    sb.append(StringPool.DASH);
090                    sb.append(Time.getShortTimestamp());
091                    sb.append(".lar");
092    
093                    File larFile = LayoutLocalServiceUtil.exportLayoutsAsFile(
094                            groupId, privateLayout, layoutIds, parameterMap,
095                            dateRange.getStartDate(), dateRange.getEndDate());
096    
097                    BackgroundTaskLocalServiceUtil.addBackgroundTaskAttachment(
098                            userId, backgroundTask.getBackgroundTaskId(), sb.toString(),
099                            larFile);
100    
101                    boolean updateLastPublishDate = MapUtil.getBoolean(
102                            parameterMap, PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE);
103    
104                    if (updateLastPublishDate) {
105                            ExportImportDateUtil.updateLastPublishDate(
106                                    groupId, privateLayout, dateRange, dateRange.getEndDate());
107                    }
108    
109                    return BackgroundTaskResult.SUCCESS;
110            }
111    
112            @Override
113            public String handleException(BackgroundTask backgroundTask, Exception e) {
114                    JSONObject jsonObject = StagingUtil.getExceptionMessagesJSONObject(
115                            getLocale(backgroundTask), e, backgroundTask.getTaskContextMap());
116    
117                    return jsonObject.toString();
118            }
119    
120    }