001    /**
002     * Copyright (c) 2000-2010 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.BaseConfigurationAction;
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.RenderRequest;
034    import javax.portlet.RenderResponse;
035    import javax.portlet.ValidatorException;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class ConfigurationActionImpl extends BaseConfigurationAction {
041    
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                    String portletResource = ParamUtil.getString(
050                            actionRequest, "portletResource");
051    
052                    PortletPreferences preferences =
053                            PortletPreferencesFactoryUtil.getPortletSetup(
054                                    actionRequest, portletResource);
055    
056                    if (cmd.equals("remove-footer-article")) {
057                            removeFooterArticle(actionRequest, preferences);
058                    }
059                    else if (cmd.equals("remove-header-article")) {
060                            removeHeaderArticle(actionRequest, preferences);
061                    }
062                    else if (cmd.equals("set-footer-article")) {
063                            setFooterArticle(actionRequest, preferences);
064                    }
065                    else if (cmd.equals("set-header-article")) {
066                            setHeaderArticle(actionRequest, preferences);
067                    }
068                    else if (cmd.equals(Constants.UPDATE)) {
069                            updateConfiguration(actionRequest, preferences);
070                    }
071    
072                    if (SessionErrors.isEmpty(actionRequest)) {
073                            try {
074                                    preferences.store();
075                            }
076                            catch (ValidatorException ve) {
077                                    SessionErrors.add(
078                                            actionRequest, ValidatorException.class.getName(), ve);
079    
080                                    return;
081                            }
082    
083                            SessionMessages.add(
084                                    actionRequest, portletConfig.getPortletName() + ".doConfigure");
085                    }
086            }
087    
088            public String render(
089                            PortletConfig portletConfig, RenderRequest renderRequest,
090                            RenderResponse renderResponse)
091                    throws Exception {
092    
093                    return "/html/portlet/rss/configuration.jsp";
094            }
095    
096            protected void removeFooterArticle(
097                            ActionRequest actionRequest, PortletPreferences preferences)
098                    throws Exception {
099    
100                    preferences.setValues(
101                            "footer-article-resource-values", new String[] {"0", ""});
102            }
103    
104            protected void removeHeaderArticle(
105                            ActionRequest actionRequest, PortletPreferences preferences)
106                    throws Exception {
107    
108                    preferences.setValues(
109                            "header-article-resource-values", new String[] {"0", ""});
110            }
111    
112            protected void setFooterArticle(
113                            ActionRequest actionRequest, PortletPreferences preferences)
114                    throws Exception {
115    
116                    String footerArticleResourcePrimKey = ParamUtil.getString(
117                            actionRequest, "resourcePrimKey");
118                    String footerArticleResouceTitle = ParamUtil.getString(
119                            actionRequest, "resourceTitle");
120    
121                    preferences.setValues(
122                            "footer-article-resource-values",
123                            new String[] {
124                                    footerArticleResourcePrimKey, footerArticleResouceTitle
125                            });
126            }
127    
128            protected void setHeaderArticle(
129                            ActionRequest actionRequest, PortletPreferences preferences)
130                    throws Exception {
131    
132                    String headerArticleResourcePrimKey = ParamUtil.getString(
133                            actionRequest, "resourcePrimKey");
134                    String headerArticleResouceTitle = ParamUtil.getString(
135                            actionRequest, "resourceTitle");
136    
137                    preferences.setValues(
138                            "header-article-resource-values",
139                    new String[] {headerArticleResourcePrimKey, headerArticleResouceTitle});
140            }
141    
142            protected void updateConfiguration(
143                            ActionRequest actionRequest, PortletPreferences preferences)
144                    throws Exception {
145    
146                    int[] subscriptionIndexes = StringUtil.split(
147                            ParamUtil.getString(actionRequest, "subscriptionIndexes"), 0);
148    
149                    Map<String, String> subscriptions = new HashMap<String, String>();
150    
151                    for (int i = 0; i < subscriptionIndexes.length; i++) {
152                            int subscriptionIndex = subscriptionIndexes[i];
153    
154                            String url = ParamUtil.getString(
155                                    actionRequest, "url" + subscriptionIndex);
156                            String title = ParamUtil.getString(
157                                    actionRequest, "title" + subscriptionIndex);
158    
159                            if (Validator.isNull(url)) {
160                                    continue;
161                            }
162    
163                            subscriptions.put(url, title);
164                    }
165    
166                    String[] urls = new String[subscriptions.size()];
167                    String[] titles = new String[subscriptions.size()];
168    
169                    int i = 0;
170    
171                    for (Map.Entry<String, String> entry : subscriptions.entrySet()) {
172                            urls[i] = entry.getKey();
173                            titles[i] = entry.getValue();
174    
175                            i++;
176                    }
177    
178                    int entriesPerFeed = ParamUtil.getInteger(
179                            actionRequest, "entriesPerFeed", 4);
180                    int expandedEntriesPerFeed = ParamUtil.getInteger(
181                            actionRequest, "expandedEntriesPerFeed", 1);
182                    boolean showFeedTitle = ParamUtil.getBoolean(
183                            actionRequest, "showFeedTitle");
184                    boolean showFeedPublishedDate = ParamUtil.getBoolean(
185                            actionRequest, "showFeedPublishedDate");
186                    boolean showFeedDescription = ParamUtil.getBoolean(
187                            actionRequest, "showFeedDescription");
188                    boolean showFeedImage = ParamUtil.getBoolean(
189                            actionRequest, "showFeedImage");
190                    String feedImageAlignment = ParamUtil.getString(
191                            actionRequest, "feedImageAlignment");
192                    boolean showFeedItemAuthor = ParamUtil.getBoolean(
193                            actionRequest, "showFeedItemAuthor");
194    
195                    preferences.setValues("urls", urls);
196                    preferences.setValues("titles", titles);
197                    preferences.setValue(
198                            "items-per-channel", String.valueOf(entriesPerFeed));
199                    preferences.setValue(
200                            "expanded-items-per-channel",
201                            String.valueOf(expandedEntriesPerFeed));
202                    preferences.setValue("show-feed-title", String.valueOf(showFeedTitle));
203                    preferences.setValue(
204                            "show-feed-published-date", String.valueOf(showFeedPublishedDate));
205                    preferences.setValue(
206                            "show-feed-description", String.valueOf(showFeedDescription));
207                    preferences.setValue("show-feed-image", String.valueOf(showFeedImage));
208                    preferences.setValue(
209                            "feed-image-alignment", String.valueOf(feedImageAlignment));
210                    preferences.setValue(
211                            "show-feed-item-author", String.valueOf(showFeedItemAuthor));
212            }
213    
214    }