001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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("footer-article-values", new String[] {"0", ""});
101            }
102    
103            protected void removeHeaderArticle(
104                            ActionRequest actionRequest, PortletPreferences preferences)
105                    throws Exception {
106    
107                    preferences.setValues("header-article-values", new String[] {"0", ""});
108            }
109    
110            protected void setFooterArticle(
111                            ActionRequest actionRequest, PortletPreferences preferences)
112                    throws Exception {
113    
114                    long articleGroupId = ParamUtil.getLong(
115                            actionRequest, "articleGroupId");
116                    String articleId = ParamUtil.getString(actionRequest, "articleId");
117    
118                    preferences.setValues(
119                            "footer-article-values",
120                            new String[] {String.valueOf(articleGroupId), articleId});
121            }
122    
123            protected void setHeaderArticle(
124                            ActionRequest actionRequest, PortletPreferences preferences)
125                    throws Exception {
126    
127                    long articleGroupId = ParamUtil.getLong(
128                            actionRequest, "articleGroupId");
129                    String articleId = ParamUtil.getString(actionRequest, "articleId");
130    
131                    preferences.setValues(
132                            "header-article-values",
133                            new String[] {String.valueOf(articleGroupId), articleId});
134            }
135    
136            protected void updateConfiguration(
137                            ActionRequest actionRequest, PortletPreferences preferences)
138                    throws Exception {
139    
140                    int[] subscriptionIndexes = StringUtil.split(
141                            ParamUtil.getString(actionRequest, "subscriptionIndexes"), 0);
142    
143                    Map<String, String> subscriptions = new HashMap<String, String>();
144    
145                    for (int subscriptionIndex : subscriptionIndexes) {
146                            String url = ParamUtil.getString(
147                                    actionRequest, "url" + subscriptionIndex);
148                            String title = ParamUtil.getString(
149                                    actionRequest, "title" + subscriptionIndex);
150    
151                            if (Validator.isNull(url)) {
152                                    continue;
153                            }
154    
155                            subscriptions.put(url, title);
156                    }
157    
158                    String[] urls = new String[subscriptions.size()];
159                    String[] titles = new String[subscriptions.size()];
160    
161                    int i = 0;
162    
163                    for (Map.Entry<String, String> entry : subscriptions.entrySet()) {
164                            urls[i] = entry.getKey();
165                            titles[i] = entry.getValue();
166    
167                            i++;
168                    }
169    
170                    int entriesPerFeed = ParamUtil.getInteger(
171                            actionRequest, "entriesPerFeed", 4);
172                    int expandedEntriesPerFeed = ParamUtil.getInteger(
173                            actionRequest, "expandedEntriesPerFeed", 1);
174                    boolean showFeedTitle = ParamUtil.getBoolean(
175                            actionRequest, "showFeedTitle");
176                    boolean showFeedPublishedDate = ParamUtil.getBoolean(
177                            actionRequest, "showFeedPublishedDate");
178                    boolean showFeedDescription = ParamUtil.getBoolean(
179                            actionRequest, "showFeedDescription");
180                    boolean showFeedImage = ParamUtil.getBoolean(
181                            actionRequest, "showFeedImage");
182                    String feedImageAlignment = ParamUtil.getString(
183                            actionRequest, "feedImageAlignment");
184                    boolean showFeedItemAuthor = ParamUtil.getBoolean(
185                            actionRequest, "showFeedItemAuthor");
186    
187                    preferences.setValues("urls", urls);
188                    preferences.setValues("titles", titles);
189                    preferences.setValue(
190                            "items-per-channel", String.valueOf(entriesPerFeed));
191                    preferences.setValue(
192                            "expanded-items-per-channel",
193                            String.valueOf(expandedEntriesPerFeed));
194                    preferences.setValue("show-feed-title", String.valueOf(showFeedTitle));
195                    preferences.setValue(
196                            "show-feed-published-date", String.valueOf(showFeedPublishedDate));
197                    preferences.setValue(
198                            "show-feed-description", String.valueOf(showFeedDescription));
199                    preferences.setValue("show-feed-image", String.valueOf(showFeedImage));
200                    preferences.setValue(
201                            "feed-image-alignment", String.valueOf(feedImageAlignment));
202                    preferences.setValue(
203                            "show-feed-item-author", String.valueOf(showFeedItemAuthor));
204            }
205    
206    }