001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.util.ArrayUtil;
019 import com.liferay.portal.kernel.util.ClassLoaderUtil;
020 import com.liferay.portal.kernel.util.ColorSchemeFactoryUtil;
021 import com.liferay.portal.kernel.util.ProxyUtil;
022 import com.liferay.portal.kernel.util.ThemeFactoryUtil;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.model.Group;
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.service.LayoutSetLocalService;
029 import com.liferay.portal.util.PortalUtil;
030 import com.liferay.portlet.exportimport.staging.LayoutStagingUtil;
031 import com.liferay.portlet.exportimport.staging.StagingAdvicesThreadLocal;
032
033 import java.lang.reflect.InvocationTargetException;
034 import java.lang.reflect.Method;
035
036 import java.util.ArrayList;
037 import java.util.Date;
038 import java.util.HashSet;
039 import java.util.List;
040 import java.util.Set;
041
042 import org.aopalliance.intercept.MethodInterceptor;
043 import org.aopalliance.intercept.MethodInvocation;
044
045
050 public class LayoutSetLocalServiceStagingAdvice
051 extends LayoutSetLocalServiceImpl implements MethodInterceptor {
052
053 @Override
054 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
055 if (!StagingAdvicesThreadLocal.isEnabled()) {
056 return methodInvocation.proceed();
057 }
058
059 Method method = methodInvocation.getMethod();
060
061 String methodName = method.getName();
062
063 if (!_layoutSetLocalServiceStagingAdviceMethodNames.contains(
064 methodName)) {
065
066 return wrapReturnValue(methodInvocation.proceed());
067 }
068
069 Object returnValue = null;
070
071 Object thisObject = methodInvocation.getThis();
072 Object[] arguments = methodInvocation.getArguments();
073
074 if (methodName.equals("updateLayoutSetPrototypeLinkEnabled") &&
075 (arguments.length == 5)) {
076
077 updateLayoutSetPrototypeLinkEnabled(
078 (LayoutSetLocalService)thisObject, (Long)arguments[0],
079 (Boolean)arguments[1], (Boolean)arguments[2],
080 (String)arguments[3]);
081 }
082 else if (methodName.equals("updateLogo") && (arguments.length == 4)) {
083 returnValue = updateLogo(
084 (LayoutSetLocalService)thisObject, (Long)arguments[0],
085 (Boolean)arguments[1], (Boolean)arguments[2],
086 (byte[])arguments[3]);
087 }
088 else if (methodName.equals("updateLookAndFeel") &&
089 (arguments.length == 6)) {
090
091 returnValue = updateLookAndFeel(
092 (LayoutSetLocalService)thisObject, (Long)arguments[0],
093 (Boolean)arguments[1], (String)arguments[2],
094 (String)arguments[3], (String)arguments[4],
095 (Boolean)arguments[5]);
096 }
097 else if (methodName.equals("updateSettings")) {
098 returnValue = updateSettings(
099 (LayoutSetLocalService)thisObject, (Long)arguments[0],
100 (Boolean)arguments[1], (String)arguments[2]);
101 }
102 else {
103 try {
104 Class<?> clazz = getClass();
105
106 Class<?>[] parameterTypes = ArrayUtil.append(
107 new Class<?>[] {LayoutSetLocalService.class},
108 method.getParameterTypes());
109
110 Method layoutSetLocalServiceStagingAdviceMethod =
111 clazz.getMethod(methodName, parameterTypes);
112
113 arguments = ArrayUtil.append(
114 new Object[] {thisObject}, arguments);
115
116 returnValue = layoutSetLocalServiceStagingAdviceMethod.invoke(
117 this, arguments);
118 }
119 catch (InvocationTargetException ite) {
120 throw ite.getTargetException();
121 }
122 catch (NoSuchMethodException nsme) {
123 returnValue = methodInvocation.proceed();
124 }
125 }
126
127 return wrapReturnValue(returnValue);
128 }
129
130 public void updateLayoutSetPrototypeLinkEnabled(
131 LayoutSetLocalService layoutSetLocalService, long groupId,
132 boolean privateLayout, boolean layoutSetPrototypeLinkEnabled,
133 String layoutSetPrototypeUuid)
134 throws PortalException {
135
136 LayoutSet layoutSet = layoutSetPersistence.findByG_P(
137 groupId, privateLayout);
138
139 layoutSet = wrapLayoutSet(layoutSet);
140
141 LayoutSetBranch layoutSetBranch = LayoutStagingUtil.getLayoutSetBranch(
142 layoutSet);
143
144 if (layoutSetBranch == null) {
145 layoutSetLocalService.updateLayoutSetPrototypeLinkEnabled(
146 groupId, privateLayout, layoutSetPrototypeLinkEnabled,
147 layoutSetPrototypeUuid);
148
149 return;
150 }
151
152 if (Validator.isNull(layoutSetPrototypeUuid)) {
153 layoutSetPrototypeUuid =
154 layoutSetBranch.getLayoutSetPrototypeUuid();
155 }
156
157 if (Validator.isNull(layoutSetPrototypeUuid) &&
158 layoutSetPrototypeLinkEnabled) {
159
160 throw new IllegalStateException(
161 "Cannot set layoutSetPrototypeLinkEnabled to true when " +
162 "layoutSetPrototypeUuid is null");
163 }
164
165 layoutSetBranch.setLayoutSetPrototypeLinkEnabled(
166 layoutSetPrototypeLinkEnabled);
167 layoutSetBranch.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
168
169 layoutSetBranchPersistence.update(layoutSetBranch);
170 }
171
172 public LayoutSet updateLogo(
173 LayoutSetLocalService layoutSetLocalService, long groupId,
174 boolean privateLayout, boolean logo, byte[] logoBytes)
175 throws PortalException {
176
177 LayoutSet layoutSet = layoutSetPersistence.findByG_P(
178 groupId, privateLayout);
179
180 layoutSet = wrapLayoutSet(layoutSet);
181
182 LayoutSetBranch layoutSetBranch = LayoutStagingUtil.getLayoutSetBranch(
183 layoutSet);
184
185 if (layoutSetBranch == null) {
186 return layoutSetLocalService.updateLogo(
187 groupId, privateLayout, logo, logoBytes);
188 }
189
190 layoutSetBranch.setModifiedDate(new Date());
191
192 PortalUtil.updateImageId(
193 layoutSetBranch, logo, logoBytes, "logoId", 0, 0, 0);
194
195 layoutSetBranchPersistence.update(layoutSetBranch);
196
197 return layoutSet;
198 }
199
200 public LayoutSet updateLookAndFeel(
201 LayoutSetLocalService target, long groupId, boolean privateLayout,
202 String themeId, String colorSchemeId, String css, boolean wapTheme)
203 throws PortalException {
204
205 LayoutSet layoutSet = layoutSetPersistence.findByG_P(
206 groupId, privateLayout);
207
208 layoutSet = wrapLayoutSet(layoutSet);
209
210 LayoutSetBranch layoutSetBranch = LayoutStagingUtil.getLayoutSetBranch(
211 layoutSet);
212
213 if (layoutSetBranch == null) {
214 return target.updateLookAndFeel(
215 groupId, privateLayout, themeId, colorSchemeId, css, wapTheme);
216 }
217
218 layoutSetBranch.setModifiedDate(new Date());
219
220 if (Validator.isNull(themeId)) {
221 themeId = ThemeFactoryUtil.getDefaultRegularThemeId(
222 layoutSetBranch.getCompanyId());
223 }
224
225 if (Validator.isNull(colorSchemeId)) {
226 colorSchemeId =
227 ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId();
228 }
229
230 if (wapTheme) {
231 layoutSetBranch.setWapThemeId(themeId);
232 layoutSetBranch.setWapColorSchemeId(colorSchemeId);
233 }
234 else {
235 layoutSetBranch.setThemeId(themeId);
236 layoutSetBranch.setColorSchemeId(colorSchemeId);
237 layoutSetBranch.setCss(css);
238 }
239
240 layoutSetBranchPersistence.update(layoutSetBranch);
241
242 return layoutSet;
243 }
244
245 public LayoutSet updateSettings(
246 LayoutSetLocalService target, long groupId, boolean privateLayout,
247 String settings)
248 throws PortalException {
249
250 LayoutSet layoutSet = layoutSetPersistence.findByG_P(
251 groupId, privateLayout);
252
253 layoutSet = wrapLayoutSet(layoutSet);
254
255 LayoutSetBranch layoutSetBranch = LayoutStagingUtil.getLayoutSetBranch(
256 layoutSet);
257
258 if (layoutSetBranch == null) {
259 return target.updateSettings(groupId, privateLayout, settings);
260 }
261
262 layoutSetBranch.setModifiedDate(new Date());
263 layoutSetBranch.setSettings(settings);
264
265 layoutSetBranchPersistence.update(layoutSetBranch);
266
267 return layoutSet;
268 }
269
270 protected LayoutSet unwrapLayoutSet(LayoutSet layoutSet) {
271 LayoutSetStagingHandler layoutSetStagingHandler =
272 LayoutStagingUtil.getLayoutSetStagingHandler(layoutSet);
273
274 if (layoutSetStagingHandler == null) {
275 return layoutSet;
276 }
277
278 return layoutSetStagingHandler.getLayoutSet();
279 }
280
281 protected LayoutSet wrapLayoutSet(LayoutSet layoutSet) {
282 LayoutSetStagingHandler layoutSetStagingHandler =
283 LayoutStagingUtil.getLayoutSetStagingHandler(layoutSet);
284
285 if (layoutSetStagingHandler != null) {
286 return layoutSet;
287 }
288
289 Group group = null;
290
291 try {
292 group = layoutSet.getGroup();
293 }
294 catch (Exception e) {
295 return layoutSet;
296 }
297
298 if (!LayoutStagingUtil.isBranchingLayoutSet(
299 group, layoutSet.getPrivateLayout())) {
300
301 return layoutSet;
302 }
303
304 return (LayoutSet)ProxyUtil.newProxyInstance(
305 ClassLoaderUtil.getPortalClassLoader(),
306 new Class[] {LayoutSet.class},
307 new LayoutSetStagingHandler(layoutSet));
308 }
309
310 protected List<LayoutSet> wrapLayoutSets(List<LayoutSet> layoutSets) {
311 if (layoutSets.isEmpty()) {
312 return layoutSets;
313 }
314
315 List<LayoutSet> wrappedLayoutSets = new ArrayList<>(layoutSets.size());
316
317 for (int i = 0; i < layoutSets.size(); i++) {
318 LayoutSet wrappedLayoutSet = wrapLayoutSet(layoutSets.get(i));
319
320 wrappedLayoutSets.add(wrappedLayoutSet);
321 }
322
323 return wrappedLayoutSets;
324 }
325
326 protected Object wrapReturnValue(Object returnValue) {
327 if (returnValue instanceof LayoutSet) {
328 returnValue = wrapLayoutSet((LayoutSet)returnValue);
329 }
330 else if (returnValue instanceof List<?>) {
331 List<?> list = (List<?>)returnValue;
332
333 if (!list.isEmpty() && (list.get(0) instanceof LayoutSet)) {
334 returnValue = wrapLayoutSets((List<LayoutSet>)returnValue);
335 }
336 }
337
338 return returnValue;
339 }
340
341 private static final Set<String>
342 _layoutSetLocalServiceStagingAdviceMethodNames = new HashSet<>();
343
344 static {
345 _layoutSetLocalServiceStagingAdviceMethodNames.add(
346 "updateLayoutSetPrototypeLinkEnabled");
347 _layoutSetLocalServiceStagingAdviceMethodNames.add("updateLogo");
348 _layoutSetLocalServiceStagingAdviceMethodNames.add("updateLookAndFeel");
349 _layoutSetLocalServiceStagingAdviceMethodNames.add("updateSettings");
350 }
351
352 }