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.NoSuchLayoutException;
018    import com.liferay.portal.RemoteExportException;
019    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskResult;
020    import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
021    import com.liferay.portal.kernel.lar.MissingReferences;
022    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
023    import com.liferay.portal.kernel.staging.StagingUtil;
024    import com.liferay.portal.kernel.util.FileUtil;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.MapUtil;
027    import com.liferay.portal.kernel.util.StreamUtil;
028    import com.liferay.portal.model.BackgroundTask;
029    import com.liferay.portal.model.Layout;
030    import com.liferay.portal.security.auth.HttpPrincipal;
031    import com.liferay.portal.service.LayoutLocalServiceUtil;
032    import com.liferay.portal.service.http.LayoutServiceHttp;
033    import com.liferay.portal.service.http.StagingServiceHttp;
034    import com.liferay.portal.util.PropsValues;
035    
036    import java.io.File;
037    import java.io.FileInputStream;
038    import java.io.Serializable;
039    
040    import java.util.ArrayList;
041    import java.util.Date;
042    import java.util.List;
043    import java.util.Map;
044    
045    /**
046     * @author Mate Thurzo
047     */
048    public class LayoutRemoteStagingBackgroundTaskExecutor
049            extends BaseStagingBackgroundTaskExecutor {
050    
051            @Override
052            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
053                    throws Exception {
054    
055                    Map<String, Serializable> taskContextMap =
056                            backgroundTask.getTaskContextMap();
057    
058                    long sourceGroupId = MapUtil.getLong(taskContextMap, "groupId");
059                    boolean privateLayout = MapUtil.getBoolean(
060                            taskContextMap, "privateLayout");
061                    Map<Long, Boolean> layoutIdMap = (Map<Long, Boolean>)taskContextMap.get(
062                            "layoutIdMap");
063                    Map<String, String[]> parameterMap =
064                            (Map<String, String[]>)taskContextMap.get("parameterMap");
065                    long remoteGroupId = MapUtil.getLong(taskContextMap, "remoteGroupId");
066                    Date startDate = (Date)taskContextMap.get("startDate");
067                    Date endDate = (Date)taskContextMap.get("endDate");
068                    HttpPrincipal httpPrincipal = (HttpPrincipal)taskContextMap.get(
069                            "httpPrincipal");
070    
071                    clearBackgroundTaskStatus(backgroundTask);
072    
073                    long stagingRequestId = 0;
074    
075                    File file = null;
076                    FileInputStream fileInputStream = null;
077                    MissingReferences missingReferences = null;
078    
079                    try {
080                            file = exportLayoutsAsFile(
081                                    sourceGroupId, privateLayout, layoutIdMap, parameterMap,
082                                    remoteGroupId, startDate, endDate, httpPrincipal);
083    
084                            String checksum = FileUtil.getMD5Checksum(file);
085    
086                            fileInputStream = new FileInputStream(file);
087    
088                            stagingRequestId = StagingServiceHttp.createStagingRequest(
089                                    httpPrincipal, remoteGroupId, checksum);
090    
091                            byte[] bytes =
092                                    new byte[PropsValues.STAGING_REMOTE_TRANSFER_BUFFER_SIZE];
093    
094                            int i = 0;
095    
096                            while ((i = fileInputStream.read(bytes)) >= 0) {
097                                    if (i < PropsValues.STAGING_REMOTE_TRANSFER_BUFFER_SIZE) {
098                                            byte[] tempBytes = new byte[i];
099    
100                                            System.arraycopy(bytes, 0, tempBytes, 0, i);
101    
102                                            StagingServiceHttp.updateStagingRequest(
103                                                    httpPrincipal, stagingRequestId, file.getName(),
104                                                    tempBytes);
105                                    }
106                                    else {
107                                            StagingServiceHttp.updateStagingRequest(
108                                                    httpPrincipal, stagingRequestId, file.getName(), bytes);
109                                    }
110    
111                                    bytes =
112                                            new byte[PropsValues.STAGING_REMOTE_TRANSFER_BUFFER_SIZE];
113                            }
114    
115                            backgroundTask = markBackgroundTask(backgroundTask, "exported");
116    
117                            missingReferences = StagingServiceHttp.validateStagingRequest(
118                                    httpPrincipal, stagingRequestId, privateLayout, parameterMap);
119    
120                            backgroundTask = markBackgroundTask(backgroundTask, "validated");
121    
122                            StagingServiceHttp.publishStagingRequest(
123                                    httpPrincipal, stagingRequestId, privateLayout, parameterMap);
124    
125                            boolean updateLastPublishDate = MapUtil.getBoolean(
126                                    parameterMap, PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE);
127    
128                            if (updateLastPublishDate) {
129                                    Date lastPublishDate = endDate;
130    
131                                    if (lastPublishDate == null) {
132                                            lastPublishDate = new Date();
133                                    }
134    
135                                    StagingUtil.updateLastPublishDate(
136                                            sourceGroupId, privateLayout, lastPublishDate);
137                            }
138                    }
139                    finally {
140                            StreamUtil.cleanUp(fileInputStream);
141    
142                            FileUtil.delete(file);
143    
144                            if (stagingRequestId > 0) {
145                                    StagingServiceHttp.cleanUpStagingRequest(
146                                            httpPrincipal, stagingRequestId);
147                            }
148                    }
149    
150                    return processMissingReferences(backgroundTask, missingReferences);
151            }
152    
153            protected File exportLayoutsAsFile(
154                            long sourceGroupId, boolean privateLayout,
155                            Map<Long, Boolean> layoutIdMap, Map<String, String[]> parameterMap,
156                            long remoteGroupId, Date startDate, Date endDate,
157                            HttpPrincipal httpPrincipal)
158                    throws Exception {
159    
160                    if ((layoutIdMap == null) || layoutIdMap.isEmpty()) {
161                            return LayoutLocalServiceUtil.exportLayoutsAsFile(
162                                    sourceGroupId, privateLayout, null, parameterMap, startDate,
163                                    endDate);
164                    }
165                    else {
166                            List<Layout> layouts = new ArrayList<Layout>();
167    
168                            for (Map.Entry<Long, Boolean> entry : layoutIdMap.entrySet()) {
169                                    long plid = GetterUtil.getLong(String.valueOf(entry.getKey()));
170                                    boolean includeChildren = entry.getValue();
171    
172                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
173    
174                                    if (!layouts.contains(layout)) {
175                                            layouts.add(layout);
176                                    }
177    
178                                    List<Layout> parentLayouts = getMissingRemoteParentLayouts(
179                                            httpPrincipal, layout, remoteGroupId);
180    
181                                    for (Layout parentLayout : parentLayouts) {
182                                            if (!layouts.contains(parentLayout)) {
183                                                    layouts.add(parentLayout);
184                                            }
185                                    }
186    
187                                    if (includeChildren) {
188                                            for (Layout childLayout : layout.getAllChildren()) {
189                                                    if (!layouts.contains(childLayout)) {
190                                                            layouts.add(childLayout);
191                                                    }
192                                            }
193                                    }
194                            }
195    
196                            long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layouts);
197    
198                            if (layoutIds.length <= 0) {
199                                    throw new RemoteExportException(
200                                            RemoteExportException.NO_LAYOUTS);
201                            }
202    
203                            return LayoutLocalServiceUtil.exportLayoutsAsFile(
204                                    sourceGroupId, privateLayout, layoutIds, parameterMap,
205                                    startDate, endDate);
206                    }
207            }
208    
209            /**
210             * @see com.liferay.portal.staging.StagingImpl#getMissingParentLayouts(
211             *      Layout, long)
212             */
213            protected List<Layout> getMissingRemoteParentLayouts(
214                            HttpPrincipal httpPrincipal, Layout layout, long remoteGroupId)
215                    throws Exception {
216    
217                    List<Layout> missingRemoteParentLayouts = new ArrayList<Layout>();
218    
219                    long parentLayoutId = layout.getParentLayoutId();
220    
221                    while (parentLayoutId > 0) {
222                            Layout parentLayout = LayoutLocalServiceUtil.getLayout(
223                                    layout.getGroupId(), layout.isPrivateLayout(), parentLayoutId);
224    
225                            try {
226                                    LayoutServiceHttp.getLayoutByUuidAndGroupId(
227                                            httpPrincipal, parentLayout.getUuid(), remoteGroupId,
228                                            parentLayout.getPrivateLayout());
229    
230                                    // If one parent is found all others are assumed to exist
231    
232                                    break;
233                            }
234                            catch (NoSuchLayoutException nsle) {
235                                    missingRemoteParentLayouts.add(parentLayout);
236    
237                                    parentLayoutId = parentLayout.getParentLayoutId();
238                            }
239                    }
240    
241                    return missingRemoteParentLayouts;
242            }
243    
244    }