001    /**
002     * Copyright (c) 2000-2013 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.LayoutConstants;
023    import com.liferay.portal.model.LayoutRevision;
024    import com.liferay.portal.model.PortletPreferencesIds;
025    import com.liferay.portal.service.LayoutLocalServiceUtil;
026    import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextThreadLocal;
029    import com.liferay.portal.service.persistence.LayoutRevisionUtil;
030    import com.liferay.portal.staging.ProxiedLayoutsThreadLocal;
031    import com.liferay.portal.staging.StagingAdvicesThreadLocal;
032    
033    import java.lang.reflect.InvocationTargetException;
034    import java.lang.reflect.Method;
035    
036    import org.aopalliance.intercept.MethodInterceptor;
037    import org.aopalliance.intercept.MethodInvocation;
038    
039    /**
040     * @author Raymond Aug??
041     */
042    public class PortletPreferencesLocalServiceStagingAdvice
043            implements MethodInterceptor {
044    
045            @Override
046            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
047                    if (!StagingAdvicesThreadLocal.isEnabled()) {
048                            return methodInvocation.proceed();
049                    }
050    
051                    try {
052                            Object[] arguments = methodInvocation.getArguments();
053    
054                            if (arguments == null) {
055                                    return methodInvocation.proceed();
056                            }
057    
058                            Method method = methodInvocation.getMethod();
059    
060                            String methodName = method.getName();
061    
062                            if (methodName.equals("getPortletPreferences") &&
063                                    (arguments.length == 4)) {
064    
065                                    return getPortletPreferences(methodInvocation);
066                            }
067                            else if (methodName.equals("getPortletPreferencesCount") &&
068                                             ((arguments.length == 3) || (arguments.length == 5))) {
069    
070                                    return getPortletPreferencesCount(methodInvocation);
071                            }
072                            else if (methodName.equals("getPreferences")) {
073                                    return getPreferences(methodInvocation);
074                            }
075                            else if (methodName.equals("getStrictPreferences")) {
076                                    return getPreferences(methodInvocation);
077                            }
078                            else if (methodName.equals("updatePreferences")) {
079                                    return updatePreferences(methodInvocation);
080                            }
081    
082                            return methodInvocation.proceed();
083                    }
084                    catch (InvocationTargetException ite) {
085                            throw ite.getCause();
086                    }
087                    catch (Throwable throwable) {
088                            throw throwable;
089                    }
090            }
091    
092            protected Object getPortletPreferences(MethodInvocation methodInvocation)
093                    throws Throwable {
094    
095                    Method method = methodInvocation.getMethod();
096                    Object[] arguments = methodInvocation.getArguments();
097    
098                    long plid = (Long)arguments[2];
099    
100                    if (plid <= 0) {
101                            return methodInvocation.proceed();
102                    }
103    
104                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
105    
106                    if (!LayoutStagingUtil.isBranchingLayout(layout)) {
107                            return methodInvocation.proceed();
108                    }
109    
110                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
111                            layout);
112    
113                    arguments[2] = layoutRevision.getLayoutRevisionId();
114    
115                    return method.invoke(methodInvocation.getThis(), arguments);
116            }
117    
118            protected Object getPortletPreferencesCount(
119                            MethodInvocation methodInvocation)
120                    throws Throwable {
121    
122                    Method method = methodInvocation.getMethod();
123                    Object[] arguments = methodInvocation.getArguments();
124    
125                    long plid = LayoutConstants.DEFAULT_PLID;
126    
127                    if (arguments.length == 3) {
128                            plid = (Long)arguments[1];
129                    }
130                    else {
131                            plid = (Long)arguments[2];
132                    }
133    
134                    if (plid <= 0) {
135                            return methodInvocation.proceed();
136                    }
137    
138                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
139    
140                    if (!LayoutStagingUtil.isBranchingLayout(layout)) {
141                            return methodInvocation.proceed();
142                    }
143    
144                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
145                            layout);
146    
147                    if (arguments.length == 3) {
148                            arguments[1] = layoutRevision.getLayoutRevisionId();
149                    }
150                    else {
151                            arguments[2] = layoutRevision.getLayoutRevisionId();
152                    }
153    
154                    return method.invoke(methodInvocation.getThis(), arguments);
155            }
156    
157            protected Object getPreferences(MethodInvocation methodInvocation)
158                    throws Throwable {
159    
160                    Method method = methodInvocation.getMethod();
161                    Object[] arguments = methodInvocation.getArguments();
162    
163                    long plid = 0;
164    
165                    if (arguments.length == 1) {
166                            PortletPreferencesIds portletPreferencesIds =
167                                    (PortletPreferencesIds)arguments[0];
168    
169                            plid = portletPreferencesIds.getPlid();
170                    }
171                    else {
172                            plid = (Long)arguments[3];
173                    }
174    
175                    if (plid <= 0) {
176                            return methodInvocation.proceed();
177                    }
178    
179                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
180    
181                    if (!LayoutStagingUtil.isBranchingLayout(layout)) {
182                            return methodInvocation.proceed();
183                    }
184    
185                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
186                            layout);
187    
188                    if (layoutRevision == null) {
189                            return methodInvocation.proceed();
190                    }
191    
192                    plid = layoutRevision.getLayoutRevisionId();
193    
194                    if (arguments.length == 1) {
195                            PortletPreferencesIds portletPreferencesIds =
196                                    (PortletPreferencesIds)arguments[0];
197    
198                            portletPreferencesIds.setPlid(plid);
199                    }
200                    else {
201                            arguments[3] = plid;
202                    }
203    
204                    return method.invoke(methodInvocation.getThis(), arguments);
205            }
206    
207            protected Object updatePreferences(MethodInvocation methodInvocation)
208                    throws Throwable {
209    
210                    Method method = methodInvocation.getMethod();
211                    Object[] arguments = methodInvocation.getArguments();
212    
213                    long plid = (Long)arguments[2];
214    
215                    if (plid <= 0) {
216                            return methodInvocation.proceed();
217                    }
218    
219                    LayoutRevision layoutRevision = LayoutRevisionUtil.fetchByPrimaryKey(
220                            plid);
221    
222                    ServiceContext serviceContext =
223                            ServiceContextThreadLocal.getServiceContext();
224    
225                    if (serviceContext == null) {
226                            return methodInvocation.proceed();
227                    }
228    
229                    boolean exporting = ParamUtil.getBoolean(serviceContext, "exporting");
230    
231                    if ((layoutRevision == null) || exporting) {
232                            return methodInvocation.proceed();
233                    }
234    
235                    if (!MergeLayoutPrototypesThreadLocal.isInProgress()) {
236                            serviceContext.setWorkflowAction(
237                                    WorkflowConstants.ACTION_SAVE_DRAFT);
238                    }
239    
240                    layoutRevision = LayoutRevisionLocalServiceUtil.updateLayoutRevision(
241                            serviceContext.getUserId(), layoutRevision.getLayoutRevisionId(),
242                            layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
243                            layoutRevision.getTitle(), layoutRevision.getDescription(),
244                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
245                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
246                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
247                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
248                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
249                            serviceContext);
250    
251                    arguments[2] = layoutRevision.getLayoutRevisionId();
252    
253                    ProxiedLayoutsThreadLocal.clearProxiedLayouts();
254    
255                    return method.invoke(methodInvocation.getThis(), arguments);
256            }
257    
258    }