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