001
014
015 package com.liferay.portal.staging;
016
017 import com.liferay.portal.DuplicateLockException;
018 import com.liferay.portal.LARFileException;
019 import com.liferay.portal.LARFileSizeException;
020 import com.liferay.portal.LARTypeException;
021 import com.liferay.portal.LayoutPrototypeException;
022 import com.liferay.portal.LocaleException;
023 import com.liferay.portal.MissingReferenceException;
024 import com.liferay.portal.NoSuchGroupException;
025 import com.liferay.portal.NoSuchLayoutBranchException;
026 import com.liferay.portal.NoSuchLayoutException;
027 import com.liferay.portal.NoSuchLayoutRevisionException;
028 import com.liferay.portal.PortletIdException;
029 import com.liferay.portal.RemoteExportException;
030 import com.liferay.portal.RemoteOptionsException;
031 import com.liferay.portal.kernel.exception.PortalException;
032 import com.liferay.portal.kernel.exception.SystemException;
033 import com.liferay.portal.kernel.json.JSONArray;
034 import com.liferay.portal.kernel.json.JSONFactoryUtil;
035 import com.liferay.portal.kernel.json.JSONObject;
036 import com.liferay.portal.kernel.language.LanguageUtil;
037 import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
038 import com.liferay.portal.kernel.lar.MissingReference;
039 import com.liferay.portal.kernel.lar.MissingReferences;
040 import com.liferay.portal.kernel.lar.PortletDataContext;
041 import com.liferay.portal.kernel.lar.PortletDataException;
042 import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
043 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
044 import com.liferay.portal.kernel.lar.StagedModelType;
045 import com.liferay.portal.kernel.lar.UserIdStrategy;
046 import com.liferay.portal.kernel.log.Log;
047 import com.liferay.portal.kernel.log.LogFactoryUtil;
048 import com.liferay.portal.kernel.messaging.DestinationNames;
049 import com.liferay.portal.kernel.messaging.MessageBusUtil;
050 import com.liferay.portal.kernel.messaging.MessageStatus;
051 import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
052 import com.liferay.portal.kernel.security.pacl.DoPrivileged;
053 import com.liferay.portal.kernel.servlet.ServletResponseConstants;
054 import com.liferay.portal.kernel.staging.LayoutStagingUtil;
055 import com.liferay.portal.kernel.staging.Staging;
056 import com.liferay.portal.kernel.staging.StagingConstants;
057 import com.liferay.portal.kernel.util.Constants;
058 import com.liferay.portal.kernel.util.DateRange;
059 import com.liferay.portal.kernel.util.GetterUtil;
060 import com.liferay.portal.kernel.util.Http;
061 import com.liferay.portal.kernel.util.MapUtil;
062 import com.liferay.portal.kernel.util.ParamUtil;
063 import com.liferay.portal.kernel.util.PropsKeys;
064 import com.liferay.portal.kernel.util.StringBundler;
065 import com.liferay.portal.kernel.util.StringPool;
066 import com.liferay.portal.kernel.util.StringUtil;
067 import com.liferay.portal.kernel.util.Tuple;
068 import com.liferay.portal.kernel.util.UnicodeProperties;
069 import com.liferay.portal.kernel.util.Validator;
070 import com.liferay.portal.kernel.workflow.WorkflowConstants;
071 import com.liferay.portal.kernel.workflow.WorkflowTask;
072 import com.liferay.portal.kernel.workflow.WorkflowTaskManagerUtil;
073 import com.liferay.portal.kernel.xml.Element;
074 import com.liferay.portal.lar.backgroundtask.BackgroundTaskContextMapFactory;
075 import com.liferay.portal.lar.backgroundtask.LayoutRemoteStagingBackgroundTaskExecutor;
076 import com.liferay.portal.lar.backgroundtask.LayoutStagingBackgroundTaskExecutor;
077 import com.liferay.portal.lar.backgroundtask.PortletStagingBackgroundTaskExecutor;
078 import com.liferay.portal.messaging.LayoutsLocalPublisherRequest;
079 import com.liferay.portal.messaging.LayoutsRemotePublisherRequest;
080 import com.liferay.portal.model.Group;
081 import com.liferay.portal.model.GroupConstants;
082 import com.liferay.portal.model.Layout;
083 import com.liferay.portal.model.LayoutBranch;
084 import com.liferay.portal.model.LayoutConstants;
085 import com.liferay.portal.model.LayoutRevision;
086 import com.liferay.portal.model.LayoutSet;
087 import com.liferay.portal.model.LayoutSetBranch;
088 import com.liferay.portal.model.LayoutSetBranchConstants;
089 import com.liferay.portal.model.Lock;
090 import com.liferay.portal.model.Portlet;
091 import com.liferay.portal.model.StagedModel;
092 import com.liferay.portal.model.User;
093 import com.liferay.portal.model.WorkflowInstanceLink;
094 import com.liferay.portal.security.auth.HttpPrincipal;
095 import com.liferay.portal.security.auth.PrincipalException;
096 import com.liferay.portal.security.auth.RemoteAuthException;
097 import com.liferay.portal.security.permission.ActionKeys;
098 import com.liferay.portal.security.permission.PermissionChecker;
099 import com.liferay.portal.security.permission.PermissionThreadLocal;
100 import com.liferay.portal.security.permission.ResourceActionsUtil;
101 import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
102 import com.liferay.portal.service.GroupLocalServiceUtil;
103 import com.liferay.portal.service.LayoutBranchLocalServiceUtil;
104 import com.liferay.portal.service.LayoutLocalServiceUtil;
105 import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
106 import com.liferay.portal.service.LayoutServiceUtil;
107 import com.liferay.portal.service.LayoutSetBranchLocalServiceUtil;
108 import com.liferay.portal.service.LayoutSetLocalServiceUtil;
109 import com.liferay.portal.service.LockLocalServiceUtil;
110 import com.liferay.portal.service.ServiceContext;
111 import com.liferay.portal.service.ServiceContextThreadLocal;
112 import com.liferay.portal.service.StagingLocalServiceUtil;
113 import com.liferay.portal.service.WorkflowInstanceLinkLocalServiceUtil;
114 import com.liferay.portal.service.http.GroupServiceHttp;
115 import com.liferay.portal.service.permission.GroupPermissionUtil;
116 import com.liferay.portal.theme.ThemeDisplay;
117 import com.liferay.portal.util.PortalUtil;
118 import com.liferay.portal.util.PrefsPropsUtil;
119 import com.liferay.portal.util.PropsValues;
120 import com.liferay.portal.util.SessionClicks;
121 import com.liferay.portal.util.WebKeys;
122 import com.liferay.portlet.PortalPreferences;
123 import com.liferay.portlet.PortletPreferencesFactoryUtil;
124 import com.liferay.portlet.documentlibrary.DuplicateFileException;
125 import com.liferay.portlet.documentlibrary.FileExtensionException;
126 import com.liferay.portlet.documentlibrary.FileNameException;
127 import com.liferay.portlet.documentlibrary.FileSizeException;
128 import com.liferay.portlet.layoutsadmin.lar.StagedTheme;
129
130 import java.io.Serializable;
131
132 import java.util.ArrayList;
133 import java.util.Calendar;
134 import java.util.Date;
135 import java.util.HashMap;
136 import java.util.HashSet;
137 import java.util.Iterator;
138 import java.util.LinkedHashMap;
139 import java.util.List;
140 import java.util.Locale;
141 import java.util.Map;
142 import java.util.Set;
143
144 import javax.portlet.PortletPreferences;
145 import javax.portlet.PortletRequest;
146
147 import javax.servlet.http.HttpServletRequest;
148
149
155 @DoPrivileged
156 public class StagingImpl implements Staging {
157
158 @Override
159 public String buildRemoteURL(
160 String remoteAddress, int remotePort, String remotePathContext,
161 boolean secureConnection, long remoteGroupId, boolean privateLayout) {
162
163 StringBundler sb = new StringBundler((remoteGroupId > 0) ? 4 : 9);
164
165 if (secureConnection) {
166 sb.append(Http.HTTPS_WITH_SLASH);
167 }
168 else {
169 sb.append(Http.HTTP_WITH_SLASH);
170 }
171
172 sb.append(remoteAddress);
173
174 if (remotePort > 0) {
175 sb.append(StringPool.COLON);
176 sb.append(remotePort);
177 }
178
179 if (Validator.isNotNull(remotePathContext)) {
180 sb.append(remotePathContext);
181 }
182
183 if (remoteGroupId > 0) {
184 sb.append("/c/my_sites/view?");
185 sb.append("groupId=");
186 sb.append(remoteGroupId);
187 sb.append("&privateLayout=");
188 sb.append(privateLayout);
189 }
190
191 return sb.toString();
192 }
193
194 @Override
195 public String buildRemoteURL(UnicodeProperties typeSettingsProperties) {
196 String remoteAddress = typeSettingsProperties.getProperty(
197 "remoteAddress");
198 int remotePort = GetterUtil.getInteger(
199 typeSettingsProperties.getProperty("remotePort"));
200 String remotePathContext = typeSettingsProperties.getProperty(
201 "remotePathContext");
202 boolean secureConnection = GetterUtil.getBoolean(
203 typeSettingsProperties.getProperty("secureConnection"));
204
205 return buildRemoteURL(
206 remoteAddress, remotePort, remotePathContext, secureConnection,
207 GroupConstants.DEFAULT_LIVE_GROUP_ID, false);
208 }
209
210 @Override
211 public void checkDefaultLayoutSetBranches(
212 long userId, Group liveGroup, boolean branchingPublic,
213 boolean branchingPrivate, boolean remote,
214 ServiceContext serviceContext)
215 throws PortalException, SystemException {
216
217 long targetGroupId = 0;
218
219 if (remote) {
220 targetGroupId = liveGroup.getGroupId();
221 }
222 else {
223 Group stagingGroup = liveGroup.getStagingGroup();
224
225 if (stagingGroup == null) {
226 return;
227 }
228
229 targetGroupId = stagingGroup.getGroupId();
230 }
231
232 if (branchingPublic) {
233 LayoutSetBranch layoutSetBranch =
234 LayoutSetBranchLocalServiceUtil.fetchLayoutSetBranch(
235 targetGroupId, false,
236 LayoutSetBranchConstants.MASTER_BRANCH_NAME);
237
238 if (layoutSetBranch == null) {
239 addDefaultLayoutSetBranch(
240 userId, targetGroupId, liveGroup.getDescriptiveName(),
241 false, serviceContext);
242 }
243 }
244 else {
245 LayoutSetBranchLocalServiceUtil.deleteLayoutSetBranches(
246 targetGroupId, false, true);
247 }
248
249 if (branchingPrivate) {
250 LayoutSetBranch layoutSetBranch =
251 LayoutSetBranchLocalServiceUtil.fetchLayoutSetBranch(
252 targetGroupId, true,
253 LayoutSetBranchConstants.MASTER_BRANCH_NAME);
254
255 if (layoutSetBranch == null) {
256 addDefaultLayoutSetBranch(
257 userId, targetGroupId, liveGroup.getDescriptiveName(), true,
258 serviceContext);
259 }
260 }
261 else {
262 LayoutSetBranchLocalServiceUtil.deleteLayoutSetBranches(
263 targetGroupId, true, true);
264 }
265 }
266
267 @Override
268 public void copyFromLive(PortletRequest portletRequest) throws Exception {
269 long stagingGroupId = ParamUtil.getLong(
270 portletRequest, "stagingGroupId");
271
272 Group stagingGroup = GroupLocalServiceUtil.getGroup(stagingGroupId);
273
274 long liveGroupId = stagingGroup.getLiveGroupId();
275
276 Map<String, String[]> parameterMap = getStagingParameters(
277 portletRequest);
278
279 publishLayouts(
280 portletRequest, liveGroupId, stagingGroupId, parameterMap, false);
281 }
282
283 @Override
284 public void copyFromLive(PortletRequest portletRequest, Portlet portlet)
285 throws Exception {
286
287 long plid = ParamUtil.getLong(portletRequest, "plid");
288
289 Layout targetLayout = LayoutLocalServiceUtil.getLayout(plid);
290
291 Group stagingGroup = targetLayout.getGroup();
292 Group liveGroup = stagingGroup.getLiveGroup();
293
294 Layout sourceLayout = LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
295 targetLayout.getUuid(), liveGroup.getGroupId(),
296 targetLayout.isPrivateLayout());
297
298 copyPortlet(
299 portletRequest, liveGroup.getGroupId(), stagingGroup.getGroupId(),
300 sourceLayout.getPlid(), targetLayout.getPlid(),
301 portlet.getPortletId());
302 }
303
304 @Override
305 public void copyPortlet(
306 PortletRequest portletRequest, long sourceGroupId,
307 long targetGroupId, long sourcePlid, long targetPlid,
308 String portletId)
309 throws Exception {
310
311 long userId = PortalUtil.getUserId(portletRequest);
312
313 Map<String, String[]> parameterMap = getStagingParameters(
314 portletRequest);
315
316 DateRange dateRange = ExportImportHelperUtil.getDateRange(
317 portletRequest, sourceGroupId, false, sourcePlid, portletId,
318 "fromLastPublishDate");
319
320 Map<String, Serializable> taskContextMap =
321 BackgroundTaskContextMapFactory.buildTaskContextMap(
322 userId, sourceGroupId, false, null, parameterMap,
323 Constants.PUBLISH, dateRange.getStartDate(),
324 dateRange.getEndDate(), StringPool.BLANK);
325
326 taskContextMap.put("sourceGroupId", sourceGroupId);
327 taskContextMap.put("sourcePlid", sourcePlid);
328 taskContextMap.put("portletId", portletId);
329 taskContextMap.put("targetGroupId", targetGroupId);
330 taskContextMap.put("targetPlid", targetPlid);
331
332 BackgroundTaskLocalServiceUtil.addBackgroundTask(
333 userId, sourceGroupId, portletId, null,
334 PortletStagingBackgroundTaskExecutor.class, taskContextMap,
335 new ServiceContext());
336 }
337
338 @Override
339 public void copyRemoteLayouts(
340 long sourceGroupId, boolean privateLayout,
341 Map<Long, Boolean> layoutIdMap, Map<String, String[]> parameterMap,
342 String remoteAddress, int remotePort, String remotePathContext,
343 boolean secureConnection, long remoteGroupId,
344 boolean remotePrivateLayout, Date startDate, Date endDate)
345 throws Exception {
346
347 PermissionChecker permissionChecker =
348 PermissionThreadLocal.getPermissionChecker();
349
350 User user = permissionChecker.getUser();
351
352 StringBundler sb = new StringBundler(4);
353
354 if (secureConnection) {
355 sb.append(Http.HTTPS_WITH_SLASH);
356 }
357 else {
358 sb.append(Http.HTTP_WITH_SLASH);
359 }
360
361 sb.append(remoteAddress);
362 sb.append(StringPool.COLON);
363 sb.append(remotePort);
364 sb.append(remotePathContext);
365
366 String url = sb.toString();
367
368 HttpPrincipal httpPrincipal = new HttpPrincipal(
369 url, user.getEmailAddress(), user.getPassword(),
370 user.getPasswordEncrypted());
371
372
373
374
375 try {
376 GroupServiceHttp.checkRemoteStagingGroup(
377 httpPrincipal, remoteGroupId);
378 }
379 catch (NoSuchGroupException nsge) {
380 RemoteExportException ree = new RemoteExportException(
381 RemoteExportException.NO_GROUP);
382
383 ree.setGroupId(remoteGroupId);
384
385 throw ree;
386 }
387 catch (RemoteAuthException rae) {
388 rae.setURL(url);
389
390 throw rae;
391 }
392 catch (SystemException se) {
393 RemoteExportException ree = new RemoteExportException(
394 RemoteExportException.BAD_CONNECTION);
395
396 ree.setURL(url);
397
398 throw ree;
399 }
400
401 Map<String, Serializable> taskContextMap =
402 BackgroundTaskContextMapFactory.buildTaskContextMap(
403 user.getUserId(), sourceGroupId, privateLayout, null,
404 parameterMap, Constants.PUBLISH, startDate, endDate, null);
405
406 taskContextMap.put("httpPrincipal", httpPrincipal);
407
408 if (layoutIdMap != null) {
409 HashMap<Long, Boolean> serializableLayoutIdMap =
410 new HashMap<Long, Boolean>(layoutIdMap);
411
412 taskContextMap.put("layoutIdMap", serializableLayoutIdMap);
413 }
414
415 taskContextMap.put("remoteGroupId", remoteGroupId);
416
417 BackgroundTaskLocalServiceUtil.addBackgroundTask(
418 user.getUserId(), sourceGroupId, StringPool.BLANK, null,
419 LayoutRemoteStagingBackgroundTaskExecutor.class, taskContextMap,
420 new ServiceContext());
421 }
422
423 @Override
424 public void deleteLastImportSettings(Group liveGroup, boolean privateLayout)
425 throws PortalException, SystemException {
426
427 List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
428 liveGroup.getGroupId(), privateLayout);
429
430 for (Layout layout : layouts) {
431 UnicodeProperties typeSettingsProperties =
432 layout.getTypeSettingsProperties();
433
434 Set<String> keys = new HashSet<String>();
435
436 for (String key : typeSettingsProperties.keySet()) {
437 if (key.startsWith("last-import-")) {
438 keys.add(key);
439 }
440 }
441
442 if (keys.isEmpty()) {
443 continue;
444 }
445
446 for (String key : keys) {
447 typeSettingsProperties.remove(key);
448 }
449
450 LayoutLocalServiceUtil.updateLayout(
451 layout.getGroupId(), layout.getPrivateLayout(),
452 layout.getLayoutId(), typeSettingsProperties.toString());
453 }
454 }
455
456 @Override
457 public void deleteRecentLayoutRevisionId(
458 HttpServletRequest request, long layoutSetBranchId, long plid)
459 throws SystemException {
460
461 PortalPreferences portalPreferences =
462 PortletPreferencesFactoryUtil.getPortalPreferences(request);
463
464 deleteRecentLayoutRevisionId(
465 portalPreferences, layoutSetBranchId, plid);
466 }
467
468 @Override
469 public void deleteRecentLayoutRevisionId(
470 User user, long layoutSetBranchId, long plid)
471 throws SystemException {
472
473 PortalPreferences portalPreferences = getPortalPreferences(user);
474
475 deleteRecentLayoutRevisionId(
476 portalPreferences, layoutSetBranchId, plid);
477 }
478
479
484 @Override
485 public void disableStaging(
486 Group scopeGroup, Group liveGroup, ServiceContext serviceContext)
487 throws Exception {
488
489 disableStaging((PortletRequest)null, liveGroup, serviceContext);
490 }
491
492
497 @Override
498 public void disableStaging(Group liveGroup, ServiceContext serviceContext)
499 throws Exception {
500
501 disableStaging((PortletRequest)null, liveGroup, serviceContext);
502 }
503
504
509 @Override
510 public void disableStaging(
511 PortletRequest portletRequest, Group scopeGroup, Group liveGroup,
512 ServiceContext serviceContext)
513 throws Exception {
514
515 disableStaging(portletRequest, liveGroup, serviceContext);
516 }
517
518
523 @Override
524 public void disableStaging(
525 PortletRequest portletRequest, Group liveGroup,
526 ServiceContext serviceContext)
527 throws Exception {
528
529 StagingLocalServiceUtil.disableStaging(
530 portletRequest, liveGroup, serviceContext);
531 }
532
533
538 @Override
539 public void enableLocalStaging(
540 long userId, Group scopeGroup, Group liveGroup,
541 boolean branchingPublic, boolean branchingPrivate,
542 ServiceContext serviceContext)
543 throws Exception {
544
545 StagingLocalServiceUtil.enableLocalStaging(
546 userId, liveGroup, branchingPublic, branchingPrivate,
547 serviceContext);
548 }
549
550
556 @Override
557 public void enableRemoteStaging(
558 long userId, Group scopeGroup, Group liveGroup,
559 boolean branchingPublic, boolean branchingPrivate,
560 String remoteAddress, int remotePort, String remotePathContext,
561 boolean secureConnection, long remoteGroupId,
562 ServiceContext serviceContext)
563 throws Exception {
564
565 StagingLocalServiceUtil.enableRemoteStaging(
566 userId, liveGroup, branchingPublic, branchingPrivate, remoteAddress,
567 remotePort, remotePathContext, secureConnection, remoteGroupId,
568 serviceContext);
569 }
570
571 @Override
572 public JSONArray getErrorMessagesJSONArray(
573 Locale locale, Map<String, MissingReference> missingReferences,
574 Map<String, Serializable> contextMap) {
575
576 JSONArray errorMessagesJSONArray = JSONFactoryUtil.createJSONArray();
577
578 for (String missingReferenceDisplayName : missingReferences.keySet()) {
579 MissingReference missingReference = missingReferences.get(
580 missingReferenceDisplayName);
581
582 JSONObject errorMessageJSONObject =
583 JSONFactoryUtil.createJSONObject();
584
585 String className = missingReference.getClassName();
586 Map<String, String> referrers = missingReference.getReferrers();
587
588 if (className.equals(StagedTheme.class.getName())) {
589 errorMessageJSONObject.put(
590 "info",
591 LanguageUtil.format(
592 locale,
593 "the-referenced-theme-x-is-not-deployed-in-the-" +
594 "current-environment",
595 missingReference.getClassPK()));
596 }
597 else if (referrers.size() == 1) {
598 Set<Map.Entry<String, String>> referrerDisplayNames =
599 referrers.entrySet();
600
601 Iterator<Map.Entry<String, String>> iterator =
602 referrerDisplayNames.iterator();
603
604 Map.Entry<String, String> entry = iterator.next();
605
606 String referrerDisplayName = entry.getKey();
607 String referrerClassName = entry.getValue();
608
609 if (referrerClassName.equals(Portlet.class.getName())) {
610 referrerDisplayName = PortalUtil.getPortletTitle(
611 referrerDisplayName, locale);
612 }
613
614 errorMessageJSONObject.put(
615 "info",
616 LanguageUtil.format(
617 locale, "referenced-by-a-x-x",
618 new String[] {
619 ResourceActionsUtil.getModelResource(
620 locale, referrerClassName),
621 referrerDisplayName
622 }
623 ));
624 }
625 else {
626 errorMessageJSONObject.put(
627 "info",
628 LanguageUtil.format(
629 locale, "referenced-by-x-elements", referrers.size()));
630 }
631
632 errorMessageJSONObject.put("name", missingReferenceDisplayName);
633 errorMessageJSONObject.put(
634 "type",
635 ResourceActionsUtil.getModelResource(
636 locale, missingReference.getClassName()));
637
638 errorMessagesJSONArray.put(errorMessageJSONObject);
639 }
640
641 return errorMessagesJSONArray;
642 }
643
644 @Override
645 public JSONObject getExceptionMessagesJSONObject(
646 Locale locale, Exception e, Map<String, Serializable> contextMap) {
647
648 JSONObject exceptionMessagesJSONObject =
649 JSONFactoryUtil.createJSONObject();
650
651 String errorMessage = StringPool.BLANK;
652 JSONArray errorMessagesJSONArray = null;
653 int errorType = 0;
654 JSONArray warningMessagesJSONArray = null;
655
656 if (e instanceof DuplicateFileException) {
657 errorMessage = LanguageUtil.get(
658 locale, "please-enter-a-unique-document-name");
659 errorType = ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION;
660 }
661 else if (e instanceof FileExtensionException) {
662 errorMessage = LanguageUtil.format(
663 locale,
664 "document-names-must-end-with-one-of-the-following-extensions",
665 ".lar");
666 errorType = ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION;
667 }
668 else if (e instanceof FileNameException) {
669 errorMessage = LanguageUtil.get(
670 locale, "please-enter-a-file-with-a-valid-file-name");
671 errorType = ServletResponseConstants.SC_FILE_NAME_EXCEPTION;
672 }
673 else if (e instanceof FileSizeException ||
674 e instanceof LARFileSizeException) {
675
676 long fileMaxSize = PropsValues.DL_FILE_MAX_SIZE;
677
678 try {
679 fileMaxSize = PrefsPropsUtil.getLong(
680 PropsKeys.DL_FILE_MAX_SIZE);
681
682 if (fileMaxSize == 0) {
683 fileMaxSize = PrefsPropsUtil.getLong(
684 PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE);
685 }
686 }
687 catch (Exception ex) {
688 }
689
690 errorMessage = LanguageUtil.format(
691 locale,
692 "please-enter-a-file-with-a-valid-file-size-no-larger-than-x",
693 fileMaxSize/1024);
694 errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION;
695 }
696 else if (e instanceof LARTypeException) {
697 LARTypeException lte = (LARTypeException)e;
698
699 errorMessage = LanguageUtil.format(
700 locale,
701 "please-import-a-lar-file-of-the-correct-type-x-is-not-valid",
702 lte.getMessage());
703 errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
704 }
705 else if (e instanceof LARFileException) {
706 errorMessage = LanguageUtil.get(
707 locale, "please-specify-a-lar-file-to-import");
708 errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
709 }
710 else if (e instanceof LayoutPrototypeException) {
711 LayoutPrototypeException lpe = (LayoutPrototypeException)e;
712
713 StringBundler sb = new StringBundler(4);
714
715 sb.append("the-lar-file-could-not-be-imported-because-it-");
716 sb.append("requires-page-templates-or-site-templates-that-could-");
717 sb.append("not-be-found.-please-import-the-following-templates-");
718 sb.append("manually");
719
720 errorMessage = LanguageUtil.get(locale, sb.toString());
721
722 errorMessagesJSONArray = JSONFactoryUtil.createJSONArray();
723
724 List<Tuple> missingLayoutPrototypes =
725 lpe.getMissingLayoutPrototypes();
726
727 for (Tuple missingLayoutPrototype : missingLayoutPrototypes) {
728 JSONObject errorMessageJSONObject =
729 JSONFactoryUtil.createJSONObject();
730
731 String layoutPrototypeUuid =
732 (String)missingLayoutPrototype.getObject(1);
733
734 errorMessageJSONObject.put("info", layoutPrototypeUuid);
735
736 String layoutPrototypeName =
737 (String)missingLayoutPrototype.getObject(2);
738
739 errorMessageJSONObject.put("name", layoutPrototypeName);
740
741 String layoutPrototypeClassName =
742 (String)missingLayoutPrototype.getObject(0);
743
744 errorMessageJSONObject.put(
745 "type",
746 ResourceActionsUtil.getModelResource(
747 locale, layoutPrototypeClassName));
748
749 errorMessagesJSONArray.put(errorMessageJSONObject);
750 }
751
752 errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
753 }
754 else if (e instanceof LocaleException) {
755 LocaleException le = (LocaleException)e;
756
757 errorMessage = LanguageUtil.format(
758 locale,
759 "the-available-languages-in-the-lar-file-x-do-not-match-the-" +
760 "site's-available-languages-x",
761 new String[] {
762 StringUtil.merge(
763 le.getSourceAvailableLocales(),
764 StringPool.COMMA_AND_SPACE),
765 StringUtil.merge(
766 le.getTargetAvailableLocales(),
767 StringPool.COMMA_AND_SPACE)
768 });
769 errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
770 }
771 else if (e instanceof MissingReferenceException) {
772 MissingReferenceException mre = (MissingReferenceException)e;
773
774 String cmd = null;
775
776 if (contextMap != null) {
777 cmd = (String)contextMap.get(Constants.CMD);
778 }
779
780 if (Validator.equals(cmd, Constants.PUBLISH)) {
781 errorMessage = LanguageUtil.get(
782 locale,
783 "there-are-missing-references-that-could-not-be-found-in-" +
784 "the-live-environment.-please-publish-again-to-live-" +
785 "ensuring-the-following-elements-are-published");
786 }
787 else {
788 errorMessage = LanguageUtil.get(
789 locale,
790 "there-are-missing-references-that-could-not-be-found-in-" +
791 "the-current-site.-please-import-another-lar-file-" +
792 "containing-the-following-elements");
793 }
794
795 MissingReferences missingReferences = mre.getMissingReferences();
796
797 errorMessagesJSONArray = getErrorMessagesJSONArray(
798 locale, missingReferences.getDependencyMissingReferences(),
799 contextMap);
800 errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
801 warningMessagesJSONArray = getWarningMessagesJSONArray(
802 locale, missingReferences.getWeakMissingReferences(),
803 contextMap);
804 }
805 else if (e instanceof PortletDataException) {
806 PortletDataException pde = (PortletDataException)e;
807
808 StagedModel stagedModel = pde.getStagedModel();
809
810 String referrerClassName = StringPool.BLANK;
811 String referrerDisplayName = StringPool.BLANK;
812
813 if (stagedModel != null) {
814 StagedModelType stagedModelType =
815 stagedModel.getStagedModelType();
816
817 referrerClassName = stagedModelType.getClassName();
818 referrerDisplayName = StagedModelDataHandlerUtil.getDisplayName(
819 stagedModel);
820 }
821
822 if (pde.getType() == PortletDataException.INVALID_GROUP) {
823 errorMessage = LanguageUtil.format(
824 locale,
825 "the-x-x-could-not-be-exported-because-it-is-not-in-the-" +
826 "currently-exported-group",
827 new String[] {
828 ResourceActionsUtil.getModelResource(
829 locale, referrerClassName),
830 referrerDisplayName
831 }
832 );
833 }
834 else if (pde.getType() == PortletDataException.MISSING_DEPENDENCY) {
835 errorMessage = LanguageUtil.format(
836 locale,
837 "the-x-x-has-missing-references-that-could-not-be-found-" +
838 "during-the-export",
839 new String[] {
840 ResourceActionsUtil.getModelResource(
841 locale, referrerClassName),
842 referrerDisplayName
843 }
844 );
845 }
846 else if (pde.getType() == PortletDataException.STATUS_IN_TRASH) {
847 errorMessage = LanguageUtil.format(
848 locale,
849 "the-x-x-could-not-be-exported-because-it-is-in-the-" +
850 "recycle-bin",
851 new String[] {
852 ResourceActionsUtil.getModelResource(
853 locale, referrerClassName),
854 referrerDisplayName
855 }
856 );
857 }
858 else if (pde.getType() == PortletDataException.STATUS_UNAVAILABLE) {
859 errorMessage = LanguageUtil.format(
860 locale,
861 "the-x-x-could-not-be-exported-because-its-workflow-" +
862 "status-is-not-exportable",
863 new String[] {
864 ResourceActionsUtil.getModelResource(
865 locale, referrerClassName),
866 referrerDisplayName
867 }
868 );
869 }
870 else {
871 errorMessage = e.getLocalizedMessage();
872 }
873
874 errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
875 }
876 else if (e instanceof PortletIdException) {
877 errorMessage = LanguageUtil.get(
878 locale, "please-import-a-lar-file-for-the-current-portlet");
879 errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
880 }
881 else if (e instanceof RemoteExportException) {
882 RemoteExportException ree = (RemoteExportException)e;
883
884 if (ree.getType() == RemoteExportException.NO_LAYOUTS) {
885 errorMessage = LanguageUtil.get(
886 locale, "no-pages-are-selected-for-export");
887 }
888
889 errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
890 }
891 else {
892 errorMessage = e.getLocalizedMessage();
893 errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
894 }
895
896 exceptionMessagesJSONObject.put("message", errorMessage);
897
898 if ((errorMessagesJSONArray != null) &&
899 (errorMessagesJSONArray.length() > 0)) {
900
901 exceptionMessagesJSONObject.put(
902 "messageListItems", errorMessagesJSONArray);
903 }
904
905 exceptionMessagesJSONObject.put("status", errorType);
906
907 if ((warningMessagesJSONArray != null) &&
908 (warningMessagesJSONArray.length() > 0)) {
909
910 exceptionMessagesJSONObject.put(
911 "warningMessages", warningMessagesJSONArray);
912 }
913
914 return exceptionMessagesJSONObject;
915 }
916
917 @Override
918 public Group getLiveGroup(long groupId)
919 throws PortalException, SystemException {
920
921 if (groupId == 0) {
922 return null;
923 }
924
925 Group group = GroupLocalServiceUtil.getGroup(groupId);
926
927 if (group.isLayout()) {
928 group = group.getParentGroup();
929 }
930
931 if (group.isStagingGroup()) {
932 return group.getLiveGroup();
933 }
934 else {
935 return group;
936 }
937 }
938
939 @Override
940 public long getLiveGroupId(long groupId)
941 throws PortalException, SystemException {
942
943 if (groupId == 0) {
944 return groupId;
945 }
946
947 Group group = getLiveGroup(groupId);
948
949 return group.getGroupId();
950 }
951
952
956 @Override
957 public List<Layout> getMissingParentLayouts(Layout layout, long liveGroupId)
958 throws PortalException, SystemException {
959
960 List<Layout> missingParentLayouts = new ArrayList<Layout>();
961
962 long parentLayoutId = layout.getParentLayoutId();
963
964 Layout parentLayout = null;
965
966 while (parentLayoutId > 0) {
967 parentLayout = LayoutLocalServiceUtil.getLayout(
968 layout.getGroupId(), layout.isPrivateLayout(), parentLayoutId);
969
970 try {
971 LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
972 parentLayout.getUuid(), liveGroupId,
973 parentLayout.isPrivateLayout());
974
975
976
977 break;
978 }
979 catch (NoSuchLayoutException nsle) {
980 missingParentLayouts.add(parentLayout);
981
982 parentLayoutId = parentLayout.getParentLayoutId();
983 }
984 }
985
986 return missingParentLayouts;
987 }
988
989 @Override
990 public long getRecentLayoutRevisionId(
991 HttpServletRequest request, long layoutSetBranchId, long plid)
992 throws PortalException, SystemException {
993
994 PortalPreferences portalPreferences =
995 PortletPreferencesFactoryUtil.getPortalPreferences(request);
996
997 return getRecentLayoutRevisionId(
998 portalPreferences, layoutSetBranchId, plid);
999 }
1000
1001 @Override
1002 public long getRecentLayoutRevisionId(
1003 User user, long layoutSetBranchId, long plid)
1004 throws PortalException, SystemException {
1005
1006 PortalPreferences portalPreferences = getPortalPreferences(user);
1007
1008 return getRecentLayoutRevisionId(
1009 portalPreferences, layoutSetBranchId, plid);
1010 }
1011
1012 @Override
1013 public long getRecentLayoutSetBranchId(
1014 HttpServletRequest request, long layoutSetId) {
1015
1016 return GetterUtil.getLong(
1017 SessionClicks.get(
1018 request, Staging.class.getName(),
1019 getRecentLayoutSetBranchIdKey(layoutSetId)));
1020 }
1021
1022 @Override
1023 public long getRecentLayoutSetBranchId(User user, long layoutSetId)
1024 throws SystemException {
1025
1026 PortalPreferences portalPreferences = getPortalPreferences(user);
1027
1028 return GetterUtil.getLong(
1029 portalPreferences.getValue(
1030 Staging.class.getName(),
1031 getRecentLayoutSetBranchIdKey(layoutSetId)));
1032 }
1033
1034 @Override
1035 public String getSchedulerGroupName(String destinationName, long groupId) {
1036 return destinationName.concat(StringPool.SLASH).concat(
1037 String.valueOf(groupId));
1038 }
1039
1040 @Override
1041 public String getStagedPortletId(String portletId) {
1042 String key = portletId;
1043
1044 if (key.startsWith(StagingConstants.STAGED_PORTLET)) {
1045 return key;
1046 }
1047
1048 return StagingConstants.STAGED_PORTLET.concat(portletId);
1049 }
1050
1051 @Override
1052 public Map<String, String[]> getStagingParameters() {
1053 Map<String, String[]> parameterMap =
1054 new LinkedHashMap<String, String[]>();
1055
1056 parameterMap.put(
1057 PortletDataHandlerKeys.CATEGORIES,
1058 new String[] {Boolean.TRUE.toString()});
1059 parameterMap.put(
1060 PortletDataHandlerKeys.DATA_STRATEGY,
1061 new String[] {
1062 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE});
1063 parameterMap.put(
1064 PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
1065 new String[] {Boolean.TRUE.toString()});
1066 parameterMap.put(
1067 PortletDataHandlerKeys.DELETE_PORTLET_DATA,
1068 new String[] {Boolean.FALSE.toString()});
1069 parameterMap.put(
1070 PortletDataHandlerKeys.IGNORE_LAST_PUBLISH_DATE,
1071 new String[] {Boolean.TRUE.toString()});
1072 parameterMap.put(
1073 PortletDataHandlerKeys.LAYOUT_SET_PROTOTYPE_LINK_ENABLED,
1074 new String[] {Boolean.FALSE.toString()});
1075 parameterMap.put(
1076 PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
1077 new String[] {Boolean.FALSE.toString()});
1078 parameterMap.put(
1079 PortletDataHandlerKeys.LOGO,
1080 new String[] {Boolean.FALSE.toString()});
1081 parameterMap.put(
1082 PortletDataHandlerKeys.PERMISSIONS,
1083 new String[] {Boolean.TRUE.toString()});
1084 parameterMap.put(
1085 PortletDataHandlerKeys.PORTLET_CONFIGURATION,
1086 new String[] {Boolean.TRUE.toString()});
1087 parameterMap.put(
1088 PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
1089 new String[] {Boolean.TRUE.toString()});
1090 parameterMap.put(
1091 PortletDataHandlerKeys.PORTLET_DATA,
1092 new String[] {Boolean.TRUE.toString()});
1093 parameterMap.put(
1094 PortletDataHandlerKeys.PORTLET_DATA_ALL,
1095 new String[] {Boolean.TRUE.toString()});
1096 parameterMap.put(
1097 PortletDataHandlerKeys.PORTLET_SETUP_ALL,
1098 new String[] {Boolean.TRUE.toString()});
1099 parameterMap.put(
1100 PortletDataHandlerKeys.THEME_REFERENCE,
1101 new String[] {Boolean.TRUE.toString()});
1102 parameterMap.put(
1103 PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
1104 new String[] {Boolean.TRUE.toString()});
1105 parameterMap.put(
1106 PortletDataHandlerKeys.USER_ID_STRATEGY,
1107 new String[] {UserIdStrategy.CURRENT_USER_ID});
1108
1109 return parameterMap;
1110 }
1111
1112 @Override
1113 public Map<String, String[]> getStagingParameters(
1114 PortletRequest portletRequest) {
1115
1116 Map<String, String[]> parameterMap =
1117 new LinkedHashMap<String, String[]>(
1118 portletRequest.getParameterMap());
1119
1120 if (!parameterMap.containsKey(PortletDataHandlerKeys.DATA_STRATEGY)) {
1121 parameterMap.put(
1122 PortletDataHandlerKeys.DATA_STRATEGY,
1123 new String[] {
1124 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE});
1125 }
1126
1127
1134
1135 if (!parameterMap.containsKey(
1136 PortletDataHandlerKeys.DELETE_PORTLET_DATA)) {
1137
1138 parameterMap.put(
1139 PortletDataHandlerKeys.DELETE_PORTLET_DATA,
1140 new String[] {Boolean.FALSE.toString()});
1141 }
1142
1143 if (!parameterMap.containsKey(
1144 PortletDataHandlerKeys.LAYOUT_SET_PROTOTYPE_LINK_ENABLED)) {
1145
1146 parameterMap.put(
1147 PortletDataHandlerKeys.LAYOUT_SET_PROTOTYPE_LINK_ENABLED,
1148 new String[] {Boolean.FALSE.toString()});
1149 }
1150
1151 if (!parameterMap.containsKey(
1152 PortletDataHandlerKeys.LAYOUT_SET_SETTINGS)) {
1153
1154 parameterMap.put(
1155 PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
1156 new String[] {Boolean.FALSE.toString()});
1157 }
1158
1159 if (!parameterMap.containsKey(PortletDataHandlerKeys.LOGO)) {
1160 parameterMap.put(
1161 PortletDataHandlerKeys.LOGO,
1162 new String[] {Boolean.FALSE.toString()});
1163 }
1164
1165 if (!parameterMap.containsKey(
1166 PortletDataHandlerKeys.PORTLET_CONFIGURATION)) {
1167
1168 parameterMap.put(
1169 PortletDataHandlerKeys.PORTLET_CONFIGURATION,
1170 new String[] {Boolean.TRUE.toString()});
1171 }
1172
1173 if (!parameterMap.containsKey(PortletDataHandlerKeys.PORTLET_DATA)) {
1174 parameterMap.put(
1175 PortletDataHandlerKeys.PORTLET_DATA,
1176 new String[] {Boolean.FALSE.toString()});
1177 }
1178
1179 if (!parameterMap.containsKey(
1180 PortletDataHandlerKeys.PORTLET_DATA_ALL)) {
1181
1182 parameterMap.put(
1183 PortletDataHandlerKeys.PORTLET_DATA_ALL,
1184 new String[] {Boolean.FALSE.toString()});
1185 }
1186
1187 if (!parameterMap.containsKey(PortletDataHandlerKeys.THEME_REFERENCE)) {
1188 parameterMap.put(
1189 PortletDataHandlerKeys.THEME_REFERENCE,
1190 new String[] {Boolean.FALSE.toString()});
1191 }
1192
1193 if (!parameterMap.containsKey(
1194 PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE)) {
1195
1196 parameterMap.put(
1197 PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
1198 new String[] {Boolean.TRUE.toString()});
1199 }
1200
1201 if (!parameterMap.containsKey(
1202 PortletDataHandlerKeys.USER_ID_STRATEGY)) {
1203
1204 parameterMap.put(
1205 PortletDataHandlerKeys.USER_ID_STRATEGY,
1206 new String[] {UserIdStrategy.CURRENT_USER_ID});
1207 }
1208
1209 return parameterMap;
1210 }
1211
1212 @Override
1213 public JSONArray getWarningMessagesJSONArray(
1214 Locale locale, Map<String, MissingReference> missingReferences,
1215 Map<String, Serializable> contextMap) {
1216
1217 JSONArray warningMessagesJSONArray = JSONFactoryUtil.createJSONArray();
1218
1219 for (String missingReferenceReferrerClassName :
1220 missingReferences.keySet()) {
1221
1222 MissingReference missingReference = missingReferences.get(
1223 missingReferenceReferrerClassName);
1224
1225 Map<String, String> referrers = missingReference.getReferrers();
1226
1227 JSONObject errorMessageJSONObject =
1228 JSONFactoryUtil.createJSONObject();
1229
1230 if (Validator.isNotNull(missingReference.getClassName())) {
1231 errorMessageJSONObject.put(
1232 "info",
1233 LanguageUtil.format(
1234 locale,
1235 "the-original-x-does-not-exist-in-the-current-" +
1236 "environment",
1237 ResourceActionsUtil.getModelResource(
1238 locale, missingReference.getClassName())));
1239 }
1240
1241 errorMessageJSONObject.put("size", referrers.size());
1242 errorMessageJSONObject.put(
1243 "type",
1244 ResourceActionsUtil.getModelResource(
1245 locale, missingReferenceReferrerClassName));
1246
1247 warningMessagesJSONArray.put(errorMessageJSONObject);
1248 }
1249
1250 return warningMessagesJSONArray;
1251 }
1252
1253 @Override
1254 public WorkflowTask getWorkflowTask(
1255 long userId, LayoutRevision layoutRevision)
1256 throws PortalException, SystemException {
1257
1258 WorkflowInstanceLink workflowInstanceLink =
1259 WorkflowInstanceLinkLocalServiceUtil.fetchWorkflowInstanceLink(
1260 layoutRevision.getCompanyId(), layoutRevision.getGroupId(),
1261 LayoutRevision.class.getName(),
1262 layoutRevision.getLayoutRevisionId());
1263
1264 if (workflowInstanceLink == null) {
1265 return null;
1266 }
1267
1268 List<WorkflowTask> workflowTasks =
1269 WorkflowTaskManagerUtil.getWorkflowTasksByWorkflowInstance(
1270 layoutRevision.getCompanyId(), userId,
1271 workflowInstanceLink.getWorkflowInstanceId(), false, 0, 1,
1272 null);
1273
1274 if (!workflowTasks.isEmpty()) {
1275 return workflowTasks.get(0);
1276 }
1277
1278 return null;
1279 }
1280
1281 @Override
1282 public boolean hasWorkflowTask(long userId, LayoutRevision layoutRevision)
1283 throws PortalException, SystemException {
1284
1285 WorkflowTask workflowTask = getWorkflowTask(userId, layoutRevision);
1286
1287 if (workflowTask != null) {
1288 return true;
1289 }
1290
1291 return false;
1292 }
1293
1294 @Override
1295 public boolean isIncomplete(Layout layout, long layoutSetBranchId) {
1296 LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
1297 layout);
1298
1299 if (layoutRevision == null) {
1300 try {
1301 layoutRevision =
1302 LayoutRevisionLocalServiceUtil.getLayoutRevision(
1303 layoutSetBranchId, layout.getPlid(), true);
1304
1305 return false;
1306 }
1307 catch (Exception e) {
1308 }
1309 }
1310
1311 try {
1312 layoutRevision = LayoutRevisionLocalServiceUtil.getLayoutRevision(
1313 layoutSetBranchId, layout.getPlid(), false);
1314 }
1315 catch (Exception e) {
1316 }
1317
1318 if ((layoutRevision == null) ||
1319 (layoutRevision.getStatus() ==
1320 WorkflowConstants.STATUS_INCOMPLETE)) {
1321
1322 return true;
1323 }
1324
1325 return false;
1326 }
1327
1328 @Override
1329 public void lockGroup(long userId, long groupId) throws Exception {
1330 if (!PropsValues.STAGING_LOCK_ENABLED) {
1331 return;
1332 }
1333
1334 if (LockLocalServiceUtil.isLocked(Staging.class.getName(), groupId)) {
1335 Lock lock = LockLocalServiceUtil.getLock(
1336 Staging.class.getName(), groupId);
1337
1338 throw new DuplicateLockException(lock);
1339 }
1340
1341 LockLocalServiceUtil.lock(
1342 userId, Staging.class.getName(), String.valueOf(groupId),
1343 StagingImpl.class.getName(), false,
1344 StagingConstants.LOCK_EXPIRATION_TIME);
1345 }
1346
1347 @Override
1348 public void publishLayout(
1349 long userId, long plid, long liveGroupId, boolean includeChildren)
1350 throws Exception {
1351
1352 Map<String, String[]> parameterMap = getStagingParameters();
1353
1354 parameterMap.put(
1355 PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
1356 new String[] {Boolean.FALSE.toString()});
1357
1358 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
1359
1360 List<Layout> layouts = new ArrayList<Layout>();
1361
1362 layouts.add(layout);
1363
1364 layouts.addAll(getMissingParentLayouts(layout, liveGroupId));
1365
1366 if (includeChildren) {
1367 layouts.addAll(layout.getAllChildren());
1368 }
1369
1370 long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layouts);
1371
1372 publishLayouts(
1373 userId, layout.getGroupId(), liveGroupId, layout.isPrivateLayout(),
1374 layoutIds, parameterMap, null, null);
1375 }
1376
1377 @Override
1378 public void publishLayouts(
1379 long userId, long sourceGroupId, long targetGroupId,
1380 boolean privateLayout, long[] layoutIds,
1381 Map<String, String[]> parameterMap, Date startDate, Date endDate)
1382 throws PortalException, SystemException {
1383
1384 parameterMap.put(
1385 PortletDataHandlerKeys.PERFORM_DIRECT_BINARY_IMPORT,
1386 new String[] {Boolean.TRUE.toString()});
1387
1388 Map<String, Serializable> taskContextMap =
1389 BackgroundTaskContextMapFactory.buildTaskContextMap(
1390 userId, sourceGroupId, privateLayout, layoutIds, parameterMap,
1391 Constants.PUBLISH, startDate, endDate, StringPool.BLANK);
1392
1393 taskContextMap.put("sourceGroupId", sourceGroupId);
1394 taskContextMap.put("targetGroupId", targetGroupId);
1395
1396 BackgroundTaskLocalServiceUtil.addBackgroundTask(
1397 userId, sourceGroupId, StringPool.BLANK, null,
1398 LayoutStagingBackgroundTaskExecutor.class, taskContextMap,
1399 new ServiceContext());
1400 }
1401
1402 @Override
1403 public void publishLayouts(
1404 long userId, long sourceGroupId, long targetGroupId,
1405 boolean privateLayout, Map<Long, Boolean> layoutIdMap,
1406 Map<String, String[]> parameterMap, Date startDate, Date endDate)
1407 throws PortalException, SystemException {
1408
1409 List<Layout> layouts = new ArrayList<Layout>();
1410
1411 for (Map.Entry<Long, Boolean> entry : layoutIdMap.entrySet()) {
1412 long plid = GetterUtil.getLong(String.valueOf(entry.getKey()));
1413 boolean includeChildren = entry.getValue();
1414
1415 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
1416
1417 if (!layouts.contains(layout)) {
1418 layouts.add(layout);
1419 }
1420
1421 List<Layout> parentLayouts = getMissingParentLayouts(
1422 layout, targetGroupId);
1423
1424 for (Layout parentLayout : parentLayouts) {
1425 if (!layouts.contains(parentLayout)) {
1426 layouts.add(parentLayout);
1427 }
1428 }
1429
1430 if (includeChildren) {
1431 for (Layout childLayout : layout.getAllChildren()) {
1432 if (!layouts.contains(childLayout)) {
1433 layouts.add(childLayout);
1434 }
1435 }
1436 }
1437 }
1438
1439 long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layouts);
1440
1441 publishLayouts(
1442 userId, sourceGroupId, targetGroupId, privateLayout, layoutIds,
1443 parameterMap, startDate, endDate);
1444 }
1445
1446 @Override
1447 public void publishLayouts(
1448 long userId, long sourceGroupId, long targetGroupId,
1449 boolean privateLayout, Map<String, String[]> parameterMap,
1450 Date startDate, Date endDate)
1451 throws PortalException, SystemException {
1452
1453 publishLayouts(
1454 userId, sourceGroupId, targetGroupId, privateLayout, (long[])null,
1455 parameterMap, startDate, endDate);
1456 }
1457
1458 @Override
1459 public void publishToLive(PortletRequest portletRequest) throws Exception {
1460 long groupId = ParamUtil.getLong(portletRequest, "groupId");
1461
1462 Group liveGroup = GroupLocalServiceUtil.getGroup(groupId);
1463
1464 Map<String, String[]> parameterMap = getStagingParameters(
1465 portletRequest);
1466
1467 if (liveGroup.isStaged()) {
1468 if (liveGroup.isStagedRemotely()) {
1469 publishToRemote(portletRequest);
1470 }
1471 else {
1472 Group stagingGroup = liveGroup.getStagingGroup();
1473
1474 publishLayouts(
1475 portletRequest, stagingGroup.getGroupId(), groupId,
1476 parameterMap, false);
1477 }
1478 }
1479 }
1480
1481 @Override
1482 public void publishToLive(PortletRequest portletRequest, Portlet portlet)
1483 throws Exception {
1484
1485 long plid = ParamUtil.getLong(portletRequest, "plid");
1486
1487 Layout sourceLayout = LayoutLocalServiceUtil.getLayout(plid);
1488
1489 Group stagingGroup = null;
1490 Group liveGroup = null;
1491
1492 Layout targetLayout = null;
1493
1494 long scopeGroupId = PortalUtil.getScopeGroupId(portletRequest);
1495
1496 if (sourceLayout.hasScopeGroup() &&
1497 (sourceLayout.getScopeGroup().getGroupId() == scopeGroupId)) {
1498
1499 stagingGroup = sourceLayout.getScopeGroup();
1500 liveGroup = stagingGroup.getLiveGroup();
1501
1502 targetLayout = LayoutLocalServiceUtil.getLayout(
1503 liveGroup.getClassPK());
1504 }
1505 else {
1506 stagingGroup = sourceLayout.getGroup();
1507 liveGroup = stagingGroup.getLiveGroup();
1508
1509 targetLayout = LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
1510 sourceLayout.getUuid(), liveGroup.getGroupId(),
1511 sourceLayout.isPrivateLayout());
1512 }
1513
1514 long sourcePlid = sourceLayout.getPlid();
1515
1516 long targetPlid = LayoutConstants.DEFAULT_PLID;
1517
1518 if (targetLayout != null) {
1519 targetPlid = targetLayout.getPlid();
1520 }
1521 else if ((targetLayout == null) && sourceLayout.isTypeControlPanel()) {
1522 targetPlid = sourcePlid;
1523 }
1524
1525 copyPortlet(
1526 portletRequest, stagingGroup.getGroupId(), liveGroup.getGroupId(),
1527 sourcePlid, targetPlid, portlet.getPortletId());
1528 }
1529
1530 @Override
1531 public void publishToRemote(PortletRequest portletRequest)
1532 throws Exception {
1533
1534 publishToRemote(portletRequest, false);
1535 }
1536
1537 @Override
1538 public void scheduleCopyFromLive(PortletRequest portletRequest)
1539 throws Exception {
1540
1541 long stagingGroupId = ParamUtil.getLong(
1542 portletRequest, "stagingGroupId");
1543
1544 Group stagingGroup = GroupLocalServiceUtil.getGroup(stagingGroupId);
1545
1546 long liveGroupId = stagingGroup.getLiveGroupId();
1547
1548 Map<String, String[]> parameterMap = getStagingParameters(
1549 portletRequest);
1550
1551 publishLayouts(
1552 portletRequest, liveGroupId, stagingGroupId, parameterMap, true);
1553 }
1554
1555 @Override
1556 public void schedulePublishToLive(PortletRequest portletRequest)
1557 throws Exception {
1558
1559 long stagingGroupId = ParamUtil.getLong(
1560 portletRequest, "stagingGroupId");
1561
1562 Group stagingGroup = GroupLocalServiceUtil.getGroup(stagingGroupId);
1563
1564 long liveGroupId = stagingGroup.getLiveGroupId();
1565
1566 Map<String, String[]> parameterMap = getStagingParameters(
1567 portletRequest);
1568
1569 publishLayouts(
1570 portletRequest, stagingGroupId, liveGroupId, parameterMap, true);
1571 }
1572
1573 @Override
1574 public void schedulePublishToRemote(PortletRequest portletRequest)
1575 throws Exception {
1576
1577 publishToRemote(portletRequest, true);
1578 }
1579
1580 @Override
1581 public void setRecentLayoutBranchId(
1582 HttpServletRequest request, long layoutSetBranchId, long plid,
1583 long layoutBranchId)
1584 throws SystemException {
1585
1586 PortalPreferences portalPreferences =
1587 PortletPreferencesFactoryUtil.getPortalPreferences(request);
1588
1589 setRecentLayoutBranchId(
1590 portalPreferences, layoutSetBranchId, plid, layoutBranchId);
1591 }
1592
1593 @Override
1594 public void setRecentLayoutBranchId(
1595 User user, long layoutSetBranchId, long plid, long layoutBranchId)
1596 throws SystemException {
1597
1598 PortalPreferences portalPreferences = getPortalPreferences(user);
1599
1600 setRecentLayoutBranchId(
1601 portalPreferences, layoutSetBranchId, plid, layoutBranchId);
1602 }
1603
1604 @Override
1605 public void setRecentLayoutRevisionId(
1606 HttpServletRequest request, long layoutSetBranchId, long plid,
1607 long layoutRevisionId)
1608 throws SystemException {
1609
1610 PortalPreferences portalPreferences =
1611 PortletPreferencesFactoryUtil.getPortalPreferences(request);
1612
1613 setRecentLayoutRevisionId(
1614 portalPreferences, layoutSetBranchId, plid, layoutRevisionId);
1615 }
1616
1617 @Override
1618 public void setRecentLayoutRevisionId(
1619 User user, long layoutSetBranchId, long plid, long layoutRevisionId)
1620 throws SystemException {
1621
1622 PortalPreferences portalPreferences = getPortalPreferences(user);
1623
1624 setRecentLayoutRevisionId(
1625 portalPreferences, layoutSetBranchId, plid, layoutRevisionId);
1626 }
1627
1628 @Override
1629 public void setRecentLayoutSetBranchId(
1630 HttpServletRequest request, long layoutSetId, long layoutSetBranchId) {
1631
1632 SessionClicks.put(
1633 request, Staging.class.getName(),
1634 getRecentLayoutSetBranchIdKey(layoutSetId),
1635 String.valueOf(layoutSetBranchId));
1636 }
1637
1638 @Override
1639 public void setRecentLayoutSetBranchId(
1640 User user, long layoutSetId, long layoutSetBranchId)
1641 throws SystemException {
1642
1643 PortalPreferences portalPreferences = getPortalPreferences(user);
1644
1645 portalPreferences.setValue(
1646 Staging.class.getName(), getRecentLayoutSetBranchIdKey(layoutSetId),
1647 String.valueOf(layoutSetBranchId));
1648 }
1649
1650 @Override
1651 public void unlockGroup(long groupId) throws SystemException {
1652 if (!PropsValues.STAGING_LOCK_ENABLED) {
1653 return;
1654 }
1655
1656 LockLocalServiceUtil.unlock(Staging.class.getName(), groupId);
1657 }
1658
1659 @Override
1660 public void unscheduleCopyFromLive(PortletRequest portletRequest)
1661 throws Exception {
1662
1663 long stagingGroupId = ParamUtil.getLong(
1664 portletRequest, "stagingGroupId");
1665
1666 String jobName = ParamUtil.getString(portletRequest, "jobName");
1667 String groupName = getSchedulerGroupName(
1668 DestinationNames.LAYOUTS_LOCAL_PUBLISHER, stagingGroupId);
1669
1670 LayoutServiceUtil.unschedulePublishToLive(
1671 stagingGroupId, jobName, groupName);
1672 }
1673
1674 @Override
1675 public void unschedulePublishToLive(PortletRequest portletRequest)
1676 throws Exception {
1677
1678 long stagingGroupId = ParamUtil.getLong(
1679 portletRequest, "stagingGroupId");
1680
1681 Group stagingGroup = GroupLocalServiceUtil.getGroup(stagingGroupId);
1682
1683 long liveGroupId = stagingGroup.getLiveGroupId();
1684
1685 String jobName = ParamUtil.getString(portletRequest, "jobName");
1686 String groupName = getSchedulerGroupName(
1687 DestinationNames.LAYOUTS_LOCAL_PUBLISHER, liveGroupId);
1688
1689 LayoutServiceUtil.unschedulePublishToLive(
1690 liveGroupId, jobName, groupName);
1691 }
1692
1693 @Override
1694 public void unschedulePublishToRemote(PortletRequest portletRequest)
1695 throws Exception {
1696
1697 long groupId = ParamUtil.getLong(portletRequest, "groupId");
1698
1699 String jobName = ParamUtil.getString(portletRequest, "jobName");
1700 String groupName = getSchedulerGroupName(
1701 DestinationNames.LAYOUTS_REMOTE_PUBLISHER, groupId);
1702
1703 LayoutServiceUtil.unschedulePublishToRemote(
1704 groupId, jobName, groupName);
1705 }
1706
1707 @Override
1708 public void updateLastImportSettings(
1709 Element layoutElement, Layout layout,
1710 PortletDataContext portletDataContext)
1711 throws Exception {
1712
1713 Map<String, String[]> parameterMap =
1714 portletDataContext.getParameterMap();
1715
1716 String cmd = MapUtil.getString(parameterMap, Constants.CMD);
1717
1718 if (!cmd.equals("publish_to_live")) {
1719 return;
1720 }
1721
1722 UnicodeProperties typeSettingsProperties =
1723 layout.getTypeSettingsProperties();
1724
1725 typeSettingsProperties.setProperty(
1726 "last-import-date", String.valueOf(System.currentTimeMillis()));
1727
1728 String layoutRevisionId = GetterUtil.getString(
1729 layoutElement.attributeValue("layout-revision-id"));
1730
1731 typeSettingsProperties.setProperty(
1732 "last-import-layout-revision-id", layoutRevisionId);
1733
1734 String layoutSetBranchId = MapUtil.getString(
1735 parameterMap, "layoutSetBranchId");
1736
1737 typeSettingsProperties.setProperty(
1738 "last-import-layout-set-branch-id", layoutSetBranchId);
1739
1740 String layoutSetBranchName = MapUtil.getString(
1741 parameterMap, "layoutSetBranchName");
1742
1743 typeSettingsProperties.setProperty(
1744 "last-import-layout-set-branch-name", layoutSetBranchName);
1745
1746 String lastImportUserName = MapUtil.getString(
1747 parameterMap, "lastImportUserName");
1748
1749 typeSettingsProperties.setProperty(
1750 "last-import-user-name", lastImportUserName);
1751
1752 String lastImportUserUuid = MapUtil.getString(
1753 parameterMap, "lastImportUserUuid");
1754
1755 typeSettingsProperties.setProperty(
1756 "last-import-user-uuid", lastImportUserUuid);
1757
1758 String layoutBranchId = GetterUtil.getString(
1759 layoutElement.attributeValue("layout-branch-id"));
1760
1761 typeSettingsProperties.setProperty(
1762 "last-import-layout-branch-id", layoutBranchId);
1763
1764 String layoutBranchName = GetterUtil.getString(
1765 layoutElement.attributeValue("layout-branch-name"));
1766
1767 typeSettingsProperties.setProperty(
1768 "last-import-layout-branch-name", layoutBranchName);
1769
1770 layout.setTypeSettingsProperties(typeSettingsProperties);
1771 }
1772
1773 @Override
1774 public void updateLastPublishDate(
1775 long groupId, boolean privateLayout, Date lastPublishDate)
1776 throws Exception {
1777
1778 LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
1779 groupId, privateLayout);
1780
1781 UnicodeProperties settingsProperties =
1782 layoutSet.getSettingsProperties();
1783
1784 settingsProperties.setProperty(
1785 "last-publish-date", String.valueOf(lastPublishDate.getTime()));
1786
1787 LayoutSetLocalServiceUtil.updateSettings(
1788 layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
1789 settingsProperties.toString());
1790 }
1791
1792 @Override
1793 public void updateLastPublishDate(
1794 String portletId, PortletPreferences portletPreferences,
1795 Date lastPublishDate)
1796 throws Exception {
1797
1798 try {
1799 portletPreferences.setValue(
1800 "last-publish-date", String.valueOf(lastPublishDate.getTime()));
1801
1802 portletPreferences.store();
1803 }
1804 catch (UnsupportedOperationException uoe) {
1805 if (_log.isDebugEnabled()) {
1806 _log.debug(
1807 "Not updating the portlet setup for " + portletId +
1808 " because no setup was returned for the current " +
1809 "page");
1810 }
1811 }
1812 catch (Exception e) {
1813 _log.error(e, e);
1814 }
1815 }
1816
1817 @Override
1818 public void updateStaging(PortletRequest portletRequest, Group liveGroup)
1819 throws Exception {
1820
1821 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
1822 WebKeys.THEME_DISPLAY);
1823
1824 PermissionChecker permissionChecker =
1825 themeDisplay.getPermissionChecker();
1826
1827 long userId = permissionChecker.getUserId();
1828
1829 if (!GroupPermissionUtil.contains(
1830 permissionChecker, liveGroup.getGroupId(),
1831 ActionKeys.MANAGE_STAGING)) {
1832
1833 return;
1834 }
1835
1836 int stagingType = getStagingType(portletRequest, liveGroup);
1837
1838 boolean branchingPublic = getBoolean(
1839 portletRequest, liveGroup, "branchingPublic");
1840 boolean branchingPrivate = getBoolean(
1841 portletRequest, liveGroup, "branchingPrivate");
1842
1843 ServiceContext serviceContext =
1844 ServiceContextThreadLocal.getServiceContext();
1845
1846 if (stagingType == StagingConstants.TYPE_NOT_STAGED) {
1847 if (liveGroup.hasStagingGroup() || liveGroup.isStagedRemotely()) {
1848 StagingLocalServiceUtil.disableStaging(
1849 portletRequest, liveGroup, serviceContext);
1850 }
1851 }
1852 else if (stagingType == StagingConstants.TYPE_LOCAL_STAGING) {
1853 StagingLocalServiceUtil.enableLocalStaging(
1854 userId, liveGroup, branchingPublic, branchingPrivate,
1855 serviceContext);
1856 }
1857 else if (stagingType == StagingConstants.TYPE_REMOTE_STAGING) {
1858 String remoteAddress = getString(
1859 portletRequest, liveGroup, "remoteAddress");
1860
1861 remoteAddress = stripProtocolFromRemoteAddress(remoteAddress);
1862
1863 int remotePort = getInteger(
1864 portletRequest, liveGroup, "remotePort");
1865 String remotePathContext = getString(
1866 portletRequest, liveGroup, "remotePathContext");
1867 boolean secureConnection = getBoolean(
1868 portletRequest, liveGroup, "secureConnection");
1869 long remoteGroupId = getLong(
1870 portletRequest, liveGroup, "remoteGroupId");
1871
1872 StagingLocalServiceUtil.enableRemoteStaging(
1873 userId, liveGroup, branchingPublic, branchingPrivate,
1874 remoteAddress, remotePort, remotePathContext, secureConnection,
1875 remoteGroupId, serviceContext);
1876 }
1877 }
1878
1879 @Override
1880 public void validateRemote(
1881 String remoteAddress, int remotePort, String remotePathContext,
1882 boolean secureConnection, long remoteGroupId)
1883 throws PortalException {
1884
1885 RemoteOptionsException roe = null;
1886
1887 if (!Validator.isDomain(remoteAddress) &&
1888 !Validator.isIPAddress(remoteAddress)) {
1889
1890 roe = new RemoteOptionsException(
1891 RemoteOptionsException.REMOTE_ADDRESS);
1892
1893 roe.setRemoteAddress(remoteAddress);
1894
1895 throw roe;
1896 }
1897
1898 if ((remotePort < 1) || (remotePort > 65535)) {
1899 roe = new RemoteOptionsException(
1900 RemoteOptionsException.REMOTE_PORT);
1901
1902 roe.setRemotePort(remotePort);
1903
1904 throw roe;
1905 }
1906
1907 if (Validator.isNotNull(remotePathContext) &&
1908 (!remotePathContext.startsWith(StringPool.FORWARD_SLASH) ||
1909 remotePathContext.endsWith(StringPool.FORWARD_SLASH))) {
1910
1911 roe = new RemoteOptionsException(
1912 RemoteOptionsException.REMOTE_PATH_CONTEXT);
1913
1914 roe.setRemotePathContext(remotePathContext);
1915
1916 throw roe;
1917 }
1918
1919 if (remoteGroupId <= 0) {
1920 roe = new RemoteOptionsException(
1921 RemoteOptionsException.REMOTE_GROUP_ID);
1922
1923 roe.setRemoteGroupId(remoteGroupId);
1924
1925 throw roe;
1926 }
1927
1928 PermissionChecker permissionChecker =
1929 PermissionThreadLocal.getPermissionChecker();
1930
1931 User user = permissionChecker.getUser();
1932
1933 String remoteURL = buildRemoteURL(
1934 remoteAddress, remotePort, remotePathContext, secureConnection,
1935 GroupConstants.DEFAULT_LIVE_GROUP_ID, false);
1936
1937 HttpPrincipal httpPrincipal = new HttpPrincipal(
1938 remoteURL, user.getEmailAddress(), user.getPassword(),
1939 user.getPasswordEncrypted());
1940
1941
1942
1943
1944 try {
1945 GroupServiceHttp.checkRemoteStagingGroup(
1946 httpPrincipal, remoteGroupId);
1947 }
1948 catch (NoSuchGroupException nsge) {
1949 RemoteExportException ree = new RemoteExportException(
1950 RemoteExportException.NO_GROUP);
1951
1952 ree.setGroupId(remoteGroupId);
1953
1954 throw ree;
1955 }
1956 catch (PrincipalException pe) {
1957 RemoteExportException ree = new RemoteExportException(
1958 RemoteExportException.NO_PERMISSIONS);
1959
1960 ree.setGroupId(remoteGroupId);
1961
1962 throw ree;
1963 }
1964 catch (RemoteAuthException rae) {
1965 rae.setURL(remoteURL);
1966
1967 throw rae;
1968 }
1969 catch (SystemException se) {
1970 RemoteExportException ree = new RemoteExportException(
1971 RemoteExportException.BAD_CONNECTION, se.getMessage());
1972
1973 ree.setURL(remoteURL);
1974
1975 throw ree;
1976 }
1977 }
1978
1979 protected void addDefaultLayoutSetBranch(
1980 long userId, long groupId, String groupName, boolean privateLayout,
1981 ServiceContext serviceContext)
1982 throws PortalException, SystemException {
1983
1984 String masterBranchDescription =
1985 LayoutSetBranchConstants.MASTER_BRANCH_DESCRIPTION_PUBLIC;
1986
1987 if (privateLayout) {
1988 masterBranchDescription =
1989 LayoutSetBranchConstants.MASTER_BRANCH_DESCRIPTION_PRIVATE;
1990 }
1991
1992 String description = LanguageUtil.format(
1993 PortalUtil.getSiteDefaultLocale(groupId), masterBranchDescription,
1994 groupName);
1995
1996 try {
1997 LayoutSetBranch layoutSetBranch =
1998 LayoutSetBranchLocalServiceUtil.addLayoutSetBranch(
1999 userId, groupId, privateLayout,
2000 LayoutSetBranchConstants.MASTER_BRANCH_NAME, description,
2001 true, LayoutSetBranchConstants.ALL_BRANCHES,
2002 serviceContext);
2003
2004 List<LayoutRevision> layoutRevisions =
2005 LayoutRevisionLocalServiceUtil.getLayoutRevisions(
2006 layoutSetBranch.getLayoutSetBranchId(), false);
2007
2008 for (LayoutRevision layoutRevision : layoutRevisions) {
2009 LayoutRevisionLocalServiceUtil.updateStatus(
2010 userId, layoutRevision.getLayoutRevisionId(),
2011 WorkflowConstants.STATUS_APPROVED, serviceContext);
2012 }
2013 }
2014 catch (PortalException pe) {
2015 if (_log.isWarnEnabled()) {
2016 _log.warn(
2017 "Unable to create master branch for " +
2018 (privateLayout ? "private" : "public") + " layouts",
2019 pe);
2020 }
2021 }
2022 }
2023
2024 protected void deleteRecentLayoutRevisionId(
2025 PortalPreferences portalPreferences, long layoutSetBranchId,
2026 long plid) {
2027
2028 portalPreferences.setValue(
2029 Staging.class.getName(),
2030 getRecentLayoutRevisionIdKey(layoutSetBranchId, plid), null);
2031 }
2032
2033 protected boolean getBoolean(
2034 PortletRequest portletRequest, Group group, String param) {
2035
2036 return ParamUtil.getBoolean(
2037 portletRequest, param,
2038 GetterUtil.getBoolean(group.getTypeSettingsProperty(param)));
2039 }
2040
2041 protected int getInteger(
2042 PortletRequest portletRequest, Group group, String param) {
2043
2044 return ParamUtil.getInteger(
2045 portletRequest, param,
2046 GetterUtil.getInteger(group.getTypeSettingsProperty(param)));
2047 }
2048
2049 protected long getLong(
2050 PortletRequest portletRequest, Group group, String param) {
2051
2052 return ParamUtil.getLong(
2053 portletRequest, param,
2054 GetterUtil.getLong(group.getTypeSettingsProperty(param)));
2055 }
2056
2057 protected PortalPreferences getPortalPreferences(User user)
2058 throws SystemException {
2059
2060 boolean signedIn = !user.isDefaultUser();
2061
2062 PortalPreferences portalPreferences =
2063 PortletPreferencesFactoryUtil.getPortalPreferences(
2064 user.getUserId(), signedIn);
2065
2066 return portalPreferences;
2067 }
2068
2069 protected long getRecentLayoutBranchId(
2070 PortalPreferences portalPreferences, long layoutSetBranchId,
2071 long plid) {
2072
2073 return GetterUtil.getLong(
2074 portalPreferences.getValue(
2075 Staging.class.getName(),
2076 getRecentLayoutBranchIdKey(layoutSetBranchId, plid)));
2077 }
2078
2079 protected String getRecentLayoutBranchIdKey(
2080 long layoutSetBranchId, long plid) {
2081
2082 StringBundler sb = new StringBundler(4);
2083
2084 sb.append("layoutBranchId-");
2085 sb.append(layoutSetBranchId);
2086 sb.append(StringPool.DASH);
2087 sb.append(plid);
2088
2089 return sb.toString();
2090 }
2091
2092 protected long getRecentLayoutRevisionId(
2093 PortalPreferences portalPreferences, long layoutSetBranchId,
2094 long plid)
2095 throws PortalException, SystemException {
2096
2097 long layoutRevisionId = GetterUtil.getLong(
2098 portalPreferences.getValue(
2099 Staging.class.getName(),
2100 getRecentLayoutRevisionIdKey(layoutSetBranchId, plid)));
2101
2102 if (layoutRevisionId > 0) {
2103 return layoutRevisionId;
2104 }
2105
2106 long layoutBranchId = getRecentLayoutBranchId(
2107 portalPreferences, layoutSetBranchId, plid);
2108
2109 if (layoutBranchId > 0) {
2110 try {
2111 LayoutBranchLocalServiceUtil.getLayoutBranch(layoutBranchId);
2112 }
2113 catch (NoSuchLayoutBranchException nslbe) {
2114 LayoutBranch layoutBranch =
2115 LayoutBranchLocalServiceUtil.getMasterLayoutBranch(
2116 layoutSetBranchId, plid);
2117
2118 layoutBranchId = layoutBranch.getLayoutBranchId();
2119 }
2120 }
2121
2122 if (layoutBranchId > 0) {
2123 try {
2124 LayoutRevision layoutRevision =
2125 LayoutRevisionLocalServiceUtil.getLayoutRevision(
2126 layoutSetBranchId, layoutBranchId, plid);
2127
2128 if (layoutRevision != null) {
2129 layoutRevisionId = layoutRevision.getLayoutRevisionId();
2130 }
2131 }
2132 catch (NoSuchLayoutRevisionException nslre) {
2133 }
2134 }
2135
2136 return layoutRevisionId;
2137 }
2138
2139 protected String getRecentLayoutRevisionIdKey(
2140 long layoutSetBranchId, long plid) {
2141
2142 StringBundler sb = new StringBundler(4);
2143
2144 sb.append("layoutRevisionId-");
2145 sb.append(layoutSetBranchId);
2146 sb.append(StringPool.DASH);
2147 sb.append(plid);
2148
2149 return sb.toString();
2150 }
2151
2152 protected String getRecentLayoutSetBranchIdKey(long layoutSetId) {
2153 return "layoutSetBranchId_" + layoutSetId;
2154 }
2155
2156 protected int getStagingType(
2157 PortletRequest portletRequest, Group liveGroup) {
2158
2159 String stagingType = portletRequest.getParameter("stagingType");
2160
2161 if (stagingType != null) {
2162 return GetterUtil.getInteger(stagingType);
2163 }
2164
2165 if (liveGroup.isStagedRemotely()) {
2166 return StagingConstants.TYPE_REMOTE_STAGING;
2167 }
2168
2169 if (liveGroup.hasStagingGroup()) {
2170 return StagingConstants.TYPE_LOCAL_STAGING;
2171 }
2172
2173 return StagingConstants.TYPE_NOT_STAGED;
2174 }
2175
2176 protected String getString(
2177 PortletRequest portletRequest, Group group, String param) {
2178
2179 return ParamUtil.getString(
2180 portletRequest, param,
2181 GetterUtil.getString(group.getTypeSettingsProperty(param)));
2182 }
2183
2184 protected void publishLayouts(
2185 PortletRequest portletRequest, long sourceGroupId,
2186 long targetGroupId, Map<String, String[]> parameterMap,
2187 boolean schedule)
2188 throws Exception {
2189
2190 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
2191 WebKeys.THEME_DISPLAY);
2192
2193 String tabs1 = ParamUtil.getString(portletRequest, "tabs1");
2194
2195 boolean privateLayout = true;
2196
2197 if (tabs1.equals("public-pages")) {
2198 privateLayout = false;
2199 }
2200
2201 String scope = ParamUtil.getString(portletRequest, "scope");
2202
2203 Map<Long, Boolean> layoutIdMap = new LinkedHashMap<Long, Boolean>();
2204
2205 if (scope.equals("selected-pages")) {
2206 layoutIdMap = ExportImportHelperUtil.getLayoutIdMap(portletRequest);
2207 }
2208
2209 DateRange dateRange = ExportImportHelperUtil.getDateRange(
2210 portletRequest, sourceGroupId, privateLayout, 0, null,
2211 "fromLastPublishDate");
2212
2213 if (schedule) {
2214 String groupName = getSchedulerGroupName(
2215 DestinationNames.LAYOUTS_LOCAL_PUBLISHER, targetGroupId);
2216
2217 int recurrenceType = ParamUtil.getInteger(
2218 portletRequest, "recurrenceType");
2219
2220 Calendar startCalendar = ExportImportHelperUtil.getCalendar(
2221 portletRequest, "schedulerStartDate", true);
2222
2223 String cronText = SchedulerEngineHelperUtil.getCronText(
2224 portletRequest, startCalendar, true, recurrenceType);
2225
2226 Date schedulerEndDate = null;
2227
2228 int endDateType = ParamUtil.getInteger(
2229 portletRequest, "endDateType");
2230
2231 if (endDateType == 1) {
2232 Calendar endCalendar = ExportImportHelperUtil.getCalendar(
2233 portletRequest, "schedulerEndDate", true);
2234
2235 schedulerEndDate = endCalendar.getTime();
2236 }
2237
2238 String description = ParamUtil.getString(
2239 portletRequest, "description");
2240
2241 LayoutServiceUtil.schedulePublishToLive(
2242 sourceGroupId, targetGroupId, privateLayout, layoutIdMap,
2243 parameterMap, scope, dateRange.getStartDate(),
2244 dateRange.getEndDate(), groupName, cronText,
2245 startCalendar.getTime(), schedulerEndDate, description);
2246 }
2247 else {
2248 MessageStatus messageStatus = new MessageStatus();
2249
2250 messageStatus.startTimer();
2251
2252 String command =
2253 LayoutsLocalPublisherRequest.COMMAND_SELECTED_PAGES;
2254
2255 try {
2256 if (scope.equals("all-pages")) {
2257 command = LayoutsLocalPublisherRequest.COMMAND_ALL_PAGES;
2258
2259 publishLayouts(
2260 themeDisplay.getUserId(), sourceGroupId, targetGroupId,
2261 privateLayout, parameterMap, dateRange.getStartDate(),
2262 dateRange.getEndDate());
2263 }
2264 else {
2265 publishLayouts(
2266 themeDisplay.getUserId(), sourceGroupId, targetGroupId,
2267 privateLayout, layoutIdMap, parameterMap,
2268 dateRange.getStartDate(), dateRange.getEndDate());
2269 }
2270 }
2271 catch (Exception e) {
2272 messageStatus.setException(e);
2273
2274 throw e;
2275 }
2276 finally {
2277 messageStatus.stopTimer();
2278
2279 LayoutsLocalPublisherRequest publisherRequest =
2280 new LayoutsLocalPublisherRequest(
2281 command, themeDisplay.getUserId(), sourceGroupId,
2282 targetGroupId, privateLayout, layoutIdMap, parameterMap,
2283 dateRange.getStartDate(), dateRange.getEndDate());
2284
2285 messageStatus.setPayload(publisherRequest);
2286
2287 MessageBusUtil.sendMessage(
2288 DestinationNames.MESSAGE_BUS_MESSAGE_STATUS, messageStatus);
2289 }
2290 }
2291 }
2292
2293 protected void publishToRemote(
2294 PortletRequest portletRequest, boolean schedule)
2295 throws Exception {
2296
2297 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
2298 WebKeys.THEME_DISPLAY);
2299
2300 String tabs1 = ParamUtil.getString(portletRequest, "tabs1");
2301
2302 long groupId = ParamUtil.getLong(portletRequest, "groupId");
2303
2304 boolean privateLayout = true;
2305
2306 if (tabs1.equals("public-pages")) {
2307 privateLayout = false;
2308 }
2309
2310 String scope = ParamUtil.getString(portletRequest, "scope");
2311
2312 if (Validator.isNull(scope)) {
2313 scope = "all-pages";
2314 }
2315
2316 Map<Long, Boolean> layoutIdMap = null;
2317
2318 if (scope.equals("selected-pages")) {
2319 layoutIdMap = ExportImportHelperUtil.getLayoutIdMap(portletRequest);
2320 }
2321
2322 Map<String, String[]> parameterMap = getStagingParameters(
2323 portletRequest);
2324
2325 parameterMap.put(
2326 PortletDataHandlerKeys.PUBLISH_TO_REMOTE,
2327 new String[] {Boolean.TRUE.toString()});
2328
2329 Group group = GroupLocalServiceUtil.getGroup(groupId);
2330
2331 UnicodeProperties groupTypeSettingsProperties =
2332 group.getTypeSettingsProperties();
2333
2334 String remoteAddress = ParamUtil.getString(
2335 portletRequest, "remoteAddress",
2336 groupTypeSettingsProperties.getProperty("remoteAddress"));
2337
2338 remoteAddress = stripProtocolFromRemoteAddress(remoteAddress);
2339
2340 int remotePort = ParamUtil.getInteger(
2341 portletRequest, "remotePort",
2342 GetterUtil.getInteger(
2343 groupTypeSettingsProperties.getProperty("remotePort")));
2344 String remotePathContext = ParamUtil.getString(
2345 portletRequest, "remotePathContext",
2346 groupTypeSettingsProperties.getProperty("remotePathContext"));
2347 boolean secureConnection = ParamUtil.getBoolean(
2348 portletRequest, "secureConnection",
2349 GetterUtil.getBoolean(
2350 groupTypeSettingsProperties.getProperty("secureConnection")));
2351 long remoteGroupId = ParamUtil.getLong(
2352 portletRequest, "remoteGroupId",
2353 GetterUtil.getLong(
2354 groupTypeSettingsProperties.getProperty("remoteGroupId")));
2355 boolean remotePrivateLayout = ParamUtil.getBoolean(
2356 portletRequest, "remotePrivateLayout");
2357
2358 validateRemote(
2359 remoteAddress, remotePort, remotePathContext, secureConnection,
2360 remoteGroupId);
2361
2362 DateRange dateRange = ExportImportHelperUtil.getDateRange(
2363 portletRequest, groupId, privateLayout, 0, null,
2364 "fromLastPublishDate");
2365
2366 if (schedule) {
2367 String groupName = getSchedulerGroupName(
2368 DestinationNames.LAYOUTS_REMOTE_PUBLISHER, groupId);
2369
2370 int recurrenceType = ParamUtil.getInteger(
2371 portletRequest, "recurrenceType");
2372
2373 Calendar startCalendar = ExportImportHelperUtil.getCalendar(
2374 portletRequest, "schedulerStartDate", true);
2375
2376 String cronText = SchedulerEngineHelperUtil.getCronText(
2377 portletRequest, startCalendar, true, recurrenceType);
2378
2379 Date schedulerEndDate = null;
2380
2381 int endDateType = ParamUtil.getInteger(
2382 portletRequest, "endDateType");
2383
2384 if (endDateType == 1) {
2385 Calendar endCalendar = ExportImportHelperUtil.getCalendar(
2386 portletRequest, "schedulerEndDate", true);
2387
2388 schedulerEndDate = endCalendar.getTime();
2389 }
2390
2391 String description = ParamUtil.getString(
2392 portletRequest, "description");
2393
2394 LayoutServiceUtil.schedulePublishToRemote(
2395 groupId, privateLayout, layoutIdMap, parameterMap,
2396 remoteAddress, remotePort, remotePathContext, secureConnection,
2397 remoteGroupId, remotePrivateLayout, dateRange.getStartDate(),
2398 dateRange.getEndDate(), groupName, cronText,
2399 startCalendar.getTime(), schedulerEndDate, description);
2400 }
2401 else {
2402 MessageStatus messageStatus = new MessageStatus();
2403
2404 messageStatus.startTimer();
2405
2406 try {
2407 copyRemoteLayouts(
2408 groupId, privateLayout, layoutIdMap, parameterMap,
2409 remoteAddress, remotePort, remotePathContext,
2410 secureConnection, remoteGroupId, remotePrivateLayout,
2411 dateRange.getStartDate(), dateRange.getEndDate());
2412 }
2413 catch (Exception e) {
2414 messageStatus.setException(e);
2415
2416 throw e;
2417 }
2418 finally {
2419 messageStatus.stopTimer();
2420
2421 LayoutsRemotePublisherRequest publisherRequest =
2422 new LayoutsRemotePublisherRequest(
2423 themeDisplay.getUserId(), groupId, privateLayout,
2424 layoutIdMap, parameterMap, remoteAddress, remotePort,
2425 remotePathContext, secureConnection, remoteGroupId,
2426 remotePrivateLayout, dateRange.getStartDate(),
2427 dateRange.getEndDate());
2428
2429 messageStatus.setPayload(publisherRequest);
2430
2431 MessageBusUtil.sendMessage(
2432 DestinationNames.MESSAGE_BUS_MESSAGE_STATUS, messageStatus);
2433 }
2434 }
2435 }
2436
2437 protected void setRecentLayoutBranchId(
2438 PortalPreferences portalPreferences, long layoutSetBranchId, long plid,
2439 long layoutBranchId) {
2440
2441 portalPreferences.setValue(
2442 Staging.class.getName(),
2443 getRecentLayoutBranchIdKey(layoutSetBranchId, plid),
2444 String.valueOf(layoutBranchId));
2445 }
2446
2447 protected void setRecentLayoutRevisionId(
2448 PortalPreferences portalPreferences, long layoutSetBranchId,
2449 long plid, long layoutRevisionId)
2450 throws SystemException {
2451
2452 long layoutBranchId = 0;
2453
2454 try {
2455 LayoutRevision layoutRevision =
2456 LayoutRevisionLocalServiceUtil.getLayoutRevision(
2457 layoutRevisionId);
2458
2459 layoutBranchId = layoutRevision.getLayoutBranchId();
2460
2461 LayoutRevision lastLayoutRevision =
2462 LayoutRevisionLocalServiceUtil.getLayoutRevision(
2463 layoutSetBranchId, layoutBranchId, plid);
2464
2465 if (lastLayoutRevision.getLayoutRevisionId() == layoutRevisionId) {
2466 deleteRecentLayoutRevisionId(
2467 portalPreferences, layoutSetBranchId, plid);
2468 }
2469 else {
2470 portalPreferences.setValue(
2471 Staging.class.getName(),
2472 getRecentLayoutRevisionIdKey(layoutSetBranchId, plid),
2473 String.valueOf(layoutRevisionId));
2474 }
2475 }
2476 catch (PortalException pe) {
2477 if (_log.isWarnEnabled()) {
2478 _log.warn("Unable to set recent layout revision ID", pe);
2479 }
2480 }
2481
2482 portalPreferences.setValue(
2483 Staging.class.getName(),
2484 getRecentLayoutBranchIdKey(layoutSetBranchId, plid),
2485 String.valueOf(layoutBranchId));
2486 }
2487
2488 protected String stripProtocolFromRemoteAddress(String remoteAddress) {
2489 if (remoteAddress.startsWith(Http.HTTP_WITH_SLASH)) {
2490 remoteAddress = remoteAddress.substring(
2491 Http.HTTP_WITH_SLASH.length());
2492 }
2493 else if (remoteAddress.startsWith(Http.HTTPS_WITH_SLASH)) {
2494 remoteAddress = remoteAddress.substring(
2495 Http.HTTPS_WITH_SLASH.length());
2496 }
2497
2498 return remoteAddress;
2499 }
2500
2501 protected void updateGroupTypeSettingsProperties(
2502 Group group, String remoteAddress, int remotePort,
2503 String remotePathContext, boolean secureConnection,
2504 long remoteGroupId)
2505 throws Exception {
2506
2507 UnicodeProperties typeSettingsProperties =
2508 group.getTypeSettingsProperties();
2509
2510 typeSettingsProperties.setProperty("remoteAddress", remoteAddress);
2511 typeSettingsProperties.setProperty(
2512 "remoteGroupId", String.valueOf(remoteGroupId));
2513 typeSettingsProperties.setProperty(
2514 "remotePathContext", remotePathContext);
2515 typeSettingsProperties.setProperty(
2516 "remotePort", String.valueOf(remotePort));
2517 typeSettingsProperties.setProperty(
2518 "secureConnection", String.valueOf(secureConnection));
2519
2520 group.setTypeSettingsProperties(typeSettingsProperties);
2521
2522 GroupLocalServiceUtil.updateGroup(group);
2523 }
2524
2525 private static Log _log = LogFactoryUtil.getLog(StagingImpl.class);
2526
2527 }