1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.rss.action;
16  
17  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.servlet.SessionMessages;
20  import com.liferay.portal.kernel.util.Constants;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portlet.PortletPreferencesFactoryUtil;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import javax.portlet.ActionRequest;
30  import javax.portlet.ActionResponse;
31  import javax.portlet.PortletConfig;
32  import javax.portlet.PortletPreferences;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  import javax.portlet.ValidatorException;
36  
37  /**
38   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class ConfigurationActionImpl extends BaseConfigurationAction {
43  
44      public void processAction(
45              PortletConfig portletConfig, ActionRequest actionRequest,
46              ActionResponse actionResponse)
47          throws Exception {
48  
49          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
50  
51          String portletResource = ParamUtil.getString(
52              actionRequest, "portletResource");
53  
54          PortletPreferences preferences =
55              PortletPreferencesFactoryUtil.getPortletSetup(
56                  actionRequest, portletResource);
57  
58          if (cmd.equals("remove-footer-article")) {
59              removeFooterArticle(actionRequest, preferences);
60          }
61          else if (cmd.equals("remove-header-article")) {
62              removeHeaderArticle(actionRequest, preferences);
63          }
64          else if (cmd.equals("set-footer-article")) {
65              setFooterArticle(actionRequest, preferences);
66          }
67          else if (cmd.equals("set-header-article")) {
68              setHeaderArticle(actionRequest, preferences);
69          }
70          else if (cmd.equals(Constants.UPDATE)) {
71              updateConfiguration(actionRequest, preferences);
72          }
73  
74          if (SessionErrors.isEmpty(actionRequest)) {
75              try {
76                  preferences.store();
77              }
78              catch (ValidatorException ve) {
79                  SessionErrors.add(
80                      actionRequest, ValidatorException.class.getName(), ve);
81  
82                  return;
83              }
84  
85              SessionMessages.add(
86                  actionRequest, portletConfig.getPortletName() + ".doConfigure");
87          }
88      }
89  
90      public String render(
91              PortletConfig portletConfig, RenderRequest renderRequest,
92              RenderResponse renderResponse)
93          throws Exception {
94  
95          return "/html/portlet/rss/configuration.jsp";
96      }
97  
98      protected void removeFooterArticle(
99              ActionRequest actionRequest, PortletPreferences preferences)
100         throws Exception {
101 
102         preferences.setValues(
103             "footer-article-resource-values", new String[] {"0", ""});
104     }
105 
106     protected void removeHeaderArticle(
107             ActionRequest actionRequest, PortletPreferences preferences)
108         throws Exception {
109 
110         preferences.setValues(
111             "header-article-resource-values", new String[] {"0", ""});
112     }
113 
114     protected void setFooterArticle(
115             ActionRequest actionRequest, PortletPreferences preferences)
116         throws Exception {
117 
118         String footerArticleResourcePrimKey = ParamUtil.getString(
119             actionRequest, "resourcePrimKey");
120         String footerArticleResouceTitle = ParamUtil.getString(
121             actionRequest, "resourceTitle");
122 
123         preferences.setValues(
124             "footer-article-resource-values",
125             new String[] {
126                 footerArticleResourcePrimKey, footerArticleResouceTitle
127             });
128     }
129 
130     protected void setHeaderArticle(
131             ActionRequest actionRequest, PortletPreferences preferences)
132         throws Exception {
133 
134         String headerArticleResourcePrimKey = ParamUtil.getString(
135             actionRequest, "resourcePrimKey");
136         String headerArticleResouceTitle = ParamUtil.getString(
137             actionRequest, "resourceTitle");
138 
139         preferences.setValues(
140             "header-article-resource-values",
141         new String[] {headerArticleResourcePrimKey, headerArticleResouceTitle});
142     }
143 
144     protected void updateConfiguration(
145             ActionRequest actionRequest, PortletPreferences preferences)
146         throws Exception {
147 
148         int[] subscriptionIndexes = StringUtil.split(
149             ParamUtil.getString(actionRequest, "subscriptionIndexes"), 0);
150 
151         Map<String, String> subscriptions = new HashMap<String, String>();
152 
153         for (int i = 0; i < subscriptionIndexes.length; i++) {
154             int subscriptionIndex = subscriptionIndexes[i];
155 
156             String url = ParamUtil.getString(
157                 actionRequest, "url" + subscriptionIndex);
158             String title = ParamUtil.getString(
159                 actionRequest, "title" + subscriptionIndex);
160 
161             if (Validator.isNull(url)) {
162                 continue;
163             }
164 
165             subscriptions.put(url, title);
166         }
167 
168         String[] urls = new String[subscriptions.size()];
169         String[] titles = new String[subscriptions.size()];
170 
171         int i = 0;
172 
173         for (String url : subscriptions.keySet()) {
174             urls[i] = url;
175             titles[i] = subscriptions.get(url);
176 
177             i++;
178         }
179 
180         int entriesPerFeed = ParamUtil.getInteger(
181             actionRequest, "entriesPerFeed", 4);
182         int expandedEntriesPerFeed = ParamUtil.getInteger(
183             actionRequest, "expandedEntriesPerFeed", 1);
184         boolean showFeedTitle = ParamUtil.getBoolean(
185             actionRequest, "showFeedTitle");
186         boolean showFeedPublishedDate = ParamUtil.getBoolean(
187             actionRequest, "showFeedPublishedDate");
188         boolean showFeedDescription = ParamUtil.getBoolean(
189             actionRequest, "showFeedDescription");
190         boolean showFeedImage = ParamUtil.getBoolean(
191             actionRequest, "showFeedImage");
192         String feedImageAlignment = ParamUtil.getString(
193             actionRequest, "feedImageAlignment");
194         boolean showFeedItemAuthor = ParamUtil.getBoolean(
195             actionRequest, "showFeedItemAuthor");
196 
197         preferences.setValues("urls", urls);
198         preferences.setValues("titles", titles);
199         preferences.setValue(
200             "items-per-channel", String.valueOf(entriesPerFeed));
201         preferences.setValue(
202             "expanded-items-per-channel",
203             String.valueOf(expandedEntriesPerFeed));
204         preferences.setValue("show-feed-title", String.valueOf(showFeedTitle));
205         preferences.setValue(
206             "show-feed-published-date", String.valueOf(showFeedPublishedDate));
207         preferences.setValue(
208             "show-feed-description", String.valueOf(showFeedDescription));
209         preferences.setValue("show-feed-image", String.valueOf(showFeedImage));
210         preferences.setValue(
211             "feed-image-alignment", String.valueOf(feedImageAlignment));
212         preferences.setValue(
213             "show-feed-item-author", String.valueOf(showFeedItemAuthor));
214     }
215 
216 }