001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.exception.NoSuchLayoutRevisionException;
018    import com.liferay.portal.exception.NoSuchPortletPreferencesException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
026    import com.liferay.portal.model.Layout;
027    import com.liferay.portal.model.LayoutConstants;
028    import com.liferay.portal.model.LayoutRevision;
029    import com.liferay.portal.model.LayoutRevisionConstants;
030    import com.liferay.portal.model.LayoutSetBranch;
031    import com.liferay.portal.model.PortletPreferences;
032    import com.liferay.portal.model.User;
033    import com.liferay.portal.service.ServiceContext;
034    import com.liferay.portal.service.base.LayoutRevisionLocalServiceBaseImpl;
035    import com.liferay.portal.util.comparator.LayoutRevisionCreateDateComparator;
036    import com.liferay.portlet.exportimport.staging.MergeLayoutPrototypesThreadLocal;
037    import com.liferay.portlet.exportimport.staging.StagingUtil;
038    
039    import java.util.ArrayList;
040    import java.util.Date;
041    import java.util.List;
042    
043    /**
044     * @author Raymond Aug??
045     * @author Brian Wing Shun Chan
046     */
047    public class LayoutRevisionLocalServiceImpl
048            extends LayoutRevisionLocalServiceBaseImpl {
049    
050            @Override
051            public LayoutRevision addLayoutRevision(
052                            long userId, long layoutSetBranchId, long layoutBranchId,
053                            long parentLayoutRevisionId, boolean head, long plid,
054                            long portletPreferencesPlid, boolean privateLayout, String name,
055                            String title, String description, String keywords, String robots,
056                            String typeSettings, boolean iconImage, long iconImageId,
057                            String themeId, String colorSchemeId, String wapThemeId,
058                            String wapColorSchemeId, String css, ServiceContext serviceContext)
059                    throws PortalException {
060    
061                    // Layout revision
062    
063                    User user = userPersistence.findByPrimaryKey(userId);
064                    LayoutSetBranch layoutSetBranch =
065                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
066                    parentLayoutRevisionId = getParentLayoutRevisionId(
067                            layoutSetBranchId, parentLayoutRevisionId, plid);
068                    Date now = new Date();
069    
070                    long layoutRevisionId = counterLocalService.increment();
071    
072                    LayoutRevision layoutRevision = layoutRevisionPersistence.create(
073                            layoutRevisionId);
074    
075                    layoutRevision.setGroupId(layoutSetBranch.getGroupId());
076                    layoutRevision.setCompanyId(user.getCompanyId());
077                    layoutRevision.setUserId(user.getUserId());
078                    layoutRevision.setUserName(user.getFullName());
079                    layoutRevision.setLayoutSetBranchId(layoutSetBranchId);
080                    layoutRevision.setLayoutBranchId(layoutBranchId);
081                    layoutRevision.setParentLayoutRevisionId(parentLayoutRevisionId);
082                    layoutRevision.setHead(head);
083                    layoutRevision.setPlid(plid);
084                    layoutRevision.setPrivateLayout(privateLayout);
085                    layoutRevision.setName(name);
086                    layoutRevision.setTitle(title);
087                    layoutRevision.setDescription(description);
088                    layoutRevision.setKeywords(keywords);
089                    layoutRevision.setRobots(robots);
090                    layoutRevision.setTypeSettings(typeSettings);
091                    layoutRevision.setIconImageId(iconImageId);
092                    layoutRevision.setThemeId(themeId);
093                    layoutRevision.setColorSchemeId(colorSchemeId);
094                    layoutRevision.setWapThemeId(wapThemeId);
095                    layoutRevision.setWapColorSchemeId(wapColorSchemeId);
096                    layoutRevision.setCss(css);
097                    layoutRevision.setStatus(WorkflowConstants.STATUS_DRAFT);
098                    layoutRevision.setStatusDate(serviceContext.getModifiedDate(now));
099    
100                    layoutRevisionPersistence.update(layoutRevision);
101    
102                    _layoutRevisionId.set(layoutRevision.getLayoutRevisionId());
103    
104                    // Portlet preferences
105    
106                    if (portletPreferencesPlid == LayoutConstants.DEFAULT_PLID) {
107                            portletPreferencesPlid = plid;
108                    }
109    
110                    copyPortletPreferences(layoutRevision, portletPreferencesPlid);
111    
112                    // Workflow
113    
114                    if (isWorkflowEnabled(plid)) {
115                            WorkflowHandlerRegistryUtil.startWorkflowInstance(
116                                    user.getCompanyId(), layoutRevision.getGroupId(),
117                                    user.getUserId(), LayoutRevision.class.getName(),
118                                    layoutRevision.getLayoutRevisionId(), layoutRevision,
119                                    serviceContext);
120                    }
121                    else {
122                            updateMajor(layoutRevision);
123    
124                            updateStatus(
125                                    userId, layoutRevisionId, WorkflowConstants.STATUS_APPROVED,
126                                    serviceContext);
127                    }
128    
129                    StagingUtil.setRecentLayoutRevisionId(
130                            user, layoutSetBranchId, plid,
131                            layoutRevision.getLayoutRevisionId());
132    
133                    return layoutRevision;
134            }
135    
136            @Override
137            public void deleteLayoutLayoutRevisions(long plid) throws PortalException {
138                    for (LayoutRevision layoutRevision : getLayoutRevisions(plid)) {
139                            layoutRevisionLocalService.deleteLayoutRevision(layoutRevision);
140                    }
141            }
142    
143            @Override
144            public LayoutRevision deleteLayoutRevision(LayoutRevision layoutRevision)
145                    throws PortalException {
146    
147                    if (layoutRevision.hasChildren()) {
148                            for (LayoutRevision curLayoutRevision :
149                                            layoutRevision.getChildren()) {
150    
151                                    curLayoutRevision.setParentLayoutRevisionId(
152                                            layoutRevision.getParentLayoutRevisionId());
153    
154                                    layoutRevisionPersistence.update(curLayoutRevision);
155                            }
156                    }
157    
158                    List<PortletPreferences> portletPreferencesList =
159                            portletPreferencesLocalService.getPortletPreferencesByPlid(
160                                    layoutRevision.getLayoutRevisionId());
161    
162                    for (PortletPreferences portletPreferences : portletPreferencesList) {
163                            try {
164                                    portletPreferencesLocalService.deletePortletPreferences(
165                                            portletPreferences.getPortletPreferencesId());
166                            }
167                            catch (NoSuchPortletPreferencesException nsppe) {
168                            }
169                    }
170    
171                    recentLayoutRevisionLocalService.deleteRecentLayoutRevisions(
172                            layoutRevision.getLayoutRevisionId());
173    
174                    if (layoutRevision.isPending()) {
175                            workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
176                                    layoutRevision.getCompanyId(), layoutRevision.getGroupId(),
177                                    LayoutRevision.class.getName(),
178                                    layoutRevision.getLayoutRevisionId());
179                    }
180    
181                    return layoutRevisionPersistence.remove(layoutRevision);
182            }
183    
184            @Override
185            public LayoutRevision deleteLayoutRevision(long layoutRevisionId)
186                    throws PortalException {
187    
188                    LayoutRevision layoutRevision =
189                            layoutRevisionPersistence.findByPrimaryKey(layoutRevisionId);
190    
191                    return deleteLayoutRevision(layoutRevision);
192            }
193    
194            @Override
195            public void deleteLayoutRevisions(long layoutSetBranchId, long plid)
196                    throws PortalException {
197    
198                    for (LayoutRevision layoutRevision : getLayoutRevisions(
199                                    layoutSetBranchId, plid)) {
200    
201                            layoutRevisionLocalService.deleteLayoutRevision(layoutRevision);
202                    }
203            }
204    
205            @Override
206            public void deleteLayoutRevisions(
207                            long layoutSetBranchId, long layoutBranchId, long plid)
208                    throws PortalException {
209    
210                    List<LayoutRevision> layoutRevisions =
211                            layoutRevisionPersistence.findByL_L_P(
212                                    layoutSetBranchId, layoutBranchId, plid);
213    
214                    for (LayoutRevision layoutRevision : layoutRevisions) {
215                            layoutRevisionLocalService.deleteLayoutRevision(layoutRevision);
216                    }
217            }
218    
219            @Override
220            public void deleteLayoutSetBranchLayoutRevisions(long layoutSetBranchId)
221                    throws PortalException {
222    
223                    List<LayoutRevision> layoutRevisions =
224                            layoutRevisionPersistence.findByLayoutSetBranchId(
225                                    layoutSetBranchId);
226    
227                    for (LayoutRevision layoutRevision : layoutRevisions) {
228                            layoutRevisionLocalService.deleteLayoutRevision(layoutRevision);
229                    }
230            }
231    
232            @Override
233            public LayoutRevision fetchLastLayoutRevision(long plid, boolean head) {
234                    try {
235                            return layoutRevisionPersistence.findByH_P_Last(
236                                    head, plid, new LayoutRevisionCreateDateComparator(true));
237                    }
238                    catch (NoSuchLayoutRevisionException nslre) {
239                            return null;
240                    }
241            }
242    
243            @Override
244            public LayoutRevision fetchLatestLayoutRevision(
245                    long layoutSetBranchId, long plid) {
246    
247                    return layoutRevisionPersistence.fetchByL_P_First(
248                            layoutSetBranchId, plid,
249                            new LayoutRevisionCreateDateComparator(false));
250            }
251    
252            @Override
253            public LayoutRevision fetchLayoutRevision(
254                    long layoutSetBranchId, boolean head, long plid) {
255    
256                    return layoutRevisionPersistence.fetchByL_H_P(
257                            layoutSetBranchId, head, plid);
258            }
259    
260            @Override
261            public List<LayoutRevision> getChildLayoutRevisions(
262                    long layoutSetBranchId, long parentLayoutRevisionId, long plid) {
263    
264                    return layoutRevisionPersistence.findByL_P_P(
265                            layoutSetBranchId, parentLayoutRevisionId, plid);
266            }
267    
268            @Override
269            public List<LayoutRevision> getChildLayoutRevisions(
270                    long layoutSetBranchId, long parentLayoutRevision, long plid, int start,
271                    int end, OrderByComparator<LayoutRevision> orderByComparator) {
272    
273                    return layoutRevisionPersistence.findByL_P_P(
274                            layoutSetBranchId, parentLayoutRevision, plid, start, end,
275                            orderByComparator);
276            }
277    
278            @Override
279            public int getChildLayoutRevisionsCount(
280                    long layoutSetBranchId, long parentLayoutRevision, long plid) {
281    
282                    return layoutRevisionPersistence.countByL_P_P(
283                            layoutSetBranchId, parentLayoutRevision, plid);
284            }
285    
286            @Override
287            public LayoutRevision getLayoutRevision(
288                            long layoutSetBranchId, long plid, boolean head)
289                    throws PortalException {
290    
291                    return layoutRevisionPersistence.findByL_H_P(
292                            layoutSetBranchId, head, plid);
293            }
294    
295            @Override
296            public LayoutRevision getLayoutRevision(
297                            long layoutSetBranchId, long layoutBranchId, long plid)
298                    throws PortalException {
299    
300                    List<LayoutRevision> layoutRevisions =
301                            layoutRevisionPersistence.findByL_L_P(
302                                    layoutSetBranchId, layoutBranchId, plid, 0, 1,
303                                    new LayoutRevisionCreateDateComparator(false));
304    
305                    if (!layoutRevisions.isEmpty()) {
306                            return layoutRevisions.get(0);
307                    }
308    
309                    StringBundler sb = new StringBundler(7);
310    
311                    sb.append("{layoutSetBranchId=");
312                    sb.append(layoutSetBranchId);
313                    sb.append(", layoutBranchId=");
314                    sb.append(layoutBranchId);
315                    sb.append(", plid=");
316                    sb.append(plid);
317                    sb.append("}");
318    
319                    throw new NoSuchLayoutRevisionException(sb.toString());
320            }
321    
322            @Override
323            public List<LayoutRevision> getLayoutRevisions(long plid) {
324                    return layoutRevisionPersistence.findByPlid(plid);
325            }
326    
327            @Override
328            public List<LayoutRevision> getLayoutRevisions(
329                    long layoutSetBranchId, boolean head) {
330    
331                    return layoutRevisionPersistence.findByL_H(layoutSetBranchId, head);
332            }
333    
334            @Override
335            public List<LayoutRevision> getLayoutRevisions(
336                    long layoutSetBranchId, int status) {
337    
338                    return layoutRevisionPersistence.findByL_S(layoutSetBranchId, status);
339            }
340    
341            @Override
342            public List<LayoutRevision> getLayoutRevisions(
343                    long layoutSetBranchId, long plid) {
344    
345                    return layoutRevisionPersistence.findByL_P(layoutSetBranchId, plid);
346            }
347    
348            @Override
349            public List<LayoutRevision> getLayoutRevisions(
350                    long layoutSetBranchId, long plid, int status) {
351    
352                    return layoutRevisionPersistence.findByL_P_S(
353                            layoutSetBranchId, plid, status);
354            }
355    
356            @Override
357            public List<LayoutRevision> getLayoutRevisions(
358                    long layoutSetBranchId, long plid, int start, int end,
359                    OrderByComparator<LayoutRevision> orderByComparator) {
360    
361                    return layoutRevisionPersistence.findByL_P(
362                            layoutSetBranchId, plid, start, end, orderByComparator);
363            }
364    
365            @Override
366            public List<LayoutRevision> getLayoutRevisions(
367                    long layoutSetBranchId, long layoutBranchId, long plid, int start,
368                    int end, OrderByComparator<LayoutRevision> orderByComparator) {
369    
370                    return layoutRevisionPersistence.findByL_L_P(
371                            layoutSetBranchId, layoutBranchId, plid, start, end,
372                            orderByComparator);
373            }
374    
375            @Override
376            public int getLayoutRevisionsCount(
377                    long layoutSetBranchId, long layoutBranchId, long plid) {
378    
379                    return layoutRevisionPersistence.countByL_L_P(
380                            layoutSetBranchId, layoutBranchId, plid);
381            }
382    
383            @Override
384            public LayoutRevision updateLayoutRevision(
385                            long userId, long layoutRevisionId, long layoutBranchId,
386                            String name, String title, String description, String keywords,
387                            String robots, String typeSettings, boolean iconImage,
388                            long iconImageId, String themeId, String colorSchemeId,
389                            String wapThemeId, String wapColorSchemeId, String css,
390                            ServiceContext serviceContext)
391                    throws PortalException {
392    
393                    // Layout revision
394    
395                    User user = userPersistence.findByPrimaryKey(userId);
396                    LayoutRevision oldLayoutRevision =
397                            layoutRevisionPersistence.findByPrimaryKey(layoutRevisionId);
398                    Date now = new Date();
399    
400                    LayoutRevision layoutRevision = null;
401    
402                    int workflowAction = serviceContext.getWorkflowAction();
403    
404                    boolean revisionInProgress = ParamUtil.getBoolean(
405                            serviceContext, "revisionInProgress");
406    
407                    if (!MergeLayoutPrototypesThreadLocal.isInProgress() &&
408                            (workflowAction != WorkflowConstants.ACTION_PUBLISH) &&
409                            (_layoutRevisionId.get() <= 0) && !revisionInProgress) {
410    
411                            long newLayoutRevisionId = counterLocalService.increment();
412    
413                            layoutRevision = layoutRevisionPersistence.create(
414                                    newLayoutRevisionId);
415    
416                            layoutRevision.setGroupId(oldLayoutRevision.getGroupId());
417                            layoutRevision.setCompanyId(oldLayoutRevision.getCompanyId());
418                            layoutRevision.setUserId(user.getUserId());
419                            layoutRevision.setUserName(user.getFullName());
420                            layoutRevision.setLayoutSetBranchId(
421                                    oldLayoutRevision.getLayoutSetBranchId());
422                            layoutRevision.setParentLayoutRevisionId(
423                                    oldLayoutRevision.getLayoutRevisionId());
424                            layoutRevision.setHead(false);
425                            layoutRevision.setLayoutBranchId(layoutBranchId);
426                            layoutRevision.setPlid(oldLayoutRevision.getPlid());
427                            layoutRevision.setPrivateLayout(
428                                    oldLayoutRevision.isPrivateLayout());
429                            layoutRevision.setName(name);
430                            layoutRevision.setTitle(title);
431                            layoutRevision.setDescription(description);
432                            layoutRevision.setKeywords(keywords);
433                            layoutRevision.setRobots(robots);
434                            layoutRevision.setTypeSettings(typeSettings);
435                            layoutRevision.setIconImageId(iconImageId);
436                            layoutRevision.setThemeId(themeId);
437                            layoutRevision.setColorSchemeId(colorSchemeId);
438                            layoutRevision.setWapThemeId(wapThemeId);
439                            layoutRevision.setWapColorSchemeId(wapColorSchemeId);
440                            layoutRevision.setCss(css);
441                            layoutRevision.setStatus(WorkflowConstants.STATUS_DRAFT);
442                            layoutRevision.setStatusDate(serviceContext.getModifiedDate(now));
443    
444                            layoutRevisionPersistence.update(layoutRevision);
445    
446                            _layoutRevisionId.set(layoutRevision.getLayoutRevisionId());
447    
448                            // Portlet preferences
449    
450                            copyPortletPreferences(
451                                    layoutRevision, layoutRevision.getParentLayoutRevisionId());
452    
453                            StagingUtil.setRecentLayoutBranchId(
454                                    user, layoutRevision.getLayoutSetBranchId(),
455                                    layoutRevision.getPlid(), layoutRevision.getLayoutBranchId());
456    
457                            StagingUtil.setRecentLayoutRevisionId(
458                                    user, layoutRevision.getLayoutSetBranchId(),
459                                    layoutRevision.getPlid(), layoutRevision.getLayoutRevisionId());
460                    }
461                    else {
462                            if (_layoutRevisionId.get() > 0) {
463                                    layoutRevision = layoutRevisionPersistence.findByPrimaryKey(
464                                            _layoutRevisionId.get());
465                            }
466                            else {
467                                    layoutRevision = oldLayoutRevision;
468                            }
469    
470                            layoutRevision.setName(name);
471                            layoutRevision.setTitle(title);
472                            layoutRevision.setDescription(description);
473                            layoutRevision.setKeywords(keywords);
474                            layoutRevision.setRobots(robots);
475                            layoutRevision.setTypeSettings(typeSettings);
476                            layoutRevision.setIconImageId(iconImageId);
477                            layoutRevision.setThemeId(themeId);
478                            layoutRevision.setColorSchemeId(colorSchemeId);
479                            layoutRevision.setWapThemeId(wapThemeId);
480                            layoutRevision.setWapColorSchemeId(wapColorSchemeId);
481                            layoutRevision.setCss(css);
482    
483                            layoutRevisionPersistence.update(layoutRevision);
484    
485                            _layoutRevisionId.set(layoutRevision.getLayoutRevisionId());
486                    }
487    
488                    boolean major = ParamUtil.getBoolean(serviceContext, "major");
489    
490                    if (major || !isWorkflowEnabled(layoutRevision.getPlid())) {
491                            updateMajor(layoutRevision);
492                    }
493    
494                    // Workflow
495    
496                    if (isWorkflowEnabled(layoutRevision.getPlid())) {
497                            WorkflowHandlerRegistryUtil.startWorkflowInstance(
498                                    layoutRevision.getCompanyId(), layoutRevision.getGroupId(),
499                                    userId, LayoutRevision.class.getName(),
500                                    layoutRevision.getLayoutRevisionId(), layoutRevision,
501                                    serviceContext);
502                    }
503                    else {
504                            updateStatus(
505                                    userId, layoutRevision.getLayoutRevisionId(),
506                                    WorkflowConstants.STATUS_APPROVED, serviceContext);
507                    }
508    
509                    return layoutRevision;
510            }
511    
512            @Override
513            public LayoutRevision updateStatus(
514                            long userId, long layoutRevisionId, int status,
515                            ServiceContext serviceContext)
516                    throws PortalException {
517    
518                    User user = userPersistence.findByPrimaryKey(userId);
519    
520                    LayoutRevision layoutRevision =
521                            layoutRevisionPersistence.findByPrimaryKey(layoutRevisionId);
522    
523                    layoutRevision.setStatus(status);
524                    layoutRevision.setStatusByUserId(user.getUserId());
525                    layoutRevision.setStatusByUserName(user.getFullName());
526                    layoutRevision.setStatusDate(new Date());
527    
528                    if (status == WorkflowConstants.STATUS_APPROVED) {
529                            layoutRevision.setHead(true);
530                    }
531                    else {
532                            layoutRevision.setHead(false);
533                            layoutRevision.setMajor(false);
534                    }
535    
536                    layoutRevisionPersistence.update(layoutRevision);
537    
538                    if (status == WorkflowConstants.STATUS_APPROVED) {
539                            List<LayoutRevision> layoutRevisions =
540                                    layoutRevisionPersistence.findByL_P(
541                                            layoutRevision.getLayoutSetBranchId(),
542                                            layoutRevision.getPlid());
543    
544                            for (LayoutRevision curLayoutRevision : layoutRevisions) {
545                                    if (curLayoutRevision.getLayoutRevisionId() !=
546                                                    layoutRevision.getLayoutRevisionId()) {
547    
548                                            curLayoutRevision.setHead(false);
549    
550                                            layoutRevisionPersistence.update(curLayoutRevision);
551                                    }
552                            }
553                    }
554                    else {
555                            List<LayoutRevision> layoutRevisions =
556                                    layoutRevisionPersistence.findByL_P_S(
557                                            layoutRevision.getLayoutSetBranchId(),
558                                            layoutRevision.getPlid(),
559                                            WorkflowConstants.STATUS_APPROVED);
560    
561                            for (LayoutRevision curLayoutRevision : layoutRevisions) {
562                                    if (curLayoutRevision.getLayoutRevisionId() !=
563                                                    layoutRevision.getLayoutRevisionId()) {
564    
565                                            curLayoutRevision.setHead(true);
566    
567                                            layoutRevisionPersistence.update(curLayoutRevision);
568    
569                                            break;
570                                    }
571                            }
572                    }
573    
574                    return layoutRevision;
575            }
576    
577            protected void copyPortletPreferences(
578                    LayoutRevision layoutRevision, long parentLayoutRevisionId) {
579    
580                    List<PortletPreferences> portletPreferencesList =
581                            portletPreferencesLocalService.getPortletPreferencesByPlid(
582                                    parentLayoutRevisionId);
583    
584                    for (PortletPreferences portletPreferences : portletPreferencesList) {
585                            portletPreferencesLocalService.addPortletPreferences(
586                                    layoutRevision.getCompanyId(), portletPreferences.getOwnerId(),
587                                    portletPreferences.getOwnerType(),
588                                    layoutRevision.getLayoutRevisionId(),
589                                    portletPreferences.getPortletId(), null,
590                                    portletPreferences.getPreferences());
591                    }
592            }
593    
594            protected long getParentLayoutRevisionId(
595                    long layoutSetBranchId, long parentLayoutRevisionId, long plid) {
596    
597                    LayoutRevision parentLayoutRevision = null;
598    
599                    if (parentLayoutRevisionId > 0) {
600                            parentLayoutRevision = layoutRevisionPersistence.fetchByPrimaryKey(
601                                    parentLayoutRevisionId);
602    
603                            if (parentLayoutRevision == null) {
604                                    List<LayoutRevision> layoutRevisions =
605                                            layoutRevisionPersistence.findByL_P(
606                                                    layoutSetBranchId, plid, 0, 1);
607    
608                                    if (!layoutRevisions.isEmpty()) {
609                                            parentLayoutRevision = layoutRevisions.get(0);
610                                    }
611                            }
612                    }
613    
614                    if (parentLayoutRevision != null) {
615                            return parentLayoutRevision.getLayoutRevisionId();
616                    }
617    
618                    return LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID;
619            }
620    
621            protected boolean isWorkflowEnabled(long plid) throws PortalException {
622                    Layout layout = layoutLocalService.getLayout(plid);
623    
624                    if (layout.isTypeLinkToLayout() || layout.isTypeURL()) {
625                            return false;
626                    }
627    
628                    return true;
629            }
630    
631            protected LayoutRevision updateMajor(LayoutRevision layoutRevision)
632                    throws PortalException {
633    
634                    List<LayoutRevision> parentLayoutRevisions = new ArrayList<>();
635    
636                    long parentLayoutRevisionId =
637                            layoutRevision.getParentLayoutRevisionId();
638    
639                    while (parentLayoutRevisionId !=
640                                            LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID) {
641    
642                            LayoutRevision parentLayoutRevision =
643                                    layoutRevisionPersistence.findByPrimaryKey(
644                                            parentLayoutRevisionId);
645    
646                            if (parentLayoutRevision.isMajor()) {
647                                    break;
648                            }
649    
650                            parentLayoutRevisions.add(parentLayoutRevision);
651    
652                            parentLayoutRevisionId =
653                                    parentLayoutRevision.getParentLayoutRevisionId();
654                    }
655    
656                    layoutRevision.setParentLayoutRevisionId(parentLayoutRevisionId);
657                    layoutRevision.setMajor(true);
658    
659                    layoutRevisionPersistence.update(layoutRevision);
660    
661                    for (LayoutRevision parentLayoutRevision : parentLayoutRevisions) {
662                            List<LayoutRevision> childrenLayoutRevisions =
663                                    parentLayoutRevision.getChildren();
664    
665                            if (!childrenLayoutRevisions.isEmpty()) {
666                                    break;
667                            }
668    
669                            layoutRevisionLocalService.deleteLayoutRevision(
670                                    parentLayoutRevision);
671                    }
672    
673                    return layoutRevision;
674            }
675    
676            private static final ThreadLocal<Long> _layoutRevisionId =
677                    new AutoResetThreadLocal<>(
678                            LayoutRevisionLocalServiceImpl.class + "._layoutRevisionId", 0L);
679    
680    }