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