001    /**
002     * Copyright (c) 2000-2011 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.LayoutStagingHandler;
025    
026    import java.lang.reflect.InvocationHandler;
027    
028    /**
029     * @author Raymond Augé
030     */
031    public class LayoutStagingImpl implements LayoutStaging {
032    
033            public LayoutRevision getLayoutRevision(Layout layout) {
034                    LayoutStagingHandler layoutStagingHandler = getLayoutStagingHandler(
035                            layout);
036    
037                    if (layoutStagingHandler == null) {
038                            return null;
039                    }
040    
041                    return layoutStagingHandler.getLayoutRevision();
042            }
043    
044            public LayoutStagingHandler getLayoutStagingHandler(Layout layout) {
045                    if (!ProxyUtil.isProxyClass(layout.getClass())) {
046                            return null;
047                    }
048    
049                    InvocationHandler invocationHandler = ProxyUtil.getInvocationHandler(
050                            layout);
051    
052                    if (!(invocationHandler instanceof LayoutStagingHandler)) {
053                            return null;
054                    }
055    
056                    return (LayoutStagingHandler)invocationHandler;
057            }
058    
059            public boolean isBranchingLayout(Layout layout) {
060                    try {
061                            return isBranchingLayoutSet(
062                                    layout.getGroup(), layout.isPrivateLayout());
063                    }
064                    catch (Exception e) {
065                            throw new IllegalStateException(e);
066                    }
067            }
068    
069            public boolean isBranchingLayoutSet(Group group, boolean privateLayout) {
070                    boolean isStagingGroup = false;
071    
072                    if (group.isStagingGroup()) {
073                            isStagingGroup = true;
074    
075                            group = group.getLiveGroup();
076                    }
077    
078                    UnicodeProperties typeSettingsProperties =
079                            group.getTypeSettingsProperties();
080    
081                    boolean branchingEnabled = false;
082    
083                    if (privateLayout) {
084                            branchingEnabled = GetterUtil.getBoolean(
085                                    typeSettingsProperties.getProperty("branchingPrivate"));
086                    }
087                    else {
088                            branchingEnabled = GetterUtil.getBoolean(
089                                    typeSettingsProperties.getProperty("branchingPublic"));
090                    }
091    
092                    if (group.isStaged() && branchingEnabled) {
093                            if (!group.isStagedRemotely() && !isStagingGroup) {
094                                    return false;
095                            }
096    
097                            return true;
098                    }
099    
100                    return false;
101            }
102    
103    }