001
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
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 Date lastPublishDate = endDate;
081
082 if (lastPublishDate == null) {
083 lastPublishDate = new Date();
084 }
085
086 file = exportLayoutsAsFile(
087 sourceGroupId, privateLayout, layoutIdMap, parameterMap,
088 remoteGroupId, startDate, endDate, httpPrincipal);
089
090 String checksum = FileUtil.getMD5Checksum(file);
091
092 fileInputStream = new FileInputStream(file);
093
094 stagingRequestId = StagingServiceHttp.createStagingRequest(
095 httpPrincipal, remoteGroupId, checksum);
096
097 byte[] bytes =
098 new byte[PropsValues.STAGING_REMOTE_TRANSFER_BUFFER_SIZE];
099
100 int i = 0;
101
102 while ((i = fileInputStream.read(bytes)) >= 0) {
103 if (i < PropsValues.STAGING_REMOTE_TRANSFER_BUFFER_SIZE) {
104 byte[] tempBytes = new byte[i];
105
106 System.arraycopy(bytes, 0, tempBytes, 0, i);
107
108 StagingServiceHttp.updateStagingRequest(
109 httpPrincipal, stagingRequestId, file.getName(),
110 tempBytes);
111 }
112 else {
113 StagingServiceHttp.updateStagingRequest(
114 httpPrincipal, stagingRequestId, file.getName(), bytes);
115 }
116
117 bytes =
118 new byte[PropsValues.STAGING_REMOTE_TRANSFER_BUFFER_SIZE];
119 }
120
121 backgroundTask = markBackgroundTask(backgroundTask, "exported");
122
123 missingReferences = StagingServiceHttp.validateStagingRequest(
124 httpPrincipal, stagingRequestId, privateLayout, parameterMap);
125
126 backgroundTask = markBackgroundTask(backgroundTask, "validated");
127
128 StagingServiceHttp.publishStagingRequest(
129 httpPrincipal, stagingRequestId, privateLayout, parameterMap);
130
131 boolean updateLastPublishDate = MapUtil.getBoolean(
132 parameterMap, PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE);
133
134 if (updateLastPublishDate) {
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
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
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 }