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                    plid = layoutRevision.getLayoutRevisionId();
144    
145                    if (arguments.length == 1) {
146                            PortletPreferencesIds portletPreferencesIds =
147                                    (PortletPreferencesIds)arguments[0];
148    
149                            portletPreferencesIds.setPlid(plid);
150                    }
151                    else {
152                            arguments[3] = plid;
153                    }
154    
155                    return method.invoke(methodInvocation.getThis(), arguments);
156            }
157    
158            protected Object updatePreferences(MethodInvocation methodInvocation)
159                    throws Throwable {
160    
161                    Method method = methodInvocation.getMethod();
162                    Object[] arguments = methodInvocation.getArguments();
163    
164                    long plid = (Long)arguments[2];
165    
166                    if (plid <= 0) {
167                            return methodInvocation.proceed();
168                    }
169    
170                    LayoutRevision layoutRevision = LayoutRevisionUtil.fetchByPrimaryKey(
171                            plid);
172    
173                    ServiceContext serviceContext =
174                            ServiceContextThreadLocal.getServiceContext();
175    
176                    if (serviceContext == null) {
177                            return methodInvocation.proceed();
178                    }
179    
180                    boolean exporting = ParamUtil.getBoolean(serviceContext, "exporting");
181    
182                    if ((layoutRevision == null) || exporting) {
183                            return methodInvocation.proceed();
184                    }
185    
186                    if (!MergeLayoutPrototypesThreadLocal.isInProgress()) {
187                            serviceContext.setWorkflowAction(
188                                    WorkflowConstants.ACTION_SAVE_DRAFT);
189                    }
190    
191                    layoutRevision = LayoutRevisionLocalServiceUtil.updateLayoutRevision(
192                            serviceContext.getUserId(), layoutRevision.getLayoutRevisionId(),
193                            layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
194                            layoutRevision.getTitle(), layoutRevision.getDescription(),
195                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
196                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
197                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
198                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
199                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
200                            serviceContext);
201    
202                    arguments[2] = layoutRevision.getLayoutRevisionId();
203    
204                    ProxiedLayoutsThreadLocal.clearProxiedLayouts();
205    
206                    return method.invoke(methodInvocation.getThis(), arguments);
207            }
208    
209    }