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