001    /**
002     * Copyright (c) 2000-2012 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.staging;
016    
017    import com.liferay.portal.kernel.staging.LayoutStaging;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ProxyUtil;
020    import com.liferay.portal.kernel.util.UnicodeProperties;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.LayoutRevision;
024    import com.liferay.portal.model.LayoutSet;
025    import com.liferay.portal.model.LayoutSetBranch;
026    import com.liferay.portal.model.LayoutSetStagingHandler;
027    import com.liferay.portal.model.LayoutStagingHandler;
028    
029    import java.lang.reflect.InvocationHandler;
030    
031    /**
032     * @author Raymond Augé
033     */
034    public class LayoutStagingImpl implements LayoutStaging {
035    
036            public LayoutRevision getLayoutRevision(Layout layout) {
037                    LayoutStagingHandler layoutStagingHandler = getLayoutStagingHandler(
038                            layout);
039    
040                    if (layoutStagingHandler == null) {
041                            return null;
042                    }
043    
044                    return layoutStagingHandler.getLayoutRevision();
045            }
046    
047            public LayoutSetBranch getLayoutSetBranch(LayoutSet layoutSet) {
048                    LayoutSetStagingHandler layoutSetStagingHandler =
049                            getLayoutSetStagingHandler(layoutSet);
050    
051                    if (layoutSetStagingHandler == null) {
052                            return null;
053                    }
054    
055                    return layoutSetStagingHandler.getLayoutSetBranch();
056            }
057    
058            public LayoutSetStagingHandler getLayoutSetStagingHandler(
059                    LayoutSet layoutSet) {
060    
061                    if (!ProxyUtil.isProxyClass(layoutSet.getClass())) {
062                            return null;
063                    }
064    
065                    InvocationHandler invocationHandler = ProxyUtil.getInvocationHandler(
066                            layoutSet);
067    
068                    if (!(invocationHandler instanceof LayoutSetStagingHandler)) {
069                            return null;
070                    }
071    
072                    return (LayoutSetStagingHandler)invocationHandler;
073            }
074    
075            public LayoutStagingHandler getLayoutStagingHandler(Layout layout) {
076                    if (!ProxyUtil.isProxyClass(layout.getClass())) {
077                            return null;
078                    }
079    
080                    InvocationHandler invocationHandler = ProxyUtil.getInvocationHandler(
081                            layout);
082    
083                    if (!(invocationHandler instanceof LayoutStagingHandler)) {
084                            return null;
085                    }
086    
087                    return (LayoutStagingHandler)invocationHandler;
088            }
089    
090            public boolean isBranchingLayout(Layout layout) {
091                    try {
092                            return isBranchingLayoutSet(
093                                    layout.getGroup(), layout.isPrivateLayout());
094                    }
095                    catch (Exception e) {
096                            throw new IllegalStateException(e);
097                    }
098            }
099    
100            public boolean isBranchingLayoutSet(Group group, boolean privateLayout) {
101                    boolean isStagingGroup = false;
102    
103                    if (group.isStagingGroup()) {
104                            isStagingGroup = true;
105    
106                            group = group.getLiveGroup();
107                    }
108    
109                    UnicodeProperties typeSettingsProperties =
110                            group.getTypeSettingsProperties();
111    
112                    boolean branchingEnabled = false;
113    
114                    if (privateLayout) {
115                            branchingEnabled = GetterUtil.getBoolean(
116                                    typeSettingsProperties.getProperty("branchingPrivate"));
117                    }
118                    else {
119                            branchingEnabled = GetterUtil.getBoolean(
120                                    typeSettingsProperties.getProperty("branchingPublic"));
121                    }
122    
123                    if (group.isStaged() && branchingEnabled) {
124                            if (!group.isStagedRemotely() && !isStagingGroup) {
125                                    return false;
126                            }
127    
128                            return true;
129                    }
130    
131                    return false;
132            }
133    
134    }