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.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                    try {
052                            Class<?> clazz = getClass();
053    
054                            _doProcessActionMethod = clazz.getDeclaredMethod(
055                                    "doProcessAction",
056                                    new Class<?>[] {
057                                            PortletConfig.class, ActionRequest.class,
058                                            ActionResponse.class
059                                    });
060                    }
061                    catch (Exception e) {
062                            _log.error(e, e);
063                    }
064            }
065    
066            @Override
067            public void processAction(
068                            PortletConfig portletConfig, ActionRequest actionRequest,
069                            ActionResponse actionResponse)
070                    throws Exception {
071    
072                    // This logic has to run in a transaction which we will invoke directly
073                    // since this is not a Spring bean
074    
075                    ServiceBeanMethodInvocationFactoryUtil.proceed(
076                            this, ConfigurationActionImpl.class, _doProcessActionMethod,
077                            new Object[] {portletConfig, actionRequest, actionResponse},
078                            new String[] {"transactionAdvice"});
079            }
080    
081            /**
082             * This method is invoked in a transaction because we may result in a
083             * persistence call before and/or after the call to super.processAction()
084             * which itself results in a persistence call.
085             */
086            @Transactional(
087                    isolation = Isolation.PORTAL, propagation = Propagation.REQUIRES_NEW,
088                    rollbackFor = {Exception.class}
089            )
090            protected void doProcessAction(
091                            PortletConfig portletConfig, ActionRequest actionRequest,
092                            ActionResponse actionResponse)
093                    throws Exception {
094    
095                    String[] extensions = actionRequest.getParameterValues("extensions");
096    
097                    setPreference(actionRequest, "extensions", extensions);
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 Log _log = LogFactoryUtil.getLog(
150                    ConfigurationActionImpl.class);
151    
152            private Method _doProcessActionMethod;
153    
154    }