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