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