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