001
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
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 @Override
067 public Object invoke(Object proxy, Method method, Object[] arguments)
068 throws Throwable {
069
070 try {
071 if (_layoutSetBranch == null) {
072 return method.invoke(_layoutSet, arguments);
073 }
074
075 String methodName = method.getName();
076
077 if (methodName.equals("toEscapedModel")) {
078 if (_layoutSet.isEscapedModel()) {
079 return this;
080 }
081
082 return _toEscapedModel();
083 }
084
085 if (methodName.equals("clone")) {
086 return _clone();
087 }
088
089 Object bean = _layoutSet;
090
091 if (_layoutSetBranchMethodNames.contains(methodName)) {
092 try {
093 Class<?> layoutSetBranchClass = _layoutSetBranch.getClass();
094
095 method = layoutSetBranchClass.getMethod(
096 methodName,
097 ReflectionUtil.getParameterTypes(arguments));
098
099 bean = _layoutSetBranch;
100 }
101 catch (NoSuchMethodException nsme) {
102 _log.error(nsme, nsme);
103 }
104 }
105
106 return method.invoke(bean, arguments);
107 }
108 catch (InvocationTargetException ite) {
109 throw ite.getTargetException();
110 }
111 }
112
113 public void setLayoutSetBranch(LayoutSetBranch layoutSetBranch) {
114 _layoutSetBranch = layoutSetBranch;
115 }
116
117 private Object _clone() {
118 return ProxyUtil.newProxyInstance(
119 PortalClassLoaderUtil.getClassLoader(), new Class[] {Layout.class},
120 new LayoutSetStagingHandler(_layoutSet));
121 }
122
123 private LayoutSetBranch _getLayoutSetBranch(LayoutSet layoutSet)
124 throws PortalException, SystemException {
125
126 ServiceContext serviceContext =
127 ServiceContextThreadLocal.getServiceContext();
128
129 if ((serviceContext == null) || !serviceContext.isSignedIn()) {
130 return null;
131 }
132
133 long layoutSetBranchId = ParamUtil.getLong(
134 serviceContext, "layoutSetBranchId");
135
136 return LayoutSetBranchLocalServiceUtil.getUserLayoutSetBranch(
137 serviceContext.getUserId(), layoutSet.getGroupId(),
138 layoutSet.isPrivateLayout(), layoutSet.getLayoutSetId(),
139 layoutSetBranchId);
140 }
141
142 private Object _toEscapedModel() {
143 return ProxyUtil.newProxyInstance(
144 PortalClassLoaderUtil.getClassLoader(), new Class[] {Layout.class},
145 new LayoutSetStagingHandler(_layoutSet.toEscapedModel()));
146 }
147
148 private static Log _log = LogFactoryUtil.getLog(
149 LayoutSetStagingHandler.class);
150
151 private static Set<String> _layoutSetBranchMethodNames =
152 new HashSet<String>();
153
154 static {
155 _layoutSetBranchMethodNames.add("getColorScheme");
156 _layoutSetBranchMethodNames.add("getColorSchemeId");
157 _layoutSetBranchMethodNames.add("getCss");
158 _layoutSetBranchMethodNames.add("getLayoutSetPrototypeLinkEnabled");
159 _layoutSetBranchMethodNames.add("getLayoutSetPrototypeUuid");
160 _layoutSetBranchMethodNames.add("getLogo");
161 _layoutSetBranchMethodNames.add("getLogoId");
162 _layoutSetBranchMethodNames.add("getSettings");
163 _layoutSetBranchMethodNames.add("getTheme");
164 _layoutSetBranchMethodNames.add("getThemeId");
165 _layoutSetBranchMethodNames.add("getWapColorScheme");
166 _layoutSetBranchMethodNames.add("getWapColorSchemeId");
167 _layoutSetBranchMethodNames.add("getWapTheme");
168 _layoutSetBranchMethodNames.add("getWapThemeId");
169 _layoutSetBranchMethodNames.add("getSettingsProperties");
170 _layoutSetBranchMethodNames.add("getSettings");
171 _layoutSetBranchMethodNames.add("getStagingLogoId");
172 _layoutSetBranchMethodNames.add("getThemeSetting");
173 _layoutSetBranchMethodNames.add("getSettingsProperty");
174 _layoutSetBranchMethodNames.add("isLayoutSetPrototypeLinkActive");
175 _layoutSetBranchMethodNames.add("isEscapedModel");
176 _layoutSetBranchMethodNames.add("isLogo");
177 _layoutSetBranchMethodNames.add("setColorSchemeId");
178 _layoutSetBranchMethodNames.add("setCss");
179 _layoutSetBranchMethodNames.add("setLayoutSetPrototypeLinkEnabled");
180 _layoutSetBranchMethodNames.add("setLayoutSetPrototypeUuid");
181 _layoutSetBranchMethodNames.add("setEscapedModel");
182 _layoutSetBranchMethodNames.add("setLogo");
183 _layoutSetBranchMethodNames.add("setLogoId");
184 _layoutSetBranchMethodNames.add("setSettings");
185 _layoutSetBranchMethodNames.add("setSettingsProperties");
186 _layoutSetBranchMethodNames.add("setThemeId");
187 _layoutSetBranchMethodNames.add("setWapColorSchemeId");
188 _layoutSetBranchMethodNames.add("setWapThemeId");
189 }
190
191 private LayoutSet _layoutSet;
192 private LayoutSetBranch _layoutSetBranch;
193
194 }