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