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