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.kernel.util.UnicodeProperties;
025    import com.liferay.portal.model.BackgroundTask;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.service.GroupLocalServiceUtil;
028    import com.liferay.portal.service.LayoutLocalServiceUtil;
029    import com.liferay.portal.service.LayoutSetBranchLocalServiceUtil;
030    import com.liferay.portal.service.ServiceContext;
031    
032    import java.io.File;
033    import java.io.Serializable;
034    
035    import java.util.Date;
036    import java.util.Map;
037    
038    /**
039     * @author Julio Camarero
040     */
041    public class LayoutStagingBackgroundTaskExecutor
042            extends BaseStagingBackgroundTaskExecutor {
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                    long targetGroupId = MapUtil.getLong(taskContextMap, "targetGroupId");
053    
054                    StagingUtil.lockGroup(userId, targetGroupId);
055    
056                    long sourceGroupId = MapUtil.getLong(taskContextMap, "sourceGroupId");
057                    boolean privateLayout = MapUtil.getBoolean(
058                            taskContextMap, "privateLayout");
059                    long[] layoutIds = GetterUtil.getLongValues(
060                            taskContextMap.get("layoutIds"));
061                    Map<String, String[]> parameterMap =
062                            (Map<String, String[]>)taskContextMap.get("parameterMap");
063                    Date startDate = (Date)taskContextMap.get("startDate");
064                    Date endDate = (Date)taskContextMap.get("endDate");
065    
066                    clearBackgroundTaskStatus(backgroundTask);
067    
068                    File file = null;
069                    MissingReferences missingReferences = null;
070    
071                    try {
072                            Date lastPublishDate = endDate;
073    
074                            if (lastPublishDate == null) {
075                                    lastPublishDate = new Date();
076                            }
077    
078                            file = LayoutLocalServiceUtil.exportLayoutsAsFile(
079                                    sourceGroupId, privateLayout, layoutIds, parameterMap,
080                                    startDate, endDate);
081    
082                            backgroundTask = markBackgroundTask(backgroundTask, "exported");
083    
084                            missingReferences =
085                                    LayoutLocalServiceUtil.validateImportLayoutsFile(
086                                            userId, targetGroupId, privateLayout, parameterMap, file);
087    
088                            backgroundTask = markBackgroundTask(backgroundTask, "validated");
089    
090                            LayoutLocalServiceUtil.importLayouts(
091                                    userId, targetGroupId, privateLayout, parameterMap, file);
092    
093                            initLayoutSetBranches(userId, sourceGroupId, targetGroupId);
094    
095                            boolean updateLastPublishDate = MapUtil.getBoolean(
096                                    parameterMap, PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE);
097    
098                            if (updateLastPublishDate) {
099                                    StagingUtil.updateLastPublishDate(
100                                            sourceGroupId, privateLayout, lastPublishDate);
101                            }
102                    }
103                    finally {
104                            FileUtil.delete(file);
105    
106                            StagingUtil.unlockGroup(targetGroupId);
107                    }
108    
109                    return processMissingReferences(backgroundTask, missingReferences);
110            }
111    
112            protected void initLayoutSetBranches(
113                            long userId, long sourceGroupId, long targetGroupId)
114                    throws Exception {
115    
116                    Group sourceGroup = GroupLocalServiceUtil.getGroup(sourceGroupId);
117    
118                    if (!sourceGroup.hasStagingGroup()) {
119                            return;
120                    }
121    
122                    LayoutSetBranchLocalServiceUtil.deleteLayoutSetBranches(
123                            targetGroupId, false, true);
124                    LayoutSetBranchLocalServiceUtil.deleteLayoutSetBranches(
125                            targetGroupId, true, true);
126    
127                    UnicodeProperties typeSettingsProperties =
128                            sourceGroup.getTypeSettingsProperties();
129    
130                    boolean branchingPrivate = GetterUtil.getBoolean(
131                            typeSettingsProperties.getProperty("branchingPrivate"));
132                    boolean branchingPublic = GetterUtil.getBoolean(
133                            typeSettingsProperties.getProperty("branchingPublic"));
134    
135                    ServiceContext serviceContext = new ServiceContext();
136    
137                    serviceContext.setUserId(userId);
138    
139                    StagingUtil.checkDefaultLayoutSetBranches(
140                            userId, sourceGroup, branchingPublic, branchingPrivate, false,
141                            serviceContext);
142            }
143    
144    }