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