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.staging.LayoutStagingUtil;
018    import com.liferay.portal.kernel.staging.MergeLayoutPrototypesThreadLocal;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.model.LayoutRevision;
023    import com.liferay.portal.model.PortletPreferencesIds;
024    import com.liferay.portal.service.LayoutLocalServiceUtil;
025    import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.ServiceContextThreadLocal;
028    import com.liferay.portal.service.persistence.LayoutRevisionUtil;
029    
030    import java.lang.reflect.InvocationTargetException;
031    import java.lang.reflect.Method;
032    
033    import org.aopalliance.intercept.MethodInterceptor;
034    import org.aopalliance.intercept.MethodInvocation;
035    
036    /**
037     * @author Raymond Augé
038     */
039    public class PortletPreferencesLocalServiceStagingAdvice
040            extends LayoutLocalServiceImpl implements MethodInterceptor {
041    
042            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
043                    try {
044                            Object[] arguments = methodInvocation.getArguments();
045    
046                            if (arguments == null) {
047                                    return methodInvocation.proceed();
048                            }
049    
050                            Method method = methodInvocation.getMethod();
051    
052                            String methodName = method.getName();
053    
054                            if (methodName.equals("getPortletPreferences") &&
055                                    (arguments.length == 4)) {
056    
057                                    return getPortletPreferences(methodInvocation);
058                            }
059                            else if (methodName.equals("getPreferences")) {
060                                    return getPreferences(methodInvocation);
061                            }
062                            else if (methodName.equals("getStrictPreferences")) {
063                                    return getPreferences(methodInvocation);
064                            }
065                            else if (methodName.equals("updatePreferences")) {
066                                    return updatePreferences(methodInvocation);
067                            }
068    
069                            return methodInvocation.proceed();
070                    }
071                    catch (InvocationTargetException ite) {
072                            throw ite.getCause();
073                    }
074                    catch (Throwable throwable) {
075                            throw throwable;
076                    }
077            }
078    
079            protected Object getPortletPreferences(MethodInvocation methodInvocation)
080                    throws Throwable {
081    
082                    Method method = methodInvocation.getMethod();
083                    Object[] arguments = methodInvocation.getArguments();
084    
085                    long plid = (Long)arguments[2];
086    
087                    if (plid <= 0) {
088                            return methodInvocation.proceed();
089                    }
090    
091                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
092    
093                    if (!LayoutStagingUtil.isBranchingLayout(layout)) {
094                            return methodInvocation.proceed();
095                    }
096    
097                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
098                            layout);
099    
100                    arguments[2] = layoutRevision.getLayoutRevisionId();
101    
102                    return method.invoke(methodInvocation.getThis(), arguments);
103            }
104    
105            protected Object getPreferences(MethodInvocation methodInvocation)
106                    throws Throwable {
107    
108                    Method method = methodInvocation.getMethod();
109                    Object[] arguments = methodInvocation.getArguments();
110    
111                    long plid = 0;
112    
113                    if (arguments.length == 1) {
114                            PortletPreferencesIds portletPreferencesIds =
115                                    (PortletPreferencesIds)arguments[0];
116    
117                            plid = portletPreferencesIds.getPlid();
118                    }
119                    else {
120                            plid = (Long)arguments[3];
121                    }
122    
123                    if (plid <= 0) {
124                            return methodInvocation.proceed();
125                    }
126    
127                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
128    
129                    if (!LayoutStagingUtil.isBranchingLayout(layout)) {
130                            return methodInvocation.proceed();
131                    }
132    
133                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
134                            layout);
135    
136                    plid = layoutRevision.getLayoutRevisionId();
137    
138                    if (arguments.length == 1) {
139                            PortletPreferencesIds portletPreferencesIds =
140                                    (PortletPreferencesIds)arguments[0];
141    
142                            portletPreferencesIds.setPlid(plid);
143                    }
144                    else {
145                            arguments[3] = plid;
146                    }
147    
148                    return method.invoke(methodInvocation.getThis(), arguments);
149            }
150    
151            protected Object updatePreferences(MethodInvocation methodInvocation)
152                    throws Throwable {
153    
154                    Method method = methodInvocation.getMethod();
155                    Object[] arguments = methodInvocation.getArguments();
156    
157                    long plid = (Long)arguments[2];
158    
159                    if (plid <= 0) {
160                            return methodInvocation.proceed();
161                    }
162    
163                    LayoutRevision layoutRevision = LayoutRevisionUtil.fetchByPrimaryKey(
164                            plid);
165    
166                    ServiceContext serviceContext =
167                            ServiceContextThreadLocal.getServiceContext();
168    
169                    if (serviceContext == null) {
170                            return methodInvocation.proceed();
171                    }
172    
173                    boolean exporting = ParamUtil.getBoolean(serviceContext, "exporting");
174    
175                    if ((layoutRevision == null) || exporting) {
176                            return methodInvocation.proceed();
177                    }
178    
179                    if (!MergeLayoutPrototypesThreadLocal.isInProgress()) {
180                            serviceContext.setWorkflowAction(
181                                    WorkflowConstants.ACTION_SAVE_DRAFT);
182                    }
183    
184                    layoutRevision = LayoutRevisionLocalServiceUtil.updateLayoutRevision(
185                            serviceContext.getUserId(), layoutRevision.getLayoutRevisionId(),
186                            layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
187                            layoutRevision.getTitle(), layoutRevision.getDescription(),
188                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
189                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
190                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
191                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
192                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
193                            serviceContext);
194    
195                    arguments[2] = layoutRevision.getLayoutRevisionId();
196    
197                    return method.invoke(methodInvocation.getThis(), arguments);
198            }
199    
200    }