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