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.BackgroundTaskResult;
018    import com.liferay.portal.kernel.backgroundtask.BaseBackgroundTaskExecutor;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.MapUtil;
021    import com.liferay.portal.model.BackgroundTask;
022    import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
023    import com.liferay.portal.service.LayoutLocalServiceUtil;
024    
025    import java.io.File;
026    import java.io.Serializable;
027    
028    import java.util.Date;
029    import java.util.Map;
030    
031    /**
032     * @author Daniel Kocsis
033     * @author Michael C. Han
034     */
035    public class LayoutExportBackgroundTaskExecutor
036            extends BaseBackgroundTaskExecutor {
037    
038            public LayoutExportBackgroundTaskExecutor() {
039                    setBackgroundTaskStatusMessageTranslator(
040                            new ExportBackgroundTaskStatusMessageTranslator());
041                    setSerial(true);
042            }
043    
044            @Override
045            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
046                    throws Exception {
047    
048                    Map<String, Serializable> taskContextMap =
049                            backgroundTask.getTaskContextMap();
050    
051                    long userId = MapUtil.getLong(taskContextMap, "userId");
052                    String fileName = MapUtil.getString(taskContextMap, "fileName");
053    
054                    long groupId = MapUtil.getLong(taskContextMap, "groupId");
055                    boolean privateLayout = MapUtil.getBoolean(
056                            taskContextMap, "privateLayout");
057                    long[] layoutIds = GetterUtil.getLongValues(
058                            taskContextMap.get("layoutIds"));
059                    Map<String, String[]> parameterMap =
060                            (Map<String, String[]>)taskContextMap.get("parameterMap");
061                    Date startDate = (Date)taskContextMap.get("startDate");
062                    Date endDate = (Date)taskContextMap.get("endDate");
063    
064                    File larFile = LayoutLocalServiceUtil.exportLayoutsAsFile(
065                            groupId, privateLayout, layoutIds, parameterMap, startDate,
066                            endDate);
067    
068                    BackgroundTaskLocalServiceUtil.addBackgroundTaskAttachment(
069                            userId, backgroundTask.getBackgroundTaskId(), fileName, larFile);
070    
071                    return BackgroundTaskResult.SUCCESS;
072            }
073    
074    }