001    /**
002     * Copyright (c) 2000-2012 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.model;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
023    import com.liferay.portal.kernel.util.ProxyUtil;
024    import com.liferay.portal.kernel.util.ReflectionUtil;
025    import com.liferay.portal.service.LayoutSetBranchLocalServiceUtil;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.ServiceContextThreadLocal;
028    
029    import java.io.Serializable;
030    
031    import java.lang.reflect.InvocationHandler;
032    import java.lang.reflect.InvocationTargetException;
033    import java.lang.reflect.Method;
034    
035    import java.util.HashSet;
036    import java.util.Set;
037    
038    /**
039     * @author Julio Camarero
040     * @author Brian Wing Shun Chan
041     */
042    public class LayoutSetStagingHandler
043            implements InvocationHandler, Serializable {
044    
045            public LayoutSetStagingHandler(LayoutSet layoutSet) {
046                    _layoutSet = layoutSet;
047    
048                    try {
049                            _layoutSetBranch = _getLayoutSetBranch(layoutSet);
050                    }
051                    catch (Exception e) {
052                            _log.error(e, e);
053    
054                            throw new IllegalStateException(e);
055                    }
056            }
057    
058            public LayoutSet getLayoutSet() {
059                    return _layoutSet;
060            }
061    
062            public LayoutSetBranch getLayoutSetBranch() {
063                    return _layoutSetBranch;
064            }
065    
066            public Object invoke(Object proxy, Method method, Object[] arguments)
067                    throws Throwable {
068    
069                    try {
070                            if (_layoutSetBranch == null) {
071                                    return method.invoke(_layoutSet, arguments);
072                            }
073    
074                            String methodName = method.getName();
075    
076                            if (methodName.equals("toEscapedModel")) {
077                                    if (_layoutSet.isEscapedModel()) {
078                                            return this;
079                                    }
080    
081                                    return _toEscapedModel();
082                            }
083    
084                            if (methodName.equals("clone")) {
085                                    return _clone();
086                            }
087    
088                            Object bean = _layoutSet;
089    
090                            if (_layoutSetBranchMethodNames.contains(methodName)) {
091                                    try {
092                                            Class<?> layoutSetBranchClass = _layoutSetBranch.getClass();
093    
094                                            method = layoutSetBranchClass.getMethod(
095                                                    methodName,
096                                                    ReflectionUtil.getParameterTypes(arguments));
097    
098                                            bean = _layoutSetBranch;
099                                    }
100                                    catch (NoSuchMethodException nsme) {
101                                            _log.error(nsme, nsme);
102                                    }
103                            }
104    
105                            return method.invoke(bean, arguments);
106                    }
107                    catch (InvocationTargetException ite) {
108                            throw ite.getTargetException();
109                    }
110            }
111    
112            public void setLayoutSetBranch(LayoutSetBranch layoutSetBranch) {
113                    _layoutSetBranch = layoutSetBranch;
114            }
115    
116            private Object _clone() {
117                    return ProxyUtil.newProxyInstance(
118                            PortalClassLoaderUtil.getClassLoader(), new Class[] {Layout.class},
119                            new LayoutSetStagingHandler(_layoutSet));
120            }
121    
122            private LayoutSetBranch _getLayoutSetBranch(LayoutSet layoutSet)
123                    throws PortalException, SystemException {
124    
125                    ServiceContext serviceContext =
126                            ServiceContextThreadLocal.getServiceContext();
127    
128                    if ((serviceContext == null) || !serviceContext.isSignedIn()) {
129                            return null;
130                    }
131    
132                    long layoutSetBranchId = ParamUtil.getLong(
133                            serviceContext, "layoutSetBranchId");
134    
135                    return LayoutSetBranchLocalServiceUtil.getUserLayoutSetBranch(
136                            serviceContext.getUserId(), layoutSet.getGroupId(),
137                            layoutSet.isPrivateLayout(), layoutSet.getLayoutSetId(),
138                            layoutSetBranchId);
139            }
140    
141            private Object _toEscapedModel() {
142                    return ProxyUtil.newProxyInstance(
143                            PortalClassLoaderUtil.getClassLoader(), new Class[] {Layout.class},
144                            new LayoutSetStagingHandler(_layoutSet.toEscapedModel()));
145            }
146    
147            private static Log _log = LogFactoryUtil.getLog(
148                    LayoutSetStagingHandler.class);
149    
150            private static Set<String> _layoutSetBranchMethodNames =
151                    new HashSet<String>();
152    
153            static {
154                    _layoutSetBranchMethodNames.add("getColorScheme");
155                    _layoutSetBranchMethodNames.add("getColorSchemeId");
156                    _layoutSetBranchMethodNames.add("getCss");
157                    _layoutSetBranchMethodNames.add("getLayoutSetPrototypeLinkEnabled");
158                    _layoutSetBranchMethodNames.add("getLayoutSetPrototypeUuid");
159                    _layoutSetBranchMethodNames.add("getLogo");
160                    _layoutSetBranchMethodNames.add("getLogoId");
161                    _layoutSetBranchMethodNames.add("getSettings");
162                    _layoutSetBranchMethodNames.add("getTheme");
163                    _layoutSetBranchMethodNames.add("getThemeId");
164                    _layoutSetBranchMethodNames.add("getWapColorScheme");
165                    _layoutSetBranchMethodNames.add("getWapColorSchemeId");
166                    _layoutSetBranchMethodNames.add("getWapTheme");
167                    _layoutSetBranchMethodNames.add("getWapThemeId");
168                    _layoutSetBranchMethodNames.add("getSettingsProperties");
169                    _layoutSetBranchMethodNames.add("getSettings");
170                    _layoutSetBranchMethodNames.add("getStagingLogoId");
171                    _layoutSetBranchMethodNames.add("getThemeSetting");
172                    _layoutSetBranchMethodNames.add("getSettingsProperty");
173                    _layoutSetBranchMethodNames.add("isLayoutSetPrototypeLinkActive");
174                    _layoutSetBranchMethodNames.add("isEscapedModel");
175                    _layoutSetBranchMethodNames.add("isLogo");
176                    _layoutSetBranchMethodNames.add("setColorSchemeId");
177                    _layoutSetBranchMethodNames.add("setCss");
178                    _layoutSetBranchMethodNames.add("setLayoutSetPrototypeLinkEnabled");
179                    _layoutSetBranchMethodNames.add("setLayoutSetPrototypeUuid");
180                    _layoutSetBranchMethodNames.add("setEscapedModel");
181                    _layoutSetBranchMethodNames.add("setLogo");
182                    _layoutSetBranchMethodNames.add("setLogoId");
183                    _layoutSetBranchMethodNames.add("setSettings");
184                    _layoutSetBranchMethodNames.add("setSettingsProperties");
185                    _layoutSetBranchMethodNames.add("setThemeId");
186                    _layoutSetBranchMethodNames.add("setWapColorSchemeId");
187                    _layoutSetBranchMethodNames.add("setWapThemeId");
188            }
189    
190            private LayoutSet _layoutSet;
191            private LayoutSetBranch _layoutSetBranch;
192    
193    }