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