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