001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.staging;
016    
017    import com.liferay.portal.LayoutSetBranchNameException;
018    import com.liferay.portal.NoSuchGroupException;
019    import com.liferay.portal.NoSuchLayoutBranchException;
020    import com.liferay.portal.NoSuchLayoutException;
021    import com.liferay.portal.NoSuchLayoutRevisionException;
022    import com.liferay.portal.RemoteExportException;
023    import com.liferay.portal.RemoteOptionsException;
024    import com.liferay.portal.kernel.exception.PortalException;
025    import com.liferay.portal.kernel.exception.SystemException;
026    import com.liferay.portal.kernel.language.LanguageUtil;
027    import com.liferay.portal.kernel.lar.PortletDataContext;
028    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
029    import com.liferay.portal.kernel.lar.UserIdStrategy;
030    import com.liferay.portal.kernel.messaging.DestinationNames;
031    import com.liferay.portal.kernel.messaging.MessageBusUtil;
032    import com.liferay.portal.kernel.messaging.MessageStatus;
033    import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
034    import com.liferay.portal.kernel.staging.LayoutStagingUtil;
035    import com.liferay.portal.kernel.staging.Staging;
036    import com.liferay.portal.kernel.staging.StagingConstants;
037    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
038    import com.liferay.portal.kernel.util.GetterUtil;
039    import com.liferay.portal.kernel.util.Http;
040    import com.liferay.portal.kernel.util.LocaleUtil;
041    import com.liferay.portal.kernel.util.MapUtil;
042    import com.liferay.portal.kernel.util.ParamUtil;
043    import com.liferay.portal.kernel.util.StringBundler;
044    import com.liferay.portal.kernel.util.StringPool;
045    import com.liferay.portal.kernel.util.Time;
046    import com.liferay.portal.kernel.util.TimeZoneUtil;
047    import com.liferay.portal.kernel.util.UnicodeProperties;
048    import com.liferay.portal.kernel.util.Validator;
049    import com.liferay.portal.kernel.workflow.WorkflowConstants;
050    import com.liferay.portal.kernel.xml.Element;
051    import com.liferay.portal.lar.LayoutExporter;
052    import com.liferay.portal.messaging.LayoutsLocalPublisherRequest;
053    import com.liferay.portal.messaging.LayoutsRemotePublisherRequest;
054    import com.liferay.portal.model.Group;
055    import com.liferay.portal.model.GroupConstants;
056    import com.liferay.portal.model.Layout;
057    import com.liferay.portal.model.LayoutBranch;
058    import com.liferay.portal.model.LayoutRevision;
059    import com.liferay.portal.model.LayoutSet;
060    import com.liferay.portal.model.LayoutSetBranchConstants;
061    import com.liferay.portal.model.Portlet;
062    import com.liferay.portal.model.User;
063    import com.liferay.portal.security.auth.HttpPrincipal;
064    import com.liferay.portal.security.auth.PrincipalException;
065    import com.liferay.portal.security.permission.ActionKeys;
066    import com.liferay.portal.security.permission.PermissionChecker;
067    import com.liferay.portal.security.permission.PermissionThreadLocal;
068    import com.liferay.portal.service.GroupLocalServiceUtil;
069    import com.liferay.portal.service.LayoutBranchLocalServiceUtil;
070    import com.liferay.portal.service.LayoutLocalServiceUtil;
071    import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
072    import com.liferay.portal.service.LayoutServiceUtil;
073    import com.liferay.portal.service.LayoutSetBranchLocalServiceUtil;
074    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
075    import com.liferay.portal.service.ServiceContext;
076    import com.liferay.portal.service.ServiceContextThreadLocal;
077    import com.liferay.portal.service.UserLocalServiceUtil;
078    import com.liferay.portal.service.http.GroupServiceHttp;
079    import com.liferay.portal.service.http.LayoutServiceHttp;
080    import com.liferay.portal.service.permission.GroupPermissionUtil;
081    import com.liferay.portal.theme.ThemeDisplay;
082    import com.liferay.portal.util.PortalUtil;
083    import com.liferay.portal.util.SessionClicks;
084    import com.liferay.portal.util.WebKeys;
085    import com.liferay.portlet.PortalPreferences;
086    import com.liferay.portlet.PortletPreferencesFactoryUtil;
087    
088    import java.io.File;
089    
090    import java.util.ArrayList;
091    import java.util.Calendar;
092    import java.util.Date;
093    import java.util.HashSet;
094    import java.util.Iterator;
095    import java.util.LinkedHashMap;
096    import java.util.List;
097    import java.util.Locale;
098    import java.util.Map.Entry;
099    import java.util.Map;
100    import java.util.Set;
101    import java.util.TimeZone;
102    
103    import javax.portlet.PortletRequest;
104    
105    import javax.servlet.http.HttpServletRequest;
106    
107    /**
108     * @author Raymond Augé
109     * @author Bruno Farache
110     * @author Wesley Gong
111     */
112    public class StagingImpl implements Staging {
113    
114            public String buildRemoteURL(
115                    String remoteAddress, int remotePort, boolean secureConnection,
116                    long remoteGroupId, boolean privateLayout) {
117    
118                    StringBundler sb = new StringBundler((remoteGroupId > 0) ? 4 : 9);
119    
120                    if (secureConnection) {
121                            sb.append(Http.HTTPS_WITH_SLASH);
122                    }
123                    else {
124                            sb.append(Http.HTTP_WITH_SLASH);
125                    }
126    
127                    sb.append(remoteAddress);
128                    sb.append(StringPool.COLON);
129                    sb.append(remotePort);
130    
131                    if (remoteGroupId > 0) {
132                            sb.append("/c/my_sites/view?");
133                            sb.append("groupId=");
134                            sb.append(remoteGroupId);
135                            sb.append("&privateLayout=");
136                            sb.append(privateLayout);
137                    }
138    
139                    return sb.toString();
140            }
141    
142            public void copyFromLive(PortletRequest portletRequest) throws Exception {
143                    long stagingGroupId = ParamUtil.getLong(
144                            portletRequest, "stagingGroupId");
145    
146                    Group stagingGroup = GroupLocalServiceUtil.getGroup(stagingGroupId);
147    
148                    long liveGroupId = stagingGroup.getLiveGroupId();
149    
150                    Map<String, String[]> parameterMap = getStagingParameters(
151                            portletRequest);
152    
153                    publishLayouts(
154                            portletRequest, liveGroupId, stagingGroupId, parameterMap, false);
155            }
156    
157            public void copyFromLive(PortletRequest portletRequest, Portlet portlet)
158                    throws Exception {
159    
160                    long plid = ParamUtil.getLong(portletRequest, "plid");
161    
162                    Layout targetLayout = LayoutLocalServiceUtil.getLayout(plid);
163    
164                    Group stagingGroup = targetLayout.getGroup();
165                    Group liveGroup = stagingGroup.getLiveGroup();
166    
167                    Layout sourceLayout = LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
168                            targetLayout.getUuid(), liveGroup.getGroupId());
169    
170                    copyPortlet(
171                            portletRequest, liveGroup.getGroupId(), stagingGroup.getGroupId(),
172                            sourceLayout.getPlid(), targetLayout.getPlid(),
173                            portlet.getPortletId());
174            }
175    
176            public void copyPortlet(
177                            PortletRequest portletRequest, long sourceGroupId,
178                            long targetGroupId, long sourcePlid, long targetPlid,
179                            String portletId)
180                    throws Exception {
181    
182                    long userId = PortalUtil.getUserId(portletRequest);
183    
184                    Map<String, String[]> parameterMap = getStagingParameters(
185                            portletRequest);
186    
187                    File file = LayoutLocalServiceUtil.exportPortletInfoAsFile(
188                            sourcePlid, sourceGroupId, portletId, parameterMap, null, null);
189    
190                    try {
191                            LayoutLocalServiceUtil.importPortletInfo(
192                                    userId, targetPlid, targetGroupId, portletId, parameterMap,
193                                    file);
194                    }
195                    finally {
196                            file.delete();
197                    }
198            }
199    
200            public void copyRemoteLayouts(
201                            long sourceGroupId, boolean privateLayout,
202                            Map<Long, Boolean> layoutIdMap, Map<String, String[]> parameterMap,
203                            String remoteAddress, int remotePort, boolean secureConnection,
204                            long remoteGroupId, boolean remotePrivateLayout, Date startDate,
205                            Date endDate)
206                    throws Exception {
207    
208                    PermissionChecker permissionChecker =
209                            PermissionThreadLocal.getPermissionChecker();
210    
211                    User user = UserLocalServiceUtil.getUser(permissionChecker.getUserId());
212    
213                    StringBundler sb = new StringBundler(4);
214    
215                    if (secureConnection) {
216                            sb.append(Http.HTTPS_WITH_SLASH);
217                    }
218                    else {
219                            sb.append(Http.HTTP_WITH_SLASH);
220                    }
221    
222                    sb.append(remoteAddress);
223                    sb.append(StringPool.COLON);
224                    sb.append(remotePort);
225    
226                    String url = sb.toString();
227    
228                    HttpPrincipal httpPrincipal = new HttpPrincipal(
229                            url, user.getEmailAddress(), user.getPassword(),
230                            user.getPasswordEncrypted());
231    
232                    // Ping remote host and verify that the group exists
233    
234                    try {
235                            GroupServiceHttp.getGroup(httpPrincipal, remoteGroupId);
236                    }
237                    catch (NoSuchGroupException nsge) {
238                            RemoteExportException ree = new RemoteExportException(
239                                    RemoteExportException.NO_GROUP);
240    
241                            ree.setGroupId(remoteGroupId);
242    
243                            throw ree;
244                    }
245                    catch (SystemException se) {
246                            RemoteExportException ree = new RemoteExportException(
247                                    RemoteExportException.BAD_CONNECTION);
248    
249                            ree.setURL(url);
250    
251                            throw ree;
252                    }
253    
254                    byte[] bytes = null;
255    
256                    if (layoutIdMap == null) {
257                            bytes = LayoutLocalServiceUtil.exportLayouts(
258                                    sourceGroupId, privateLayout, parameterMap, startDate, endDate);
259                    }
260                    else {
261                            List<Layout> layouts = new ArrayList<Layout>();
262    
263                            Iterator<Map.Entry<Long, Boolean>> itr1 =
264                                    layoutIdMap.entrySet().iterator();
265    
266                            while (itr1.hasNext()) {
267                                    Entry<Long, Boolean> entry = itr1.next();
268    
269                                    long plid = GetterUtil.getLong(String.valueOf(entry.getKey()));
270                                    boolean includeChildren = entry.getValue();
271    
272                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
273    
274                                    if (!layouts.contains(layout)) {
275                                            layouts.add(layout);
276                                    }
277    
278                                    Iterator<Layout> itr2 = getMissingParentLayouts(
279                                            layout, sourceGroupId).iterator();
280    
281                                    while (itr2.hasNext()) {
282                                            Layout parentLayout = itr2.next();
283    
284                                            if (!layouts.contains(parentLayout)) {
285                                                    layouts.add(parentLayout);
286                                            }
287                                    }
288    
289                                    if (includeChildren) {
290                                            itr2 = layout.getAllChildren().iterator();
291    
292                                            while (itr2.hasNext()) {
293                                                    Layout childLayout = itr2.next();
294    
295                                                    if (!layouts.contains(childLayout)) {
296                                                            layouts.add(childLayout);
297                                                    }
298                                            }
299                                    }
300                            }
301    
302                            long[] layoutIds = new long[layouts.size()];
303    
304                            for (int i = 0; i < layouts.size(); i++) {
305                                    Layout curLayout = layouts.get(i);
306    
307                                    layoutIds[i] = curLayout.getLayoutId();
308                            }
309    
310                            if (layoutIds.length <= 0) {
311                                    throw new RemoteExportException(
312                                            RemoteExportException.NO_LAYOUTS);
313                            }
314    
315                            bytes = LayoutLocalServiceUtil.exportLayouts(
316                                    sourceGroupId, privateLayout, layoutIds, parameterMap,
317                                    startDate, endDate);
318                    }
319    
320                    LayoutServiceHttp.importLayouts(
321                            httpPrincipal, remoteGroupId, remotePrivateLayout, parameterMap,
322                            bytes);
323            }
324    
325            public void deleteLastImportSettings(Group liveGroup, boolean privateLayout)
326                    throws Exception {
327    
328                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
329                            liveGroup.getGroupId(), privateLayout);
330    
331                    for (Layout layout : layouts) {
332                            UnicodeProperties typeSettingsProperties =
333                                    layout.getTypeSettingsProperties();
334    
335                            Set<String> keys = new HashSet<String>();
336    
337                            for (String key : typeSettingsProperties.keySet()) {
338                                    if (key.startsWith("last-import-")) {
339                                            keys.add(key);
340                                    }
341                            }
342    
343                            if (keys.isEmpty()) {
344                                    continue;
345                            }
346    
347                            for (String key : keys) {
348                                    typeSettingsProperties.remove(key);
349                            }
350    
351                            LayoutLocalServiceUtil.updateLayout(
352                                    layout.getGroupId(), layout.getPrivateLayout(),
353                                    layout.getLayoutId(), typeSettingsProperties.toString());
354                    }
355            }
356    
357            public void deleteRecentLayoutRevisionId(
358                            HttpServletRequest request, long layoutSetBranchId, long plid)
359                    throws SystemException {
360    
361                    PortalPreferences portalPreferences =
362                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
363    
364                    deleteRecentLayoutRevisionId(
365                            portalPreferences, layoutSetBranchId, plid);
366            }
367    
368            public void deleteRecentLayoutRevisionId(
369                            User user, long layoutSetBranchId, long plid)
370                    throws SystemException {
371    
372                    PortalPreferences portalPreferences = getPortalPreferences(user);
373    
374                    deleteRecentLayoutRevisionId(
375                            portalPreferences, layoutSetBranchId, plid);
376            }
377    
378            public void disableStaging(
379                            Group scopeGroup, Group liveGroup, ServiceContext serviceContext)
380                    throws Exception {
381    
382                    disableStaging(null, scopeGroup, liveGroup, serviceContext);
383            }
384    
385            public void disableStaging(
386                            PortletRequest portletRequest, Group scopeGroup, Group liveGroup,
387                            ServiceContext serviceContext)
388                    throws Exception {
389    
390                    UnicodeProperties typeSettingsProperties =
391                            liveGroup.getTypeSettingsProperties();
392    
393                    typeSettingsProperties.remove("branchingPrivate");
394                    typeSettingsProperties.remove("branchingPublic");
395                    typeSettingsProperties.remove("remoteAddress");
396                    typeSettingsProperties.remove("remoteGroupId");
397                    typeSettingsProperties.remove("remotePort");
398                    typeSettingsProperties.remove("secureConnection");
399                    typeSettingsProperties.remove("staged");
400                    typeSettingsProperties.remove("stagedRemotely");
401    
402                    Set<String> keys = new HashSet<String>();
403    
404                    for (String key : typeSettingsProperties.keySet()) {
405                            if (key.startsWith(StagingConstants.STAGED_PORTLET)) {
406                                    keys.add(key);
407                            }
408                    }
409    
410                    for (String key : keys) {
411                            typeSettingsProperties.remove(key);
412                    }
413    
414                    deleteLastImportSettings(liveGroup, true);
415                    deleteLastImportSettings(liveGroup, false);
416    
417                    if (liveGroup.hasStagingGroup()) {
418                            Group stagingGroup = liveGroup.getStagingGroup();
419    
420                            LayoutSetBranchLocalServiceUtil.deleteLayoutSetBranches(
421                                    stagingGroup.getGroupId(), true, true);
422                            LayoutSetBranchLocalServiceUtil.deleteLayoutSetBranches(
423                                    stagingGroup.getGroupId(), false, true);
424    
425                            GroupLocalServiceUtil.deleteGroup(stagingGroup.getGroupId());
426                    }
427                    else {
428                            LayoutSetBranchLocalServiceUtil.deleteLayoutSetBranches(
429                                    liveGroup.getGroupId(), true, true);
430                            LayoutSetBranchLocalServiceUtil.deleteLayoutSetBranches(
431                                    liveGroup.getGroupId(), false, true);
432                    }
433    
434                    GroupLocalServiceUtil.updateGroup(
435                            liveGroup.getGroupId(), typeSettingsProperties.toString());
436            }
437    
438            public void enableLocalStaging(
439                            long userId, Group scopeGroup, Group liveGroup,
440                            boolean branchingPublic, boolean branchingPrivate,
441                            ServiceContext serviceContext)
442                    throws Exception {
443    
444                    if (liveGroup.isStagedRemotely()) {
445                            disableStaging(scopeGroup, liveGroup, serviceContext);
446                    }
447    
448                    UnicodeProperties typeSettingsProperties =
449                            liveGroup.getTypeSettingsProperties();
450    
451                    typeSettingsProperties.setProperty(
452                            "branchingPrivate", String.valueOf(branchingPrivate));
453                    typeSettingsProperties.setProperty(
454                            "branchingPublic", String.valueOf(branchingPublic));
455                    typeSettingsProperties.setProperty("staged", Boolean.TRUE.toString());
456                    typeSettingsProperties.setProperty(
457                            "stagedRemotely", String.valueOf(false));
458    
459                    setCommonStagingOptions(
460                            liveGroup, typeSettingsProperties, serviceContext);
461    
462                    if (!liveGroup.hasStagingGroup()) {
463                            serviceContext.setAttribute("staging", String.valueOf(true));
464    
465                            Group stagingGroup = GroupLocalServiceUtil.addGroup(
466                                    userId, liveGroup.getClassName(), liveGroup.getClassPK(),
467                                    liveGroup.getGroupId(), liveGroup.getDescriptiveName(),
468                                    liveGroup.getDescription(), liveGroup.getType(),
469                                    liveGroup.getFriendlyURL(), false, liveGroup.isActive(),
470                                    serviceContext);
471    
472                            GroupLocalServiceUtil.updateGroup(
473                                    liveGroup.getGroupId(), typeSettingsProperties.toString());
474    
475                            if (liveGroup.hasPrivateLayouts()) {
476                                    Map<String, String[]> parameterMap = getStagingParameters();
477    
478                                    publishLayouts(
479                                            userId, liveGroup.getGroupId(), stagingGroup.getGroupId(),
480                                            true, parameterMap, null, null);
481                            }
482    
483                            if (liveGroup.hasPublicLayouts()) {
484                                    Map<String, String[]> parameterMap = getStagingParameters();
485    
486                                    publishLayouts(
487                                            userId, liveGroup.getGroupId(), stagingGroup.getGroupId(),
488                                            false, parameterMap, null, null);
489                            }
490    
491                            checkDefaultLayoutSetBranches(
492                                    userId, liveGroup, branchingPublic, branchingPrivate, false,
493                                    serviceContext);
494                    }
495                    else {
496                            GroupLocalServiceUtil.updateGroup(
497                                    liveGroup.getGroupId(), typeSettingsProperties.toString());
498    
499                            checkDefaultLayoutSetBranches(
500                                    userId, liveGroup, branchingPublic, branchingPrivate, false,
501                                    serviceContext);
502    
503                            if (!branchingPublic) {
504                                    LayoutSetBranchLocalServiceUtil.deleteLayoutSetBranches(
505                                            liveGroup.getStagingGroup().getGroupId(), false, true);
506                            }
507    
508                            if (!branchingPrivate) {
509                                    LayoutSetBranchLocalServiceUtil.deleteLayoutSetBranches(
510                                            liveGroup.getStagingGroup().getGroupId(), true, true);
511                            }
512                    }
513            }
514    
515            public void enableRemoteStaging(
516                            long userId, Group scopeGroup, Group liveGroup,
517                            boolean branchingPublic, boolean branchingPrivate,
518                            String remoteAddress, long remoteGroupId, int remotePort,
519                            boolean secureConnection, ServiceContext serviceContext)
520                    throws Exception {
521    
522                    validate(remoteAddress, remoteGroupId, remotePort, secureConnection);
523    
524                    if (liveGroup.hasStagingGroup()) {
525                            disableStaging(scopeGroup, liveGroup, serviceContext);
526                    }
527    
528                    UnicodeProperties typeSettingsProperties =
529                            liveGroup.getTypeSettingsProperties();
530    
531                    typeSettingsProperties.setProperty(
532                            "branchingPrivate", String.valueOf(branchingPrivate));
533                    typeSettingsProperties.setProperty(
534                            "branchingPublic", String.valueOf(branchingPublic));
535                    typeSettingsProperties.setProperty("remoteAddress", remoteAddress);
536                    typeSettingsProperties.setProperty(
537                            "remoteGroupId", String.valueOf(remoteGroupId));
538                    typeSettingsProperties.setProperty(
539                            "remotePort", String.valueOf(remotePort));
540                    typeSettingsProperties.setProperty(
541                            "secureConnection", String.valueOf(secureConnection));
542                    typeSettingsProperties.setProperty("staged", Boolean.TRUE.toString());
543                    typeSettingsProperties.setProperty(
544                            "stagedRemotely", Boolean.TRUE.toString());
545    
546                    setCommonStagingOptions(
547                            liveGroup, typeSettingsProperties, serviceContext);
548    
549                    GroupLocalServiceUtil.updateGroup(
550                            liveGroup.getGroupId(), typeSettingsProperties.toString());
551    
552                    checkDefaultLayoutSetBranches(
553                            userId, liveGroup, branchingPublic, branchingPrivate, true,
554                            serviceContext);
555            }
556    
557            public Group getLiveGroup(long groupId)
558                    throws PortalException, SystemException {
559    
560                    if (groupId == 0) {
561                            return null;
562                    }
563    
564                    Group group = GroupLocalServiceUtil.getGroup(groupId);
565    
566                    if (group.isLayout()) {
567                            group = group.getParentGroup();
568                    }
569    
570                    if (group.isStagingGroup()) {
571                            return group.getLiveGroup();
572                    }
573                    else {
574                            return group;
575                    }
576            }
577    
578            public long getLiveGroupId(long groupId)
579                    throws PortalException, SystemException {
580    
581                    if (groupId == 0) {
582                            return groupId;
583                    }
584    
585                    Group group = getLiveGroup(groupId);
586    
587                    return group.getGroupId();
588            }
589    
590            public List<Layout> getMissingParentLayouts(Layout layout, long liveGroupId)
591                    throws Exception {
592    
593                    List<Layout> missingParentLayouts = new ArrayList<Layout>();
594    
595                    long parentLayoutId = layout.getParentLayoutId();
596    
597                    Layout parentLayout = null;
598    
599                    while (parentLayoutId > 0) {
600                            parentLayout = LayoutLocalServiceUtil.getLayout(
601                                    layout.getGroupId(), layout.isPrivateLayout(), parentLayoutId);
602    
603                            try {
604                                    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
605                                            parentLayout.getUuid(), liveGroupId);
606    
607                                    // If one parent is found all others are assumed to exist
608    
609                                    break;
610                            }
611                            catch (NoSuchLayoutException nsle) {
612                                    missingParentLayouts.add(parentLayout);
613    
614                                    parentLayoutId = parentLayout.getParentLayoutId();
615                            }
616                    }
617    
618                    return missingParentLayouts;
619            }
620    
621            public long getRecentLayoutRevisionId(
622                            HttpServletRequest request, long layoutSetBranchId, long plid)
623                    throws PortalException, SystemException {
624    
625                    PortalPreferences portalPreferences =
626                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
627    
628                    return getRecentLayoutRevisionId(
629                            portalPreferences, layoutSetBranchId, plid);
630            }
631    
632            public long getRecentLayoutRevisionId(
633                            User user, long layoutSetBranchId, long plid)
634                    throws PortalException, SystemException {
635    
636                    PortalPreferences portalPreferences = getPortalPreferences(user);
637    
638                    return getRecentLayoutRevisionId(
639                            portalPreferences, layoutSetBranchId, plid);
640            }
641    
642            public long getRecentLayoutSetBranchId(
643                    HttpServletRequest request, long layoutSetId) {
644    
645                    return GetterUtil.getLong(
646                            SessionClicks.get(
647                                    request, Staging.class.getName(),
648                                    getRecentLayoutSetBranchIdKey(layoutSetId)));
649            }
650    
651            public long getRecentLayoutSetBranchId(User user, long layoutSetId)
652                    throws SystemException {
653    
654                    PortalPreferences portalPreferences = getPortalPreferences(user);
655    
656                    return GetterUtil.getLong(
657                            portalPreferences.getValue(
658                                    Staging.class.getName(),
659                                    getRecentLayoutSetBranchIdKey(layoutSetId)));
660            }
661    
662            public String getSchedulerGroupName(String destinationName, long groupId) {
663                    return destinationName.concat(StringPool.SLASH).concat(
664                            String.valueOf(groupId));
665            }
666    
667            public Map<String, String[]> getStagingParameters() {
668                    Map<String, String[]> parameterMap =
669                            new LinkedHashMap<String, String[]>();
670    
671                    parameterMap.put(
672                            PortletDataHandlerKeys.CATEGORIES,
673                            new String[] {Boolean.TRUE.toString()});
674                    parameterMap.put(
675                            PortletDataHandlerKeys.DATA_STRATEGY,
676                            new String[] {
677                                    PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE});
678                    parameterMap.put(
679                            PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
680                            new String[] {Boolean.TRUE.toString()});
681                    parameterMap.put(
682                            PortletDataHandlerKeys.DELETE_PORTLET_DATA,
683                            new String[] {Boolean.FALSE.toString()});
684                    parameterMap.put(
685                            PortletDataHandlerKeys.IGNORE_LAST_PUBLISH_DATE,
686                            new String[] {Boolean.TRUE.toString()});
687                    parameterMap.put(
688                            PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
689                            new String[] {Boolean.FALSE.toString()});
690                    parameterMap.put(
691                            PortletDataHandlerKeys.LOGO,
692                            new String[] {Boolean.FALSE.toString()});
693                    parameterMap.put(
694                            PortletDataHandlerKeys.PERMISSIONS,
695                            new String[] {Boolean.TRUE.toString()});
696                    parameterMap.put(
697                            PortletDataHandlerKeys.PORTLET_DATA,
698                            new String[] {Boolean.TRUE.toString()});
699                    parameterMap.put(
700                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
701                            new String[] {Boolean.TRUE.toString()});
702                    parameterMap.put(
703                            PortletDataHandlerKeys.PORTLET_SETUP,
704                            new String[] {Boolean.TRUE.toString()});
705                    parameterMap.put(
706                            PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
707                            new String[] {Boolean.TRUE.toString()});
708                    parameterMap.put(
709                            PortletDataHandlerKeys.THEME,
710                            new String[] {Boolean.FALSE.toString()});
711                    parameterMap.put(
712                            PortletDataHandlerKeys.THEME_REFERENCE,
713                            new String[] {Boolean.TRUE.toString()});
714                    parameterMap.put(
715                            PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
716                            new String[] {Boolean.TRUE.toString()});
717                    parameterMap.put(
718                            PortletDataHandlerKeys.USER_ID_STRATEGY,
719                            new String[] {UserIdStrategy.CURRENT_USER_ID});
720                    parameterMap.put(
721                            PortletDataHandlerKeys.USER_PERMISSIONS,
722                            new String[] {Boolean.FALSE.toString()});
723    
724                    return parameterMap;
725            }
726    
727            public Map<String, String[]> getStagingParameters(
728                    PortletRequest portletRequest) {
729    
730                    Map<String, String[]> parameterMap =
731                            new LinkedHashMap<String, String[]>(
732                                    portletRequest.getParameterMap());
733    
734                    if (!parameterMap.containsKey(PortletDataHandlerKeys.DATA_STRATEGY)) {
735                            parameterMap.put(
736                                    PortletDataHandlerKeys.DATA_STRATEGY,
737                                    new String[] {
738                                            PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE});
739                    }
740    
741                    /*if (!parameterMap.containsKey(
742                                    PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS)) {
743    
744                            parameterMap.put(
745                                    PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
746                                    new String[] {Boolean.TRUE.toString()});
747                    }*/
748    
749                    if (!parameterMap.containsKey(
750                                    PortletDataHandlerKeys.DELETE_PORTLET_DATA)) {
751    
752                            parameterMap.put(
753                                    PortletDataHandlerKeys.DELETE_PORTLET_DATA,
754                                    new String[] {Boolean.FALSE.toString()});
755                    }
756    
757                    if (!parameterMap.containsKey(
758                                    PortletDataHandlerKeys.LAYOUT_SET_SETTINGS)) {
759    
760                            parameterMap.put(
761                                    PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
762                                    new String[] {Boolean.FALSE.toString()});
763                    }
764    
765                    if (!parameterMap.containsKey(PortletDataHandlerKeys.LOGO)) {
766                            parameterMap.put(
767                                    PortletDataHandlerKeys.LOGO,
768                                    new String[] {Boolean.FALSE.toString()});
769                    }
770    
771                    if (!parameterMap.containsKey(PortletDataHandlerKeys.PORTLET_DATA)) {
772                            parameterMap.put(
773                                    PortletDataHandlerKeys.PORTLET_DATA,
774                                    new String[] {Boolean.FALSE.toString()});
775                    }
776    
777                    if (!parameterMap.containsKey(
778                                    PortletDataHandlerKeys.PORTLET_DATA_ALL)) {
779    
780                            parameterMap.put(
781                                    PortletDataHandlerKeys.PORTLET_DATA_ALL,
782                                    new String[] {Boolean.FALSE.toString()});
783                    }
784    
785                    if (!parameterMap.containsKey(PortletDataHandlerKeys.PORTLET_SETUP)) {
786                            parameterMap.put(
787                                    PortletDataHandlerKeys.PORTLET_SETUP,
788                                    new String[] {Boolean.TRUE.toString()});
789                    }
790    
791                    if (!parameterMap.containsKey(
792                                    PortletDataHandlerKeys.PORTLET_USER_PREFERENCES)) {
793    
794                            parameterMap.put(
795                                    PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
796                                    new String[] {Boolean.TRUE.toString()});
797                    }
798    
799                    if (!parameterMap.containsKey(PortletDataHandlerKeys.THEME)) {
800                            parameterMap.put(
801                                    PortletDataHandlerKeys.THEME,
802                                    new String[] {Boolean.FALSE.toString()});
803                    }
804    
805                    if (!parameterMap.containsKey(PortletDataHandlerKeys.THEME_REFERENCE)) {
806                            parameterMap.put(
807                                    PortletDataHandlerKeys.THEME_REFERENCE,
808                                    new String[] {Boolean.FALSE.toString()});
809                    }
810    
811                    if (!parameterMap.containsKey(
812                                    PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE)) {
813    
814                            parameterMap.put(
815                                    PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
816                                    new String[] {Boolean.TRUE.toString()});
817                    }
818    
819                    if (!parameterMap.containsKey(
820                                    PortletDataHandlerKeys.USER_ID_STRATEGY)) {
821    
822                            parameterMap.put(
823                                    PortletDataHandlerKeys.USER_ID_STRATEGY,
824                                    new String[] {UserIdStrategy.CURRENT_USER_ID});
825                    }
826    
827                    return parameterMap;
828            }
829    
830            public boolean isIncomplete(Layout layout, long layoutSetBranchId) {
831                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
832                            layout);
833    
834                    if (layoutRevision == null) {
835                            try {
836                                    layoutRevision =
837                                            LayoutRevisionLocalServiceUtil.getLayoutRevision(
838                                                    layoutSetBranchId, layout.getPlid(), true);
839    
840                                    return false;
841                            }
842                            catch (Exception e) {
843                            }
844                    }
845    
846                    try {
847                            layoutRevision = LayoutRevisionLocalServiceUtil.getLayoutRevision(
848                                    layoutSetBranchId, layout.getPlid(), false);
849                    }
850                    catch (Exception e) {
851                    }
852    
853                    if (layoutRevision == null ||
854                            (layoutRevision.getStatus() ==
855                                    WorkflowConstants.STATUS_INCOMPLETE)) {
856    
857                            return true;
858                    }
859    
860                    return false;
861            }
862    
863            public void publishLayout(
864                            long userId, long plid, long liveGroupId, boolean includeChildren)
865                    throws Exception {
866    
867                    Map<String, String[]> parameterMap = getStagingParameters();
868    
869                    parameterMap.put(
870                            PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
871                            new String[] {Boolean.FALSE.toString()});
872    
873                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
874    
875                    List<Layout> layouts = new ArrayList<Layout>();
876    
877                    layouts.add(layout);
878    
879                    layouts.addAll(getMissingParentLayouts(layout, liveGroupId));
880    
881                    if (includeChildren) {
882                            layouts.addAll(layout.getAllChildren());
883                    }
884    
885                    Iterator<Layout> itr = layouts.iterator();
886    
887                    long[] layoutIds = new long[layouts.size()];
888    
889                    for (int i = 0; itr.hasNext(); i++) {
890                            Layout curLayout = itr.next();
891    
892                            layoutIds[i] = curLayout.getLayoutId();
893                    }
894    
895                    publishLayouts(
896                            userId, layout.getGroupId(), liveGroupId, layout.isPrivateLayout(),
897                            layoutIds, parameterMap, null, null);
898            }
899    
900            public void publishLayouts(
901                            long userId, long sourceGroupId, long targetGroupId,
902                            boolean privateLayout, long[] layoutIds,
903                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
904                    throws Exception {
905    
906                    parameterMap.put(
907                            PortletDataHandlerKeys.PERFORM_DIRECT_BINARY_IMPORT,
908                            new String[] {Boolean.TRUE.toString()});
909    
910                    File file = LayoutLocalServiceUtil.exportLayoutsAsFile(
911                            sourceGroupId, privateLayout, layoutIds, parameterMap, startDate,
912                            endDate);
913    
914                    try {
915                            LayoutLocalServiceUtil.importLayouts(
916                                    userId, targetGroupId, privateLayout, parameterMap, file);
917                    }
918                    finally {
919                            file.delete();
920                    }
921            }
922    
923            public void publishLayouts(
924                            long userId, long sourceGroupId, long targetGroupId,
925                            boolean privateLayout, Map<Long, Boolean> layoutIdMap,
926                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
927                    throws Exception {
928    
929                    List<Layout> layouts = new ArrayList<Layout>();
930    
931                    Iterator<Map.Entry<Long, Boolean>> itr1 =
932                            layoutIdMap.entrySet().iterator();
933    
934                    while (itr1.hasNext()) {
935                            Entry<Long, Boolean> entry = itr1.next();
936    
937                            long plid = GetterUtil.getLong(String.valueOf(entry.getKey()));
938                            boolean includeChildren = entry.getValue();
939    
940                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
941    
942                            if (!layouts.contains(layout)) {
943                                    layouts.add(layout);
944                            }
945    
946                            Iterator<Layout> itr2 = getMissingParentLayouts(
947                                    layout, targetGroupId).iterator();
948    
949                            while (itr2.hasNext()) {
950                                    Layout parentLayout = itr2.next();
951    
952                                    if (!layouts.contains(parentLayout)) {
953                                            layouts.add(parentLayout);
954                                    }
955                            }
956    
957                            if (includeChildren) {
958                                    itr2 = layout.getAllChildren().iterator();
959    
960                                    while (itr2.hasNext()) {
961                                            Layout childLayout = itr2.next();
962    
963                                            if (!layouts.contains(childLayout)) {
964                                                    layouts.add(childLayout);
965                                            }
966                                    }
967                            }
968                    }
969    
970                    long[] layoutIds = new long[layouts.size()];
971    
972                    for (int i = 0; i < layouts.size(); i++) {
973                            Layout curLayout = layouts.get(i);
974    
975                            layoutIds[i] = curLayout.getLayoutId();
976                    }
977    
978                    publishLayouts(
979                            userId, sourceGroupId, targetGroupId, privateLayout, layoutIds,
980                            parameterMap, startDate, endDate);
981            }
982    
983            public void publishLayouts(
984                            long userId, long sourceGroupId, long targetGroupId,
985                            boolean privateLayout, Map<String, String[]> parameterMap,
986                            Date startDate, Date endDate)
987                    throws Exception {
988    
989                    publishLayouts(
990                            userId, sourceGroupId, targetGroupId, privateLayout, (long[])null,
991                            parameterMap, startDate, endDate);
992            }
993    
994            public void publishToLive(PortletRequest portletRequest) throws Exception {
995                    long groupId = ParamUtil.getLong(portletRequest, "groupId");
996    
997                    Group liveGroup = GroupLocalServiceUtil.getGroup(groupId);
998    
999                    Map<String, String[]> parameterMap = getStagingParameters(
1000                            portletRequest);
1001    
1002                    if (liveGroup.isStaged()) {
1003                            if (liveGroup.isStagedRemotely()) {
1004                                    publishToRemote(portletRequest);
1005                            }
1006                            else {
1007                                    Group stagingGroup = liveGroup.getStagingGroup();
1008    
1009                                    publishLayouts(
1010                                            portletRequest, stagingGroup.getGroupId(), groupId,
1011                                            parameterMap, false);
1012                            }
1013                    }
1014            }
1015    
1016            public void publishToLive(PortletRequest portletRequest, Portlet portlet)
1017                    throws Exception {
1018    
1019                    long plid = ParamUtil.getLong(portletRequest, "plid");
1020    
1021                    Layout sourceLayout = LayoutLocalServiceUtil.getLayout(plid);
1022    
1023                    Group stagingGroup = null;
1024                    Group liveGroup = null;
1025    
1026                    Layout targetLayout = null;
1027    
1028                    long scopeGroupId = PortalUtil.getScopeGroupId(portletRequest);
1029    
1030                    if (sourceLayout.hasScopeGroup() &&
1031                            (sourceLayout.getScopeGroup().getGroupId() == scopeGroupId)) {
1032    
1033                            stagingGroup = sourceLayout.getScopeGroup();
1034                            liveGroup = stagingGroup.getLiveGroup();
1035    
1036                            targetLayout = LayoutLocalServiceUtil.getLayout(
1037                                    liveGroup.getClassPK());
1038                    }
1039                    else {
1040                            stagingGroup = sourceLayout.getGroup();
1041                            liveGroup = stagingGroup.getLiveGroup();
1042    
1043                            targetLayout = LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
1044                                    sourceLayout.getUuid(), liveGroup.getGroupId());
1045                    }
1046    
1047                    copyPortlet(
1048                            portletRequest, stagingGroup.getGroupId(), liveGroup.getGroupId(),
1049                            sourceLayout.getPlid(), targetLayout.getPlid(),
1050                            portlet.getPortletId());
1051            }
1052    
1053            public void publishToRemote(PortletRequest portletRequest)
1054                    throws Exception {
1055    
1056                    publishToRemote(portletRequest, false);
1057            }
1058    
1059            public void scheduleCopyFromLive(PortletRequest portletRequest)
1060                    throws Exception {
1061    
1062                    long stagingGroupId = ParamUtil.getLong(
1063                            portletRequest, "stagingGroupId");
1064    
1065                    Group stagingGroup = GroupLocalServiceUtil.getGroup(stagingGroupId);
1066    
1067                    long liveGroupId = stagingGroup.getLiveGroupId();
1068    
1069                    Map<String, String[]> parameterMap = getStagingParameters(
1070                            portletRequest);
1071    
1072                    publishLayouts(
1073                            portletRequest, liveGroupId, stagingGroupId, parameterMap, true);
1074            }
1075    
1076            public void schedulePublishToLive(PortletRequest portletRequest)
1077                    throws Exception {
1078    
1079                    long stagingGroupId = ParamUtil.getLong(
1080                            portletRequest, "stagingGroupId");
1081    
1082                    Group stagingGroup = GroupLocalServiceUtil.getGroup(stagingGroupId);
1083    
1084                    long liveGroupId = stagingGroup.getLiveGroupId();
1085    
1086                    Map<String, String[]> parameterMap = getStagingParameters(
1087                            portletRequest);
1088    
1089                    publishLayouts(
1090                            portletRequest, stagingGroupId, liveGroupId, parameterMap, true);
1091            }
1092    
1093            public void schedulePublishToRemote(PortletRequest portletRequest)
1094                    throws Exception {
1095    
1096                    publishToRemote(portletRequest, true);
1097            }
1098    
1099            public void setRecentLayoutBranchId(
1100                    HttpServletRequest request, long layoutSetBranchId, long plid,
1101                    long layoutBranchId)
1102                    throws SystemException {
1103    
1104                    PortalPreferences portalPreferences =
1105                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
1106    
1107                    setRecentLayoutBranchId(
1108                            portalPreferences, layoutSetBranchId, plid, layoutBranchId);
1109            }
1110    
1111            public void setRecentLayoutBranchId(
1112                    User user, long layoutSetBranchId, long plid, long layoutBranchId)
1113                    throws SystemException {
1114    
1115                    PortalPreferences portalPreferences = getPortalPreferences(user);
1116    
1117                    setRecentLayoutBranchId(
1118                            portalPreferences, layoutSetBranchId, plid, layoutBranchId);
1119            }
1120    
1121            public void setRecentLayoutRevisionId(
1122                            HttpServletRequest request, long layoutSetBranchId, long plid,
1123                            long layoutRevisionId)
1124                    throws SystemException {
1125    
1126                    PortalPreferences portalPreferences =
1127                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
1128    
1129                    setRecentLayoutRevisionId(
1130                            portalPreferences, layoutSetBranchId, plid, layoutRevisionId);
1131            }
1132    
1133            public void setRecentLayoutRevisionId(
1134                            User user, long layoutSetBranchId, long plid, long layoutRevisionId)
1135                    throws SystemException {
1136    
1137                    PortalPreferences portalPreferences = getPortalPreferences(user);
1138    
1139                    setRecentLayoutRevisionId(
1140                            portalPreferences, layoutSetBranchId, plid, layoutRevisionId);
1141            }
1142    
1143            public void setRecentLayoutSetBranchId(
1144                    HttpServletRequest request, long layoutSetId, long layoutSetBranchId) {
1145    
1146                    SessionClicks.put(
1147                            request, Staging.class.getName(),
1148                            getRecentLayoutSetBranchIdKey(layoutSetId),
1149                            String.valueOf(layoutSetBranchId));
1150            }
1151    
1152            public void setRecentLayoutSetBranchId(
1153                            User user, long layoutSetId, long layoutSetBranchId)
1154                    throws SystemException {
1155    
1156                    PortalPreferences portalPreferences = getPortalPreferences(user);
1157    
1158                    portalPreferences.setValue(
1159                            Staging.class.getName(), getRecentLayoutSetBranchIdKey(layoutSetId),
1160                            String.valueOf(layoutSetBranchId));
1161            }
1162    
1163            public void unscheduleCopyFromLive(PortletRequest portletRequest)
1164                    throws Exception {
1165    
1166                    long stagingGroupId = ParamUtil.getLong(
1167                            portletRequest, "stagingGroupId");
1168    
1169                    String jobName = ParamUtil.getString(portletRequest, "jobName");
1170                    String groupName = getSchedulerGroupName(
1171                            DestinationNames.LAYOUTS_LOCAL_PUBLISHER, stagingGroupId);
1172    
1173                    LayoutServiceUtil.unschedulePublishToLive(
1174                            stagingGroupId, jobName, groupName);
1175            }
1176    
1177            public void unschedulePublishToLive(PortletRequest portletRequest)
1178                    throws Exception {
1179    
1180                    long stagingGroupId = ParamUtil.getLong(
1181                            portletRequest, "stagingGroupId");
1182    
1183                    Group stagingGroup = GroupLocalServiceUtil.getGroup(stagingGroupId);
1184    
1185                    long liveGroupId = stagingGroup.getLiveGroupId();
1186    
1187                    String jobName = ParamUtil.getString(portletRequest, "jobName");
1188                    String groupName = getSchedulerGroupName(
1189                            DestinationNames.LAYOUTS_LOCAL_PUBLISHER, liveGroupId);
1190    
1191                    LayoutServiceUtil.unschedulePublishToLive(
1192                            liveGroupId, jobName, groupName);
1193            }
1194    
1195            public void unschedulePublishToRemote(PortletRequest portletRequest)
1196                    throws Exception {
1197    
1198                    long groupId = ParamUtil.getLong(portletRequest, "groupId");
1199    
1200                    String jobName = ParamUtil.getString(portletRequest, "jobName");
1201                    String groupName = getSchedulerGroupName(
1202                            DestinationNames.LAYOUTS_REMOTE_PUBLISHER, groupId);
1203    
1204                    LayoutServiceUtil.unschedulePublishToRemote(
1205                            groupId, jobName, groupName);
1206            }
1207    
1208            public void updateLastImportSettings(
1209                            Element layoutElement, Layout layout,
1210                            PortletDataContext portletDataContext)
1211                    throws Exception {
1212    
1213                    Map<String, String[]> parameterMap =
1214                            portletDataContext.getParameterMap();
1215    
1216                    String cmd = MapUtil.getString(parameterMap, "cmd");
1217    
1218                    if (!cmd.equals("publish_to_live")) {
1219                            return;
1220                    }
1221    
1222                    UnicodeProperties typeSettingsProperties =
1223                            layout.getTypeSettingsProperties();
1224    
1225                    typeSettingsProperties.setProperty(
1226                            "last-import-date", String.valueOf(System.currentTimeMillis()));
1227    
1228                    String layoutRevisionId = GetterUtil.getString(
1229                            layoutElement.attributeValue("layout-revision-id"));
1230    
1231                    typeSettingsProperties.setProperty(
1232                            "last-import-layout-revision-id", layoutRevisionId);
1233    
1234                    String layoutSetBranchId = MapUtil.getString(
1235                            parameterMap, "layoutSetBranchId");
1236    
1237                    typeSettingsProperties.setProperty(
1238                            "last-import-layout-set-branch-id", layoutSetBranchId);
1239    
1240                    String layoutSetBranchName = MapUtil.getString(
1241                            parameterMap, "layoutSetBranchName");
1242    
1243                    typeSettingsProperties.setProperty(
1244                            "last-import-layout-set-branch-name", layoutSetBranchName);
1245    
1246                    String lastImportUserName = MapUtil.getString(
1247                            parameterMap, "lastImportUserName");
1248    
1249                    typeSettingsProperties.setProperty(
1250                            "last-import-user-name", lastImportUserName);
1251    
1252                    String lastImportUserUuid = MapUtil.getString(
1253                            parameterMap, "lastImportUserUuid");
1254    
1255                    typeSettingsProperties.setProperty(
1256                            "last-import-user-uuid", lastImportUserUuid);
1257    
1258                    String layoutBranchId = GetterUtil.getString(
1259                            layoutElement.attributeValue("layout-branch-id"));
1260    
1261                    typeSettingsProperties.setProperty(
1262                            "last-import-layout-branch-id", layoutBranchId);
1263    
1264                    String layoutBranchName = GetterUtil.getString(
1265                            layoutElement.attributeValue("layout-branch-name"));
1266    
1267                    typeSettingsProperties.setProperty(
1268                            "last-import-layout-branch-name", layoutBranchName);
1269    
1270                    layout.setTypeSettingsProperties(typeSettingsProperties);
1271            }
1272    
1273            public void updateStaging(PortletRequest portletRequest, Group liveGroup)
1274                    throws Exception {
1275    
1276                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
1277                            WebKeys.THEME_DISPLAY);
1278    
1279                    PermissionChecker permissionChecker =
1280                            themeDisplay.getPermissionChecker();
1281    
1282                    long userId = permissionChecker.getUserId();
1283    
1284                    Group scopeGroup = themeDisplay.getScopeGroup();
1285    
1286                    if (!GroupPermissionUtil.contains(
1287                                    permissionChecker, liveGroup.getGroupId(),
1288                                    ActionKeys.MANAGE_STAGING)) {
1289    
1290                            return;
1291                    }
1292    
1293                    int stagingType = ParamUtil.getInteger(portletRequest, "stagingType");
1294    
1295                    boolean branchingPublic = ParamUtil.getBoolean(
1296                            portletRequest, "branchingPublic");
1297                    boolean branchingPrivate = ParamUtil.getBoolean(
1298                            portletRequest, "branchingPrivate");
1299    
1300                    ServiceContext serviceContext =
1301                            ServiceContextThreadLocal.getServiceContext();
1302    
1303                    if (stagingType == StagingConstants.TYPE_NOT_STAGED) {
1304                            if (liveGroup.hasStagingGroup() || liveGroup.isStagedRemotely()) {
1305                                    disableStaging(
1306                                            portletRequest, scopeGroup, liveGroup, serviceContext);
1307                            }
1308                    }
1309                    else if (stagingType == StagingConstants.TYPE_LOCAL_STAGING) {
1310                            enableLocalStaging(
1311                                    userId, scopeGroup, liveGroup, branchingPublic,
1312                                    branchingPrivate, serviceContext);
1313                    }
1314                    else if (stagingType == StagingConstants.TYPE_REMOTE_STAGING) {
1315                            String remoteAddress = ParamUtil.getString(
1316                                    portletRequest, "remoteAddress");
1317    
1318                            remoteAddress = stripProtocolFromRemoteAddress(remoteAddress);
1319    
1320                            long remoteGroupId = ParamUtil.getLong(
1321                                    portletRequest, "remoteGroupId");
1322                            int remotePort = ParamUtil.getInteger(portletRequest, "remotePort");
1323                            boolean secureConnection = ParamUtil.getBoolean(
1324                                    portletRequest, "secureConnection");
1325    
1326                            enableRemoteStaging(
1327                                    userId, scopeGroup, liveGroup, branchingPublic,
1328                                    branchingPrivate, remoteAddress, remoteGroupId, remotePort,
1329                                    secureConnection, serviceContext);
1330                    }
1331            }
1332    
1333            protected void checkDefaultLayoutSetBranches(
1334                            long userId, Group liveGroup, boolean branchingPublic,
1335                            boolean branchingPrivate, boolean remote,
1336                            ServiceContext serviceContext)
1337                    throws Exception {
1338    
1339                    long targetGroupId = 0;
1340    
1341                    if (remote) {
1342                            targetGroupId = liveGroup.getGroupId();
1343                    }
1344                    else {
1345                            Group stagingGroup = liveGroup.getStagingGroup();
1346    
1347                            if (stagingGroup == null) {
1348                                    return;
1349                            }
1350    
1351                            targetGroupId = stagingGroup.getGroupId();
1352                    }
1353    
1354                    if (branchingPublic) {
1355                            Locale locale = LocaleUtil.getDefault();
1356    
1357                            String description = LanguageUtil.format(
1358                                    locale,
1359                                    LayoutSetBranchConstants.MASTER_BRANCH_DESCRIPTION_PUBLIC,
1360                                    liveGroup.getDescriptiveName());
1361    
1362                            try {
1363                                    LayoutSetBranchLocalServiceUtil.addLayoutSetBranch(
1364                                            userId, targetGroupId, false,
1365                                            LayoutSetBranchConstants.MASTER_BRANCH_NAME, description,
1366                                            true, LayoutSetBranchConstants.ALL_BRANCHES,
1367                                            serviceContext);
1368                            }
1369                            catch (LayoutSetBranchNameException lsbne) {
1370                            }
1371                    }
1372    
1373                    if (branchingPrivate) {
1374                            Locale locale = LocaleUtil.getDefault();
1375    
1376                            String description = LanguageUtil.format(
1377                                    locale,
1378                                    LayoutSetBranchConstants.MASTER_BRANCH_DESCRIPTION_PRIVATE,
1379                                    liveGroup.getDescriptiveName());
1380    
1381                            try {
1382                                    LayoutSetBranchLocalServiceUtil.addLayoutSetBranch(
1383                                            userId, targetGroupId, true,
1384                                            LayoutSetBranchConstants.MASTER_BRANCH_NAME, description,
1385                                            true, LayoutSetBranchConstants.ALL_BRANCHES,
1386                                            serviceContext);
1387                            }
1388                            catch (LayoutSetBranchNameException lsbne) {
1389                            }
1390                    }
1391            }
1392    
1393            protected void deleteRecentLayoutRevisionId(
1394                    PortalPreferences portalPreferences, long layoutSetBranchId,
1395                    long plid) {
1396    
1397                    portalPreferences.setValue(
1398                            Staging.class.getName(),
1399                            getRecentLayoutRevisionIdKey(layoutSetBranchId, plid), null);
1400            }
1401    
1402            protected Calendar getDate(
1403                            PortletRequest portletRequest, String paramPrefix,
1404                            boolean timeZoneSensitive)
1405                    throws Exception {
1406    
1407                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
1408                            WebKeys.THEME_DISPLAY);
1409    
1410                    int dateMonth = ParamUtil.getInteger(
1411                            portletRequest, paramPrefix + "Month");
1412                    int dateDay = ParamUtil.getInteger(portletRequest, paramPrefix + "Day");
1413                    int dateYear = ParamUtil.getInteger(
1414                            portletRequest, paramPrefix + "Year");
1415                    int dateHour = ParamUtil.getInteger(
1416                            portletRequest, paramPrefix + "Hour");
1417                    int dateMinute = ParamUtil.getInteger(
1418                            portletRequest, paramPrefix + "Minute");
1419                    int dateAmPm = ParamUtil.getInteger(
1420                            portletRequest, paramPrefix + "AmPm");
1421    
1422                    if (dateAmPm == Calendar.PM) {
1423                            dateHour += 12;
1424                    }
1425    
1426                    Locale locale = null;
1427                    TimeZone timeZone = null;
1428    
1429                    if (timeZoneSensitive) {
1430                            locale = themeDisplay.getLocale();
1431                            timeZone = themeDisplay.getTimeZone();
1432                    }
1433                    else {
1434                            locale = LocaleUtil.getDefault();
1435                            timeZone = TimeZoneUtil.getDefault();
1436                    }
1437    
1438                    Calendar cal = CalendarFactoryUtil.getCalendar(timeZone, locale);
1439    
1440                    cal.set(Calendar.MONTH, dateMonth);
1441                    cal.set(Calendar.DATE, dateDay);
1442                    cal.set(Calendar.YEAR, dateYear);
1443                    cal.set(Calendar.HOUR_OF_DAY, dateHour);
1444                    cal.set(Calendar.MINUTE, dateMinute);
1445                    cal.set(Calendar.SECOND, 0);
1446                    cal.set(Calendar.MILLISECOND, 0);
1447    
1448                    return cal;
1449            }
1450    
1451            protected PortalPreferences getPortalPreferences(User user)
1452                    throws SystemException {
1453    
1454                    boolean signedIn = !user.isDefaultUser();
1455    
1456                    PortalPreferences portalPreferences =
1457                            PortletPreferencesFactoryUtil.getPortalPreferences(
1458                                    user.getCompanyId(), user.getUserId(), signedIn);
1459    
1460                    return portalPreferences;
1461            }
1462    
1463            protected long getRecentLayoutBranchId(
1464                    PortalPreferences portalPreferences, long layoutSetBranchId,
1465                    long plid) {
1466    
1467                    return GetterUtil.getLong(
1468                            portalPreferences.getValue(
1469                                    Staging.class.getName(),
1470                                    getRecentLayoutBranchIdKey(layoutSetBranchId, plid)));
1471            }
1472    
1473            protected String getRecentLayoutBranchIdKey(
1474                    long layoutSetBranchId, long plid) {
1475    
1476                    StringBundler sb = new StringBundler(4);
1477    
1478                    sb.append("layoutBranchId-");
1479                    sb.append(layoutSetBranchId);
1480                    sb.append(StringPool.DASH);
1481                    sb.append(plid);
1482    
1483                    return sb.toString();
1484            }
1485    
1486            protected long getRecentLayoutRevisionId(
1487                            PortalPreferences portalPreferences, long layoutSetBranchId,
1488                            long plid)
1489                    throws PortalException, SystemException {
1490    
1491                    long layoutRevisionId = GetterUtil.getLong(
1492                            portalPreferences.getValue(
1493                                    Staging.class.getName(),
1494                                    getRecentLayoutRevisionIdKey(layoutSetBranchId, plid)));
1495    
1496                    if (layoutRevisionId > 0) {
1497                            return layoutRevisionId;
1498                    }
1499    
1500                    long layoutBranchId = getRecentLayoutBranchId(
1501                            portalPreferences, layoutSetBranchId, plid);
1502    
1503                    if (layoutBranchId > 0) {
1504                            try {
1505                                    LayoutBranchLocalServiceUtil.getLayoutBranch(layoutBranchId);
1506                            }
1507                            catch (NoSuchLayoutBranchException nlbe) {
1508                                    LayoutBranch layoutBranch =
1509                                            LayoutBranchLocalServiceUtil.getMasterLayoutBranch(
1510                                                    layoutSetBranchId, plid);
1511    
1512                                    layoutBranchId = layoutBranch.getLayoutBranchId();
1513                            }
1514                    }
1515    
1516                    if (layoutBranchId > 0) {
1517                            try {
1518                                    LayoutRevision layoutRevision =
1519                                            LayoutRevisionLocalServiceUtil.getLayoutRevision(
1520                                                    layoutSetBranchId, layoutBranchId, plid);
1521    
1522                                    if (layoutRevision != null) {
1523                                            layoutRevisionId = layoutRevision.getLayoutRevisionId();
1524                                    }
1525                            }
1526                            catch (NoSuchLayoutRevisionException nslre) {
1527                            }
1528                    }
1529    
1530                    return layoutRevisionId;
1531            }
1532    
1533            protected String getRecentLayoutRevisionIdKey(
1534                    long layoutSetBranchId, long plid) {
1535    
1536                    StringBundler sb = new StringBundler(4);
1537    
1538                    sb.append("layoutRevisionId-");
1539                    sb.append(layoutSetBranchId);
1540                    sb.append(StringPool.DASH);
1541                    sb.append(plid);
1542    
1543                    return sb.toString();
1544            }
1545    
1546            protected String getRecentLayoutSetBranchIdKey(long layoutSetId) {
1547                    return "layoutSetBranchId_" + layoutSetId;
1548            }
1549    
1550            protected void publishLayouts(
1551                            PortletRequest portletRequest, long sourceGroupId,
1552                            long targetGroupId, Map<String, String[]> parameterMap,
1553                            boolean schedule)
1554                    throws Exception {
1555    
1556                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
1557                            WebKeys.THEME_DISPLAY);
1558    
1559                    String tabs1 = ParamUtil.getString(portletRequest, "tabs1");
1560    
1561                    boolean privateLayout = true;
1562    
1563                    if (tabs1.equals("public-pages")) {
1564                            privateLayout = false;
1565                    }
1566    
1567                    String scope = ParamUtil.getString(portletRequest, "scope");
1568    
1569                    Map<Long, Boolean> layoutIdMap = new LinkedHashMap<Long, Boolean>();
1570    
1571                    if (scope.equals("selected-pages")) {
1572                            long[] rowIds = ParamUtil.getLongValues(portletRequest, "rowIds");
1573    
1574                            for (long selPlid : rowIds) {
1575                                    boolean includeChildren = ParamUtil.getBoolean(
1576                                            portletRequest, "includeChildren_" + selPlid);
1577    
1578                                    layoutIdMap.put(selPlid, includeChildren);
1579                            }
1580                    }
1581    
1582                    String range = ParamUtil.getString(portletRequest, "range");
1583    
1584                    Date startDate = null;
1585                    Date endDate = null;
1586    
1587                    if (range.equals("dateRange")) {
1588                            startDate = getDate(portletRequest, "startDate", true).getTime();
1589    
1590                            endDate = getDate(portletRequest, "endDate", true).getTime();
1591                    }
1592                    else if (range.equals("fromLastPublishDate")) {
1593                            LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
1594                                    sourceGroupId, privateLayout);
1595    
1596                            UnicodeProperties settingsProperties =
1597                                    layoutSet.getSettingsProperties();
1598    
1599                            long lastPublishDate = GetterUtil.getLong(
1600                                    settingsProperties.getProperty("last-publish-date"));
1601    
1602                            if (lastPublishDate > 0) {
1603                                    Calendar cal = Calendar.getInstance(
1604                                            themeDisplay.getTimeZone(), themeDisplay.getLocale());
1605    
1606                                    endDate = cal.getTime();
1607    
1608                                    cal.setTimeInMillis(lastPublishDate);
1609    
1610                                    startDate = cal.getTime();
1611                            }
1612                    }
1613                    else if (range.equals("last")) {
1614                            int rangeLast = ParamUtil.getInteger(portletRequest, "last");
1615    
1616                            Date now = new Date();
1617    
1618                            startDate = new Date(now.getTime() - (rangeLast * Time.HOUR));
1619    
1620                            endDate = now;
1621                    }
1622    
1623                    if (schedule) {
1624                            String groupName = getSchedulerGroupName(
1625                                    DestinationNames.LAYOUTS_LOCAL_PUBLISHER, targetGroupId);
1626    
1627                            int recurrenceType = ParamUtil.getInteger(
1628                                    portletRequest, "recurrenceType");
1629    
1630                            Calendar startCal = getDate(
1631                                    portletRequest, "schedulerStartDate", true);
1632    
1633                            String cronText = SchedulerEngineUtil.getCronText(
1634                                    portletRequest, startCal, true, recurrenceType);
1635    
1636                            Date schedulerEndDate = null;
1637    
1638                            int endDateType = ParamUtil.getInteger(
1639                                    portletRequest, "endDateType");
1640    
1641                            if (endDateType == 1) {
1642                                    Calendar endCal = getDate(
1643                                            portletRequest, "schedulerEndDate", true);
1644    
1645                                    schedulerEndDate = endCal.getTime();
1646                            }
1647    
1648                            String description = ParamUtil.getString(
1649                                    portletRequest, "description");
1650    
1651                            LayoutServiceUtil.schedulePublishToLive(
1652                                    sourceGroupId, targetGroupId, privateLayout, layoutIdMap,
1653                                    parameterMap, scope, startDate, endDate, groupName, cronText,
1654                                    startCal.getTime(), schedulerEndDate, description);
1655                    }
1656                    else {
1657                            MessageStatus messageStatus = new MessageStatus();
1658    
1659                            messageStatus.startTimer();
1660    
1661                            String command =
1662                                    LayoutsLocalPublisherRequest.COMMAND_SELECTED_PAGES;
1663    
1664                            try {
1665                                    if (scope.equals("all-pages")) {
1666                                            command = LayoutsLocalPublisherRequest.COMMAND_ALL_PAGES;
1667    
1668                                            publishLayouts(
1669                                                    themeDisplay.getUserId(), sourceGroupId, targetGroupId,
1670                                                    privateLayout, parameterMap, startDate, endDate);
1671                                    }
1672                                    else {
1673                                            publishLayouts(
1674                                                    themeDisplay.getUserId(), sourceGroupId, targetGroupId,
1675                                                    privateLayout, layoutIdMap, parameterMap, startDate,
1676                                                    endDate);
1677                                    }
1678                            }
1679                            catch (Exception e) {
1680                                    messageStatus.setException(e);
1681    
1682                                    throw e;
1683                            }
1684                            finally {
1685                                    messageStatus.stopTimer();
1686    
1687                                    LayoutsLocalPublisherRequest publisherRequest =
1688                                            new LayoutsLocalPublisherRequest(
1689                                                    command, themeDisplay.getUserId(), sourceGroupId,
1690                                                    targetGroupId, privateLayout, layoutIdMap, parameterMap,
1691                                                    startDate, endDate);
1692    
1693                                    messageStatus.setPayload(publisherRequest);
1694    
1695                                    MessageBusUtil.sendMessage(
1696                                            DestinationNames.MESSAGE_BUS_MESSAGE_STATUS, messageStatus);
1697                            }
1698                    }
1699            }
1700    
1701            protected void publishToRemote(
1702                            PortletRequest portletRequest, boolean schedule)
1703                    throws Exception {
1704    
1705                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
1706                            WebKeys.THEME_DISPLAY);
1707    
1708                    String tabs1 = ParamUtil.getString(portletRequest, "tabs1");
1709    
1710                    long groupId = ParamUtil.getLong(portletRequest, "groupId");
1711    
1712                    boolean privateLayout = true;
1713    
1714                    if (tabs1.equals("public-pages")) {
1715                            privateLayout = false;
1716                    }
1717    
1718                    String scope = ParamUtil.getString(portletRequest, "scope");
1719    
1720                    if (Validator.isNull(scope)) {
1721                            scope = "all-pages";
1722                    }
1723    
1724                    Map<Long, Boolean> layoutIdMap = null;
1725    
1726                    if (scope.equals("selected-pages")) {
1727                            layoutIdMap = new LinkedHashMap<Long, Boolean>();
1728    
1729                            long[] rowIds = ParamUtil.getLongValues(portletRequest, "rowIds");
1730    
1731                            for (long selPlid : rowIds) {
1732                                    boolean includeChildren = ParamUtil.getBoolean(
1733                                            portletRequest, "includeChildren_" + selPlid);
1734    
1735                                    layoutIdMap.put(selPlid, includeChildren);
1736                            }
1737                    }
1738    
1739                    Map<String, String[]> parameterMap = getStagingParameters(
1740                            portletRequest);
1741    
1742                    parameterMap.put(
1743                            PortletDataHandlerKeys.PUBLISH_TO_REMOTE,
1744                            new String[] {Boolean.TRUE.toString()});
1745    
1746                    Group group = GroupLocalServiceUtil.getGroup(groupId);
1747    
1748                    UnicodeProperties groupTypeSettingsProperties =
1749                            group.getTypeSettingsProperties();
1750    
1751                    String remoteAddress = ParamUtil.getString(
1752                            portletRequest, "remoteAddress",
1753                            groupTypeSettingsProperties.getProperty("remoteAddress"));
1754    
1755                    remoteAddress = stripProtocolFromRemoteAddress(remoteAddress);
1756    
1757                    long remoteGroupId = ParamUtil.getLong(
1758                            portletRequest, "remoteGroupId",
1759                            GetterUtil.getLong(
1760                                    groupTypeSettingsProperties.getProperty("remoteGroupId")));
1761                    int remotePort = ParamUtil.getInteger(
1762                            portletRequest, "remotePort",
1763                            GetterUtil.getInteger(
1764                                    groupTypeSettingsProperties.getProperty("remotePort")));
1765                    boolean remotePrivateLayout = ParamUtil.getBoolean(
1766                            portletRequest, "remotePrivateLayout");
1767                    boolean secureConnection = ParamUtil.getBoolean(
1768                            portletRequest, "secureConnection",
1769                            GetterUtil.getBoolean(
1770                                    groupTypeSettingsProperties.getProperty("secureConnection")));
1771    
1772                    validate(remoteAddress, remoteGroupId, remotePort, secureConnection);
1773    
1774                    String range = ParamUtil.getString(portletRequest, "range");
1775    
1776                    Date startDate = null;
1777                    Date endDate = null;
1778    
1779                    if (range.equals("dateRange")) {
1780                            startDate = getDate(portletRequest, "startDate", true).getTime();
1781    
1782                            endDate = getDate(portletRequest, "endDate", true).getTime();
1783                    }
1784                    else if (range.equals("fromLastPublishDate")) {
1785                            LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
1786                                    groupId, privateLayout);
1787    
1788                            UnicodeProperties layoutTypeSettingsProperties =
1789                                    layoutSet.getSettingsProperties();
1790    
1791                            long lastPublishDate = GetterUtil.getLong(
1792                                    layoutTypeSettingsProperties.getProperty("last-publish-date"));
1793    
1794                            if (lastPublishDate > 0) {
1795                                    Calendar cal = Calendar.getInstance(
1796                                            themeDisplay.getTimeZone(), themeDisplay.getLocale());
1797    
1798                                    endDate = cal.getTime();
1799    
1800                                    cal.setTimeInMillis(lastPublishDate);
1801    
1802                                    startDate = cal.getTime();
1803                            }
1804                    }
1805                    else if (range.equals("last")) {
1806                            int rangeLast = ParamUtil.getInteger(portletRequest, "last");
1807    
1808                            Date now = new Date();
1809    
1810                            startDate = new Date(now.getTime() - (rangeLast * Time.HOUR));
1811    
1812                            endDate = now;
1813                    }
1814    
1815                    if (schedule) {
1816                            String groupName = getSchedulerGroupName(
1817                                    DestinationNames.LAYOUTS_REMOTE_PUBLISHER, groupId);
1818    
1819                            int recurrenceType = ParamUtil.getInteger(
1820                                    portletRequest, "recurrenceType");
1821    
1822                            Calendar startCal = getDate(
1823                                    portletRequest, "schedulerStartDate", true);
1824    
1825                            String cronText = SchedulerEngineUtil.getCronText(
1826                                    portletRequest, startCal, true, recurrenceType);
1827    
1828                            Date schedulerEndDate = null;
1829    
1830                            int endDateType = ParamUtil.getInteger(
1831                                    portletRequest, "endDateType");
1832    
1833                            if (endDateType == 1) {
1834                                    Calendar endCal = getDate(
1835                                            portletRequest, "schedulerEndDate", true);
1836    
1837                                    schedulerEndDate = endCal.getTime();
1838                            }
1839    
1840                            String description = ParamUtil.getString(
1841                                    portletRequest, "description");
1842    
1843                            LayoutServiceUtil.schedulePublishToRemote(
1844                                    groupId, privateLayout, layoutIdMap, parameterMap,
1845                                    remoteAddress, remotePort, secureConnection, remoteGroupId,
1846                                    remotePrivateLayout, startDate, endDate, groupName, cronText,
1847                                    startCal.getTime(), schedulerEndDate, description);
1848                    }
1849                    else {
1850                            MessageStatus messageStatus = new MessageStatus();
1851    
1852                            messageStatus.startTimer();
1853    
1854                            try {
1855                                    copyRemoteLayouts(
1856                                            groupId, privateLayout, layoutIdMap, parameterMap,
1857                                            remoteAddress, remotePort, secureConnection, remoteGroupId,
1858                                            remotePrivateLayout, startDate, endDate);
1859                            }
1860                            catch (Exception e) {
1861                                    messageStatus.setException(e);
1862    
1863                                    throw e;
1864                            }
1865                            finally {
1866                                    messageStatus.stopTimer();
1867    
1868                                    LayoutsRemotePublisherRequest publisherRequest =
1869                                            new LayoutsRemotePublisherRequest(
1870                                                    themeDisplay.getUserId(), groupId, privateLayout,
1871                                                    layoutIdMap, parameterMap, remoteAddress, remotePort,
1872                                                    secureConnection, remoteGroupId, remotePrivateLayout,
1873                                                    startDate, endDate);
1874    
1875                                    messageStatus.setPayload(publisherRequest);
1876    
1877                                    MessageBusUtil.sendMessage(
1878                                            DestinationNames.MESSAGE_BUS_MESSAGE_STATUS, messageStatus);
1879                            }
1880                    }
1881            }
1882    
1883            protected void setCommonStagingOptions(
1884                            Group liveGroup, UnicodeProperties typeSettingsProperties,
1885                            ServiceContext serviceContext)
1886                    throws Exception {
1887    
1888                    LayoutExporter.updateLastPublishDate(
1889                            liveGroup.getPrivateLayoutSet(), 0);
1890                    LayoutExporter.updateLastPublishDate(liveGroup.getPublicLayoutSet(), 0);
1891    
1892                    Set<String> parameterNames = serviceContext.getAttributes().keySet();
1893    
1894                    for (String parameterName : parameterNames) {
1895                            boolean staged = ParamUtil.getBoolean(
1896                                    serviceContext, parameterName);
1897    
1898                            if (parameterName.startsWith(StagingConstants.STAGED_PORTLET) &&
1899                                    !parameterName.endsWith("Checkbox")) {
1900    
1901                                    typeSettingsProperties.setProperty(
1902                                            parameterName, String.valueOf(staged));
1903                            }
1904                    }
1905            }
1906    
1907            protected void setRecentLayoutBranchId(
1908                    PortalPreferences portalPreferences, long layoutSetBranchId, long plid,
1909                    long layoutBranchId) {
1910    
1911                    portalPreferences.setValue(
1912                            Staging.class.getName(),
1913                            getRecentLayoutBranchIdKey(layoutSetBranchId, plid),
1914                            String.valueOf(layoutBranchId));
1915            }
1916    
1917            protected void setRecentLayoutRevisionId(
1918                            PortalPreferences portalPreferences, long layoutSetBranchId,
1919                            long plid, long layoutRevisionId)
1920                    throws SystemException {
1921    
1922                    long layoutBranchId = 0;
1923    
1924                    try {
1925                            LayoutRevision layoutRevision =
1926                                    LayoutRevisionLocalServiceUtil.getLayoutRevision(
1927                                            layoutRevisionId);
1928    
1929                            layoutBranchId = layoutRevision.getLayoutBranchId();
1930    
1931                            LayoutRevision lastLayoutRevision =
1932                                    LayoutRevisionLocalServiceUtil.getLayoutRevision(
1933                                            layoutSetBranchId, layoutBranchId, plid);
1934    
1935                            if (lastLayoutRevision.getLayoutRevisionId() == layoutRevisionId) {
1936                                    deleteRecentLayoutRevisionId(
1937                                            portalPreferences, layoutSetBranchId, plid);
1938                            }
1939                            else {
1940                                    portalPreferences.setValue(
1941                                            Staging.class.getName(),
1942                                            getRecentLayoutRevisionIdKey(layoutSetBranchId, plid),
1943                                            String.valueOf(layoutRevisionId));
1944                            }
1945                    }
1946                    catch (PortalException pe) {
1947                    }
1948    
1949                    portalPreferences.setValue(
1950                            Staging.class.getName(),
1951                            getRecentLayoutBranchIdKey(layoutSetBranchId, plid),
1952                            String.valueOf(layoutBranchId));
1953            }
1954    
1955            protected String stripProtocolFromRemoteAddress(String remoteAddress) {
1956                    if (remoteAddress.startsWith(Http.HTTP_WITH_SLASH)) {
1957                            remoteAddress = remoteAddress.substring(
1958                                    Http.HTTP_WITH_SLASH.length());
1959                    }
1960                    else if (remoteAddress.startsWith(Http.HTTPS_WITH_SLASH)) {
1961                            remoteAddress = remoteAddress.substring(
1962                                    Http.HTTPS_WITH_SLASH.length());
1963                    }
1964    
1965                    return remoteAddress;
1966            }
1967    
1968            protected void validate(
1969                            String remoteAddress, long remoteGroupId, int remotePort,
1970                            boolean secureConnection)
1971                    throws Exception {
1972    
1973                    RemoteOptionsException roe = null;
1974    
1975                    if (!Validator.isDomain(remoteAddress) &&
1976                            !Validator.isIPAddress(remoteAddress)) {
1977    
1978                            roe = new RemoteOptionsException(
1979                                    RemoteOptionsException.REMOTE_ADDRESS);
1980    
1981                            roe.setRemoteAddress(remoteAddress);
1982    
1983                            throw roe;
1984                    }
1985    
1986                    if ((remotePort < 1) || (remotePort > 65535)) {
1987                            roe = new RemoteOptionsException(
1988                                    RemoteOptionsException.REMOTE_PORT);
1989    
1990                            roe.setRemotePort(remotePort);
1991    
1992                            throw roe;
1993                    }
1994    
1995                    if (remoteGroupId <= 0) {
1996                            roe = new RemoteOptionsException(
1997                                    RemoteOptionsException.REMOTE_GROUP_ID);
1998    
1999                            roe.setRemoteGroupId(remoteGroupId);
2000    
2001                            throw roe;
2002                    }
2003    
2004                    PermissionChecker permissionChecker =
2005                            PermissionThreadLocal.getPermissionChecker();
2006    
2007                    User user = UserLocalServiceUtil.getUser(permissionChecker.getUserId());
2008    
2009                    String url = buildRemoteURL(
2010                            remoteAddress, remotePort, secureConnection,
2011                            GroupConstants.DEFAULT_LIVE_GROUP_ID, false);
2012    
2013                    HttpPrincipal httpPrincipal = new HttpPrincipal(
2014                            url, user.getEmailAddress(), user.getPassword(),
2015                            user.getPasswordEncrypted());
2016    
2017                    // Ping remote host and verify that the group exists
2018    
2019                    try {
2020                            GroupServiceHttp.getGroup(httpPrincipal, remoteGroupId);
2021                    }
2022                    catch (NoSuchGroupException nsge) {
2023                            RemoteExportException ree = new RemoteExportException(
2024                                    RemoteExportException.NO_GROUP);
2025    
2026                            ree.setGroupId(remoteGroupId);
2027    
2028                            throw ree;
2029                    }
2030                    catch (PrincipalException pe) {
2031                            RemoteExportException ree = new RemoteExportException(
2032                                    RemoteExportException.NO_PERMISSIONS);
2033    
2034                            ree.setGroupId(remoteGroupId);
2035    
2036                            throw ree;
2037                    }
2038                    catch (SystemException se) {
2039                            RemoteExportException ree = new RemoteExportException(
2040                                    RemoteExportException.BAD_CONNECTION);
2041    
2042                            ree.setURL(url);
2043    
2044                            throw ree;
2045                    }
2046    
2047            }
2048    
2049    }