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.ProxyUtil;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.model.Group;
024 import com.liferay.portal.model.LayoutSet;
025 import com.liferay.portal.model.LayoutSetBranch;
026 import com.liferay.portal.model.LayoutSetStagingHandler;
027 import com.liferay.portal.model.impl.ColorSchemeImpl;
028 import com.liferay.portal.model.impl.ThemeImpl;
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 @Override
056 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
057 if (!StagingAdvicesThreadLocal.isEnabled()) {
058 return methodInvocation.proceed();
059 }
060
061 Method method = methodInvocation.getMethod();
062
063 String methodName = method.getName();
064
065 if (!_layoutSetLocalServiceStagingAdviceMethodNames.contains(
066 methodName)) {
067
068 return wrapReturnValue(methodInvocation.proceed());
069 }
070
071 Object returnValue = null;
072
073 Object thisObject = methodInvocation.getThis();
074 Object[] arguments = methodInvocation.getArguments();
075
076 if (methodName.equals("updateLayoutSetPrototypeLinkEnabled") &&
077 (arguments.length == 5)) {
078
079 updateLayoutSetPrototypeLinkEnabled(
080 (LayoutSetLocalService)thisObject, (Long)arguments[0],
081 (Boolean)arguments[1], (Boolean)arguments[2],
082 (String)arguments[3]);
083 }
084 else if (methodName.equals("updateLogo") && (arguments.length == 5)) {
085 updateLogo(
086 (LayoutSetLocalService)thisObject, (Long)arguments[0],
087 (Boolean)arguments[1], (Boolean)arguments[2],
088 (InputStream)arguments[3], (Boolean)arguments[4]);
089 }
090 else if (methodName.equals("updateLookAndFeel") &&
091 (arguments.length == 6)) {
092
093 returnValue = updateLookAndFeel(
094 (LayoutSetLocalService)thisObject, (Long)arguments[0],
095 (Boolean)arguments[1], (String)arguments[2],
096 (String)arguments[3], (String)arguments[4],
097 (Boolean)arguments[5]);
098 }
099 else if (methodName.equals("updateSettings")) {
100 returnValue = updateSettings(
101 (LayoutSetLocalService)thisObject, (Long)arguments[0],
102 (Boolean)arguments[1], (String)arguments[2]);
103 }
104 else {
105 try {
106 Class<?> clazz = getClass();
107
108 Class<?>[] parameterTypes = ArrayUtil.append(
109 new Class<?>[] {LayoutSetLocalService.class},
110 method.getParameterTypes());
111
112 Method layoutSetLocalServiceStagingAdviceMethod =
113 clazz.getMethod(methodName, parameterTypes);
114
115 arguments = ArrayUtil.append(
116 new Object[] {thisObject}, arguments);
117
118 returnValue = layoutSetLocalServiceStagingAdviceMethod.invoke(
119 this, arguments);
120 }
121 catch (InvocationTargetException ite) {
122 throw ite.getTargetException();
123 }
124 catch (NoSuchMethodException nsme) {
125 returnValue = methodInvocation.proceed();
126 }
127 }
128
129 return wrapReturnValue(returnValue);
130 }
131
132 public void updateLayoutSetPrototypeLinkEnabled(
133 LayoutSetLocalService layoutSetLocalService, long groupId,
134 boolean privateLayout, boolean layoutSetPrototypeLinkEnabled,
135 String layoutSetPrototypeUuid)
136 throws PortalException, SystemException {
137
138 LayoutSet layoutSet = layoutSetPersistence.findByG_P(
139 groupId, privateLayout);
140
141 layoutSet = wrapLayoutSet(layoutSet);
142
143 LayoutSetBranch layoutSetBranch = LayoutStagingUtil.getLayoutSetBranch(
144 layoutSet);
145
146 if (layoutSetBranch == null) {
147 layoutSetLocalService.updateLayoutSetPrototypeLinkEnabled(
148 groupId, privateLayout, layoutSetPrototypeLinkEnabled,
149 layoutSetPrototypeUuid);
150
151 return;
152 }
153
154 if (Validator.isNull(layoutSetPrototypeUuid)) {
155 layoutSetPrototypeUuid =
156 layoutSetBranch.getLayoutSetPrototypeUuid();
157 }
158
159 if (Validator.isNull(layoutSetPrototypeUuid) &&
160 layoutSetPrototypeLinkEnabled) {
161
162 throw new IllegalStateException(
163 "Cannot set layoutSetPrototypeLinkEnabled to true when " +
164 "layoutSetPrototypeUuid is null");
165 }
166
167 layoutSetBranch.setLayoutSetPrototypeLinkEnabled(
168 layoutSetPrototypeLinkEnabled);
169 layoutSetBranch.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
170
171 layoutSetBranchPersistence.update(layoutSetBranch, false);
172 }
173
174 public LayoutSet updateLogo(
175 LayoutSetLocalService layoutSetLocalService, long groupId,
176 boolean privateLayout, boolean logo, InputStream is,
177 boolean cleanUpStream)
178 throws PortalException, SystemException {
179
180 LayoutSet layoutSet = layoutSetPersistence.findByG_P(
181 groupId, privateLayout);
182
183 layoutSet = wrapLayoutSet(layoutSet);
184
185 LayoutSetBranch layoutSetBranch = LayoutStagingUtil.getLayoutSetBranch(
186 layoutSet);
187
188 if (layoutSetBranch == null) {
189 return layoutSetLocalService.updateLogo(
190 groupId, privateLayout, logo, is, cleanUpStream);
191 }
192
193 layoutSetBranch.setModifiedDate(new Date());
194 layoutSetBranch.setLogo(logo);
195
196 if (logo) {
197 long logoId = layoutSetBranch.getLogoId();
198
199 if (logoId <= 0) {
200 logoId = counterLocalService.increment();
201
202 layoutSet.setLogoId(logoId);
203 }
204 }
205 else {
206 layoutSet.setLogoId(0);
207 }
208
209 layoutSetBranchPersistence.update(layoutSetBranch, false);
210
211 if (logo) {
212 imageLocalService.updateImage(
213 layoutSetBranch.getLogoId(), is, cleanUpStream);
214 }
215 else {
216 imageLocalService.deleteImage(layoutSetBranch.getLogoId());
217 }
218
219 return layoutSet;
220 }
221
222 public LayoutSet updateLookAndFeel(
223 LayoutSetLocalService target, long groupId, boolean privateLayout,
224 String themeId, String colorSchemeId, String css, boolean wapTheme)
225 throws PortalException, SystemException {
226
227 LayoutSet layoutSet = layoutSetPersistence.findByG_P(
228 groupId, privateLayout);
229
230 layoutSet = wrapLayoutSet(layoutSet);
231
232 LayoutSetBranch layoutSetBranch = LayoutStagingUtil.getLayoutSetBranch(
233 layoutSet);
234
235 if (layoutSetBranch == null) {
236 return target.updateLookAndFeel(
237 groupId, privateLayout, themeId, colorSchemeId, css, wapTheme);
238 }
239
240 layoutSetBranch.setModifiedDate(new Date());
241
242 if (Validator.isNull(themeId)) {
243 themeId = ThemeImpl.getDefaultRegularThemeId(
244 layoutSetBranch.getCompanyId());
245 }
246
247 if (Validator.isNull(colorSchemeId)) {
248 colorSchemeId = ColorSchemeImpl.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, false);
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, false);
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 }