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.json.JSONObject;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
021    import com.liferay.portal.kernel.staging.StagingUtil;
022    import com.liferay.portal.kernel.util.DateRange;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.MapUtil;
025    import com.liferay.portal.model.BackgroundTask;
026    import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
027    import com.liferay.portal.service.LayoutLocalServiceUtil;
028    
029    import java.io.File;
030    import java.io.Serializable;
031    
032    import java.util.Date;
033    import java.util.Map;
034    
035    /**
036     * @author Daniel Kocsis
037     * @author Michael C. Han
038     */
039    public class LayoutExportBackgroundTaskExecutor
040            extends BaseBackgroundTaskExecutor {
041    
042            public LayoutExportBackgroundTaskExecutor() {
043                    setBackgroundTaskStatusMessageTranslator(
044                            new LayoutExportImportBackgroundTaskStatusMessageTranslator());
045                    setSerial(true);
046            }
047    
048            @Override
049            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
050                    throws Exception {
051    
052                    Map<String, Serializable> taskContextMap =
053                            backgroundTask.getTaskContextMap();
054    
055                    long userId = MapUtil.getLong(taskContextMap, "userId");
056                    String fileName = MapUtil.getString(taskContextMap, "fileName");
057    
058                    long groupId = MapUtil.getLong(taskContextMap, "groupId");
059                    boolean privateLayout = MapUtil.getBoolean(
060                            taskContextMap, "privateLayout");
061                    long[] layoutIds = GetterUtil.getLongValues(
062                            taskContextMap.get("layoutIds"));
063                    Map<String, String[]> parameterMap =
064                            (Map<String, String[]>)taskContextMap.get("parameterMap");
065                    Date startDate = (Date)taskContextMap.get("startDate");
066                    Date endDate = (Date)taskContextMap.get("endDate");
067    
068                    File larFile = LayoutLocalServiceUtil.exportLayoutsAsFile(
069                            groupId, privateLayout, layoutIds, parameterMap, startDate,
070                            endDate);
071    
072                    BackgroundTaskLocalServiceUtil.addBackgroundTaskAttachment(
073                            userId, backgroundTask.getBackgroundTaskId(), fileName, larFile);
074    
075                    boolean updateLastPublishDate = MapUtil.getBoolean(
076                            parameterMap, PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE);
077    
078                    if (updateLastPublishDate) {
079                            DateRange dateRange = new DateRange(startDate, endDate);
080    
081                            StagingUtil.updateLastPublishDate(
082                                    groupId, privateLayout, dateRange, endDate);
083                    }
084    
085                    return BackgroundTaskResult.SUCCESS;
086            }
087    
088            @Override
089            public String handleException(BackgroundTask backgroundTask, Exception e) {
090                    JSONObject jsonObject = StagingUtil.getExceptionMessagesJSONObject(
091                            getLocale(backgroundTask), e, backgroundTask.getTaskContextMap());
092    
093                    return jsonObject.toString();
094            }
095    
096    }