001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.staging.LayoutStagingUtil;
020    import com.liferay.portal.kernel.util.ProxyUtil;
021    import com.liferay.portal.model.LayoutSet;
022    import com.liferay.portal.model.LayoutSetStagingHandler;
023    import com.liferay.portal.staging.StagingAdvicesThreadLocal;
024    import com.liferay.portal.util.ClassLoaderUtil;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    import org.aopalliance.intercept.MethodInterceptor;
030    import org.aopalliance.intercept.MethodInvocation;
031    
032    /**
033     * @author Julio Camarero
034     * @author Brian Wing Shun Chan
035     * @author Raymond Aug??
036     */
037    public class LayoutSetLocalServiceStagingAdvice implements MethodInterceptor {
038    
039            @Override
040            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
041                    Object returnValue = methodInvocation.proceed();
042    
043                    if (!StagingAdvicesThreadLocal.isEnabled()) {
044                            return returnValue;
045                    }
046    
047                    if (returnValue instanceof LayoutSet) {
048                            return wrapLayoutSet((LayoutSet)returnValue);
049                    }
050    
051                    if (returnValue instanceof List<?>) {
052                            List<?> list = (List<?>)returnValue;
053    
054                            if (!list.isEmpty() && (list.get(0) instanceof LayoutSet)) {
055                                    returnValue = wrapLayoutSets((List<LayoutSet>)returnValue);
056                            }
057                    }
058    
059                    return returnValue;
060            }
061    
062            protected LayoutSet wrapLayoutSet(LayoutSet layoutSet)
063                    throws SystemException {
064    
065                    LayoutSetStagingHandler layoutSetStagingHandler =
066                            LayoutStagingUtil.getLayoutSetStagingHandler(layoutSet);
067    
068                    if (layoutSetStagingHandler != null) {
069                            return layoutSet;
070                    }
071    
072                    try {
073                            if (!LayoutStagingUtil.isBranchingLayoutSet(
074                                            layoutSet.getGroup(), layoutSet.getPrivateLayout())) {
075    
076                                    return layoutSet;
077                            }
078                    }
079                    catch (PortalException pe) {
080                            return layoutSet;
081                    }
082    
083                    return (LayoutSet)ProxyUtil.newProxyInstance(
084                            ClassLoaderUtil.getPortalClassLoader(),
085                            new Class<?>[] {LayoutSet.class},
086                            new LayoutSetStagingHandler(layoutSet));
087            }
088    
089            protected List<LayoutSet> wrapLayoutSets(List<LayoutSet> layoutSets)
090                    throws SystemException {
091    
092                    if (layoutSets.isEmpty()) {
093                            return layoutSets;
094                    }
095    
096                    List<LayoutSet> wrappedLayoutSets = new ArrayList<LayoutSet>(
097                            layoutSets.size());
098    
099                    for (LayoutSet layoutSet : layoutSets) {
100                            wrappedLayoutSets.add(wrapLayoutSet(layoutSet));
101                    }
102    
103                    return wrappedLayoutSets;
104            }
105    
106    }