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.BackgroundTaskResult;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.util.MapUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Time;
024    import com.liferay.portal.model.BackgroundTask;
025    import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
026    import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
027    import com.liferay.portlet.exportimport.service.ExportImportLocalServiceUtil;
028    
029    import java.io.File;
030    import java.io.Serializable;
031    
032    import java.util.Map;
033    
034    /**
035     * @author Daniel Kocsis
036     * @author Michael C. Han
037     */
038    public class LayoutExportBackgroundTaskExecutor
039            extends BaseExportImportBackgroundTaskExecutor {
040    
041            public LayoutExportBackgroundTaskExecutor() {
042                    setBackgroundTaskStatusMessageTranslator(
043                            new LayoutExportImportBackgroundTaskStatusMessageTranslator());
044            }
045    
046            @Override
047            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
048                    throws PortalException {
049    
050                    ExportImportConfiguration exportImportConfiguration =
051                            getExportImportConfiguration(backgroundTask);
052    
053                    Map<String, Serializable> settingsMap =
054                            exportImportConfiguration.getSettingsMap();
055    
056                    long userId = MapUtil.getLong(settingsMap, "userId");
057    
058                    StringBundler sb = new StringBundler(4);
059    
060                    sb.append(
061                            StringUtil.replace(
062                                    exportImportConfiguration.getName(), StringPool.SPACE,
063                                    StringPool.UNDERLINE));
064                    sb.append(StringPool.DASH);
065                    sb.append(Time.getShortTimestamp());
066                    sb.append(".lar");
067    
068                    File larFile = ExportImportLocalServiceUtil.exportLayoutsAsFile(
069                            exportImportConfiguration);
070    
071                    BackgroundTaskLocalServiceUtil.addBackgroundTaskAttachment(
072                            userId, backgroundTask.getBackgroundTaskId(), sb.toString(),
073                            larFile);
074    
075                    return BackgroundTaskResult.SUCCESS;
076            }
077    
078    }