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.portlet.journalcontent.action;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
020    import com.liferay.portal.kernel.portlet.PortletLayoutListener;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.transaction.Isolation;
023    import com.liferay.portal.kernel.transaction.Propagation;
024    import com.liferay.portal.kernel.transaction.Transactional;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.ServiceBeanMethodInvocationFactoryUtil;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.Layout;
030    import com.liferay.portal.model.Portlet;
031    import com.liferay.portal.service.PortletLocalServiceUtil;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.WebKeys;
034    import com.liferay.portlet.PortletPreferencesFactoryUtil;
035    
036    import java.lang.reflect.Method;
037    
038    import javax.portlet.ActionRequest;
039    import javax.portlet.ActionResponse;
040    import javax.portlet.PortletConfig;
041    import javax.portlet.PortletPreferences;
042    import javax.portlet.PortletRequest;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     * @author Douglas Wong
047     * @author Raymond Augé
048     */
049    public class ConfigurationActionImpl extends DefaultConfigurationAction {
050    
051            public ConfigurationActionImpl() {
052                    try {
053                            Class<?> clazz = getClass();
054    
055                            _doProcessActionMethod = clazz.getDeclaredMethod(
056                                    "doProcessAction",
057                                    new Class<?>[] {
058                                            PortletConfig.class, ActionRequest.class,
059                                            ActionResponse.class
060                                    });
061                    }
062                    catch (Exception e) {
063                            _log.error(e, e);
064                    }
065            }
066    
067            @Override
068            public void processAction(
069                            PortletConfig portletConfig, ActionRequest actionRequest,
070                            ActionResponse actionResponse)
071                    throws Exception {
072    
073                    // This logic has to run in a transaction which we will invoke directly
074                    // since this is not a Spring bean
075    
076                    ServiceBeanMethodInvocationFactoryUtil.proceed(
077                            this, ConfigurationActionImpl.class, _doProcessActionMethod,
078                            new Object[] {portletConfig, actionRequest, actionResponse},
079                            new String[] {"transactionAdvice"});
080            }
081    
082            /**
083             * This method is invoked in a transaction because we may result in a
084             * persistence call before and/or after the call to super.processAction()
085             * which itself results in a persistence call.
086             */
087            @Transactional(
088                    isolation = Isolation.PORTAL, propagation = Propagation.REQUIRES_NEW,
089                    rollbackFor = {Exception.class}
090            )
091            protected void doProcessAction(
092                            PortletConfig portletConfig, ActionRequest actionRequest,
093                            ActionResponse actionResponse)
094                    throws Exception {
095    
096                    String[] extensions = actionRequest.getParameterValues("extensions");
097    
098                    setPreference(actionRequest, "extensions", extensions);
099    
100                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
101                            WebKeys.THEME_DISPLAY);
102    
103                    Layout layout = themeDisplay.getLayout();
104    
105                    String portletResource = ParamUtil.getString(
106                            actionRequest, "portletResource");
107    
108                    PortletPreferences preferences =
109                            PortletPreferencesFactoryUtil.getPortletSetup(
110                                    layout, portletResource, StringPool.BLANK);
111    
112                    String articleId = getArticleId(actionRequest);
113    
114                    String originalArticleId = preferences.getValue("articleId", null);
115    
116                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
117                            themeDisplay.getCompanyId(), portletResource);
118    
119                    PortletLayoutListener portletLayoutListener =
120                            portlet.getPortletLayoutListenerInstance();
121    
122                    if ((portletLayoutListener != null) &&
123                            Validator.isNotNull(originalArticleId) &&
124                            !originalArticleId.equals(articleId)) {
125    
126                            // Results in a persistence call
127    
128                            portletLayoutListener.onRemoveFromLayout(
129                                    portletResource, layout.getPlid());
130                    }
131    
132                    // Results in a persistence call
133    
134                    super.processAction(portletConfig, actionRequest, actionResponse);
135    
136                    if (SessionErrors.isEmpty(actionRequest) &&
137                            (portletLayoutListener != null)) {
138    
139                            // Results in a persistence call
140    
141                            portletLayoutListener.onAddToLayout(
142                                    portletResource, layout.getPlid());
143                    }
144            }
145    
146            protected String getArticleId(PortletRequest portletRequest) {
147                    String articleId = getParameter(portletRequest, "articleId");
148    
149                    return articleId.toUpperCase();
150            }
151    
152            private static Log _log = LogFactoryUtil.getLog(
153                    ConfigurationActionImpl.class);
154    
155            private Method _doProcessActionMethod;
156    
157    }