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