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