001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.kernel.staging.LayoutStagingUtil;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.workflow.WorkflowConstants;
020    import com.liferay.portal.model.Layout;
021    import com.liferay.portal.model.LayoutRevision;
022    import com.liferay.portal.model.PortletPreferencesIds;
023    import com.liferay.portal.service.LayoutLocalServiceUtil;
024    import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.ServiceContextThreadLocal;
027    import com.liferay.portal.service.persistence.LayoutRevisionUtil;
028    
029    import java.lang.reflect.InvocationTargetException;
030    import java.lang.reflect.Method;
031    
032    import org.aopalliance.intercept.MethodInterceptor;
033    import org.aopalliance.intercept.MethodInvocation;
034    
035    /**
036     * @author Raymond Augé
037     */
038    public class PortletPreferencesLocalServiceStagingAdvice
039            extends LayoutLocalServiceImpl implements MethodInterceptor {
040    
041            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
042                    try {
043                            String methodName = methodInvocation.getMethod().getName();
044    
045                            if (methodName.equals("getPortletPreferences") &&
046                                    (methodInvocation.getArguments().length == 4)) {
047    
048                                    return getPortletPreferences(methodInvocation);
049                            }
050                            else if (methodName.equals("getPreferences")) {
051                                    return getPreferences(methodInvocation);
052                            }
053                            else if (methodName.equals("getStrictPreferences")) {
054                                    return getPreferences(methodInvocation);
055                            }
056                            else if (methodName.equals("updatePreferences")) {
057                                    return updatePreferences(methodInvocation);
058                            }
059                            else {
060                                    return methodInvocation.proceed();
061                            }
062                    }
063                    catch (InvocationTargetException ite) {
064                            throw ite.getCause();
065                    }
066                    catch (Throwable throwable) {
067                            throw throwable;
068                    }
069            }
070    
071            protected Object getPortletPreferences(MethodInvocation methodInvocation)
072                    throws Throwable {
073    
074                    Method method = methodInvocation.getMethod();
075                    Object[] arguments = methodInvocation.getArguments();
076    
077                    long plid = (Long)arguments[2];
078    
079                    if (plid <= 0) {
080                            return methodInvocation.proceed();
081                    }
082    
083                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
084    
085                    if (!LayoutStagingUtil.isBranchingLayout(layout)) {
086                            return methodInvocation.proceed();
087                    }
088    
089                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
090                            layout);
091    
092                    arguments[2] = layoutRevision.getLayoutRevisionId();
093    
094                    return method.invoke(methodInvocation.getThis(), arguments);
095            }
096    
097            protected Object getPreferences(MethodInvocation methodInvocation)
098                    throws Throwable {
099    
100                    Method method = methodInvocation.getMethod();
101                    Object[] arguments = methodInvocation.getArguments();
102    
103                    long plid = 0;
104    
105                    if (arguments.length == 1) {
106                            PortletPreferencesIds portletPreferencesIds =
107                                    (PortletPreferencesIds)arguments[0];
108    
109                            plid = portletPreferencesIds.getPlid();
110                    }
111                    else {
112                            plid = (Long)arguments[3];
113                    }
114    
115                    if (plid <= 0) {
116                            return methodInvocation.proceed();
117                    }
118    
119                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
120    
121                    if (!LayoutStagingUtil.isBranchingLayout(layout)) {
122                            return methodInvocation.proceed();
123                    }
124    
125                    LayoutRevision layoutRevision =
126                            LayoutStagingUtil.getLayoutRevision(layout);
127    
128                    plid = layoutRevision.getLayoutRevisionId();
129    
130                    if (arguments.length == 1) {
131                            PortletPreferencesIds portletPreferencesIds =
132                                    (PortletPreferencesIds)arguments[0];
133    
134                            portletPreferencesIds.setPlid(plid);
135                    }
136                    else {
137                            arguments[3] = plid;
138                    }
139    
140                    return method.invoke(methodInvocation.getThis(), arguments);
141            }
142    
143            protected Object updatePreferences(MethodInvocation methodInvocation)
144                    throws Throwable {
145    
146                    Method method = methodInvocation.getMethod();
147                    Object[] arguments = methodInvocation.getArguments();
148    
149                    long plid = (Long)arguments[2];
150    
151                    if (plid <= 0) {
152                            return methodInvocation.proceed();
153                    }
154    
155                    LayoutRevision layoutRevision = LayoutRevisionUtil.fetchByPrimaryKey(
156                            plid);
157    
158                    ServiceContext serviceContext =
159                            ServiceContextThreadLocal.getServiceContext();
160    
161                    if (serviceContext == null) {
162                            return methodInvocation.proceed();
163                    }
164    
165                    boolean exporting = ParamUtil.getBoolean(serviceContext, "exporting");
166    
167                    if ((layoutRevision == null) || exporting) {
168                            return methodInvocation.proceed();
169                    }
170    
171                    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
172    
173                    layoutRevision = LayoutRevisionLocalServiceUtil.updateLayoutRevision(
174                            serviceContext.getUserId(), layoutRevision.getLayoutRevisionId(),
175                            layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
176                            layoutRevision.getTitle(), layoutRevision.getDescription(),
177                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
178                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
179                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
180                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
181                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
182                            serviceContext);
183    
184                    arguments[2] = layoutRevision.getLayoutRevisionId();
185    
186                    return method.invoke(methodInvocation.getThis(), arguments);
187            }
188    
189    }