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