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