001    /**
002     * Copyright (c) 2000-2012 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.rss.action;
016    
017    import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
018    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portlet.PortletPreferencesFactoryUtil;
026    
027    import java.util.LinkedHashMap;
028    import java.util.Map;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.ActionResponse;
032    import javax.portlet.PortletConfig;
033    import javax.portlet.PortletPreferences;
034    import javax.portlet.ValidatorException;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class ConfigurationActionImpl extends DefaultConfigurationAction {
040    
041            @Override
042            public void processAction(
043                            PortletConfig portletConfig, ActionRequest actionRequest,
044                            ActionResponse actionResponse)
045                    throws Exception {
046    
047                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
048    
049                    if (cmd.equals(Constants.UPDATE)) {
050                            updateSubscriptions(actionRequest);
051    
052                            super.processAction(portletConfig, actionRequest, actionResponse);
053    
054                            return;
055                    }
056    
057                    String portletResource = ParamUtil.getString(
058                            actionRequest, "portletResource");
059    
060                    PortletPreferences preferences =
061                            PortletPreferencesFactoryUtil.getPortletSetup(
062                                    actionRequest, portletResource);
063    
064                    if (cmd.equals("remove-footer-article")) {
065                            removeFooterArticle(actionRequest, preferences);
066                    }
067                    else if (cmd.equals("remove-header-article")) {
068                            removeHeaderArticle(actionRequest, preferences);
069                    }
070                    else if (cmd.equals("set-footer-article")) {
071                            setFooterArticle(actionRequest, preferences);
072                    }
073                    else if (cmd.equals("set-header-article")) {
074                            setHeaderArticle(actionRequest, preferences);
075                    }
076    
077                    if (SessionErrors.isEmpty(actionRequest)) {
078                            try {
079                                    preferences.store();
080                            }
081                            catch (ValidatorException ve) {
082                                    SessionErrors.add(
083                                            actionRequest, ValidatorException.class.getName(), ve);
084    
085                                    return;
086                            }
087    
088                            LiferayPortletConfig liferayPortletConfig =
089                                    (LiferayPortletConfig)portletConfig;
090    
091                            SessionMessages.add(
092                                    actionRequest,
093                                    liferayPortletConfig.getPortletId() +
094                                            SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
095                                    portletResource);
096    
097                            SessionMessages.add(
098                                    actionRequest,
099                                    liferayPortletConfig.getPortletId() +
100                                            SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
101                    }
102            }
103    
104            protected void removeFooterArticle(
105                            ActionRequest actionRequest, PortletPreferences preferences)
106                    throws Exception {
107    
108                    preferences.setValues("footerArticleValues", new String[] {"0", ""});
109            }
110    
111            protected void removeHeaderArticle(
112                            ActionRequest actionRequest, PortletPreferences preferences)
113                    throws Exception {
114    
115                    preferences.setValues("headerArticleValues", new String[] {"0", ""});
116            }
117    
118            protected void setFooterArticle(
119                            ActionRequest actionRequest, PortletPreferences preferences)
120                    throws Exception {
121    
122                    long articleGroupId = ParamUtil.getLong(
123                            actionRequest, "articleGroupId");
124                    String articleId = ParamUtil.getString(actionRequest, "articleId");
125    
126                    preferences.setValues(
127                            "footerArticleValues",
128                            new String[] {String.valueOf(articleGroupId), articleId});
129            }
130    
131            protected void setHeaderArticle(
132                            ActionRequest actionRequest, PortletPreferences preferences)
133                    throws Exception {
134    
135                    long articleGroupId = ParamUtil.getLong(
136                            actionRequest, "articleGroupId");
137                    String articleId = ParamUtil.getString(actionRequest, "articleId");
138    
139                    preferences.setValues(
140                            "headerArticleValues",
141                            new String[] {String.valueOf(articleGroupId), articleId});
142            }
143    
144            protected void updateSubscriptions(ActionRequest actionRequest)
145                    throws Exception {
146    
147                    int[] subscriptionIndexes = StringUtil.split(
148                            ParamUtil.getString(actionRequest, "subscriptionIndexes"), 0);
149    
150                    Map<String, String> subscriptions = new LinkedHashMap<String, String>();
151    
152                    for (int subscriptionIndex : subscriptionIndexes) {
153                            String url = ParamUtil.getString(
154                                    actionRequest, "url" + subscriptionIndex);
155                            String title = ParamUtil.getString(
156                                    actionRequest, "title" + subscriptionIndex);
157    
158                            if (Validator.isNull(url)) {
159                                    continue;
160                            }
161    
162                            subscriptions.put(url, title);
163                    }
164    
165                    String[] urls = new String[subscriptions.size()];
166                    String[] titles = new String[subscriptions.size()];
167    
168                    int i = 0;
169    
170                    for (Map.Entry<String, String> entry : subscriptions.entrySet()) {
171                            urls[i] = entry.getKey();
172                            titles[i] = entry.getValue();
173    
174                            i++;
175                    }
176    
177                    setPreference(actionRequest, "urls", urls);
178                    setPreference(actionRequest, "titles", titles);
179            }
180    
181    }