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                                    StagingUtil.updateLastPublishDate(
130                                            sourceGroupId, privateLayout, endDate);
131                            }
132                    }
133                    finally {
134                            StreamUtil.cleanUp(fileInputStream);
135    
136                            FileUtil.delete(file);
137    
138                            if (stagingRequestId > 0) {
139                                    StagingServiceHttp.cleanUpStagingRequest(
140                                            httpPrincipal, stagingRequestId);
141                            }
142                    }
143    
144                    return processMissingReferences(backgroundTask, missingReferences);
145            }
146    
147            protected File exportLayoutsAsFile(
148                            long sourceGroupId, boolean privateLayout,
149                            Map<Long, Boolean> layoutIdMap, Map<String, String[]> parameterMap,
150                            long remoteGroupId, Date startDate, Date endDate,
151                            HttpPrincipal httpPrincipal)
152                    throws Exception {
153    
154                    if ((layoutIdMap == null) || layoutIdMap.isEmpty()) {
155                            return LayoutLocalServiceUtil.exportLayoutsAsFile(
156                                    sourceGroupId, privateLayout, null, parameterMap, startDate,
157                                    endDate);
158                    }
159                    else {
160                            List<Layout> layouts = new ArrayList<Layout>();
161    
162                            for (Map.Entry<Long, Boolean> entry : layoutIdMap.entrySet()) {
163                                    long plid = GetterUtil.getLong(String.valueOf(entry.getKey()));
164                                    boolean includeChildren = entry.getValue();
165    
166                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
167    
168                                    if (!layouts.contains(layout)) {
169                                            layouts.add(layout);
170                                    }
171    
172                                    List<Layout> parentLayouts = getMissingRemoteParentLayouts(
173                                            httpPrincipal, layout, remoteGroupId);
174    
175                                    for (Layout parentLayout : parentLayouts) {
176                                            if (!layouts.contains(parentLayout)) {
177                                                    layouts.add(parentLayout);
178                                            }
179                                    }
180    
181                                    if (includeChildren) {
182                                            for (Layout childLayout : layout.getAllChildren()) {
183                                                    if (!layouts.contains(childLayout)) {
184                                                            layouts.add(childLayout);
185                                                    }
186                                            }
187                                    }
188                            }
189    
190                            long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layouts);
191    
192                            if (layoutIds.length <= 0) {
193                                    throw new RemoteExportException(
194                                            RemoteExportException.NO_LAYOUTS);
195                            }
196    
197                            return LayoutLocalServiceUtil.exportLayoutsAsFile(
198                                    sourceGroupId, privateLayout, layoutIds, parameterMap,
199                                    startDate, endDate);
200                    }
201            }
202    
203            /**
204             * @see com.liferay.portal.staging.StagingImpl#getMissingParentLayouts(
205             *      Layout, long)
206             */
207            protected List<Layout> getMissingRemoteParentLayouts(
208                            HttpPrincipal httpPrincipal, Layout layout, long remoteGroupId)
209                    throws Exception {
210    
211                    List<Layout> missingRemoteParentLayouts = new ArrayList<Layout>();
212    
213                    long parentLayoutId = layout.getParentLayoutId();
214    
215                    while (parentLayoutId > 0) {
216                            Layout parentLayout = LayoutLocalServiceUtil.getLayout(
217                                    layout.getGroupId(), layout.isPrivateLayout(), parentLayoutId);
218    
219                            try {
220                                    LayoutServiceHttp.getLayoutByUuidAndGroupId(
221                                            httpPrincipal, parentLayout.getUuid(), remoteGroupId,
222                                            parentLayout.getPrivateLayout());
223    
224                                    // If one parent is found all others are assumed to exist
225    
226                                    break;
227                            }
228                            catch (NoSuchLayoutException nsle) {
229                                    missingRemoteParentLayouts.add(parentLayout);
230    
231                                    parentLayoutId = parentLayout.getParentLayoutId();
232                            }
233                    }
234    
235                    return missingRemoteParentLayouts;
236            }
237    
238    }