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