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