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