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.lar.MissingReferences;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
020    import com.liferay.portal.kernel.staging.StagingUtil;
021    import com.liferay.portal.kernel.util.FileUtil;
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.LayoutLocalServiceUtil;
026    
027    import java.io.File;
028    import java.io.Serializable;
029    
030    import java.util.Date;
031    import java.util.Map;
032    
033    /**
034     * @author Julio Camarero
035     */
036    public class LayoutStagingBackgroundTaskExecutor
037            extends BaseStagingBackgroundTaskExecutor {
038    
039            @Override
040            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
041                    throws Exception {
042    
043                    Map<String, Serializable> taskContextMap =
044                            backgroundTask.getTaskContextMap();
045    
046                    long userId = MapUtil.getLong(taskContextMap, "userId");
047                    long targetGroupId = MapUtil.getLong(taskContextMap, "targetGroupId");
048    
049                    StagingUtil.lockGroup(userId, targetGroupId);
050    
051                    long sourceGroupId = MapUtil.getLong(taskContextMap, "sourceGroupId");
052                    boolean privateLayout = MapUtil.getBoolean(
053                            taskContextMap, "privateLayout");
054                    long[] layoutIds = GetterUtil.getLongValues(
055                            taskContextMap.get("layoutIds"));
056                    Map<String, String[]> parameterMap =
057                            (Map<String, String[]>)taskContextMap.get("parameterMap");
058                    Date startDate = (Date)taskContextMap.get("startDate");
059                    Date endDate = (Date)taskContextMap.get("endDate");
060    
061                    clearBackgroundTaskStatus(backgroundTask);
062    
063                    File file = null;
064                    MissingReferences missingReferences = null;
065    
066                    try {
067                            Date lastPublishDate = endDate;
068    
069                            if (lastPublishDate == null) {
070                                    lastPublishDate = new Date();
071                            }
072    
073                            file = LayoutLocalServiceUtil.exportLayoutsAsFile(
074                                    sourceGroupId, privateLayout, layoutIds, parameterMap,
075                                    startDate, endDate);
076    
077                            backgroundTask = markBackgroundTask(backgroundTask, "exported");
078    
079                            missingReferences =
080                                    LayoutLocalServiceUtil.validateImportLayoutsFile(
081                                            userId, targetGroupId, privateLayout, parameterMap, file);
082    
083                            backgroundTask = markBackgroundTask(backgroundTask, "validated");
084    
085                            LayoutLocalServiceUtil.importLayouts(
086                                    userId, targetGroupId, privateLayout, parameterMap, file);
087    
088                            boolean updateLastPublishDate = MapUtil.getBoolean(
089                                    parameterMap, PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE);
090    
091                            if (updateLastPublishDate) {
092                                    StagingUtil.updateLastPublishDate(
093                                            sourceGroupId, privateLayout, lastPublishDate);
094                            }
095                    }
096                    finally {
097                            FileUtil.delete(file);
098    
099                            StagingUtil.unlockGroup(targetGroupId);
100                    }
101    
102                    return processMissingReferences(backgroundTask, missingReferences);
103            }
104    
105    }