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            @Override
039            public LayoutRevision getLayoutRevision(Layout layout) {
040                    LayoutStagingHandler layoutStagingHandler = getLayoutStagingHandler(
041                            layout);
042    
043                    if (layoutStagingHandler == null) {
044                            return null;
045                    }
046    
047                    return layoutStagingHandler.getLayoutRevision();
048            }
049    
050            @Override
051            public LayoutSetBranch getLayoutSetBranch(LayoutSet layoutSet) {
052                    LayoutSetStagingHandler layoutSetStagingHandler =
053                            getLayoutSetStagingHandler(layoutSet);
054    
055                    if (layoutSetStagingHandler == null) {
056                            return null;
057                    }
058    
059                    return layoutSetStagingHandler.getLayoutSetBranch();
060            }
061    
062            @Override
063            public LayoutSetStagingHandler getLayoutSetStagingHandler(
064                    LayoutSet layoutSet) {
065    
066                    if (!ProxyUtil.isProxyClass(layoutSet.getClass())) {
067                            return null;
068                    }
069    
070                    InvocationHandler invocationHandler = ProxyUtil.getInvocationHandler(
071                            layoutSet);
072    
073                    if (!(invocationHandler instanceof LayoutSetStagingHandler)) {
074                            return null;
075                    }
076    
077                    return (LayoutSetStagingHandler)invocationHandler;
078            }
079    
080            @Override
081            public LayoutStagingHandler getLayoutStagingHandler(Layout layout) {
082                    if (!ProxyUtil.isProxyClass(layout.getClass())) {
083                            return null;
084                    }
085    
086                    InvocationHandler invocationHandler = ProxyUtil.getInvocationHandler(
087                            layout);
088    
089                    if (!(invocationHandler instanceof LayoutStagingHandler)) {
090                            return null;
091                    }
092    
093                    return (LayoutStagingHandler)invocationHandler;
094            }
095    
096            @Override
097            public boolean isBranchingLayout(Layout layout) {
098                    try {
099                            return isBranchingLayoutSet(
100                                    layout.getGroup(), layout.isPrivateLayout());
101                    }
102                    catch (Exception e) {
103                            throw new IllegalStateException(e);
104                    }
105            }
106    
107            @Override
108            public boolean isBranchingLayoutSet(Group group, boolean privateLayout) {
109                    boolean isStagingGroup = false;
110    
111                    if (group.isStagingGroup()) {
112                            isStagingGroup = true;
113    
114                            group = group.getLiveGroup();
115                    }
116    
117                    UnicodeProperties typeSettingsProperties =
118                            group.getTypeSettingsProperties();
119    
120                    if (typeSettingsProperties.isEmpty()) {
121                            return false;
122                    }
123    
124                    boolean branchingEnabled = false;
125    
126                    if (privateLayout) {
127                            branchingEnabled = GetterUtil.getBoolean(
128                                    typeSettingsProperties.getProperty("branchingPrivate"));
129                    }
130                    else {
131                            branchingEnabled = GetterUtil.getBoolean(
132                                    typeSettingsProperties.getProperty("branchingPublic"));
133                    }
134    
135                    if (branchingEnabled && group.isStaged()) {
136                            if (!isStagingGroup && !group.isStagedRemotely()) {
137                                    return false;
138                            }
139    
140                            return true;
141                    }
142    
143                    return false;
144            }
145    
146    }