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