1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.rss.action;
24  
25  import com.liferay.portal.kernel.portlet.ConfigurationAction;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portlet.PortletPreferencesFactoryUtil;
29  import com.liferay.util.servlet.SessionErrors;
30  import com.liferay.util.servlet.SessionMessages;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  import javax.portlet.PortletPreferences;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  import javax.portlet.ValidatorException;
39  
40  /**
41   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class ConfigurationActionImpl implements ConfigurationAction {
47  
48      public void processAction(
49              PortletConfig config, ActionRequest req, ActionResponse res)
50          throws Exception {
51  
52          String cmd = ParamUtil.getString(req, Constants.CMD);
53  
54          String portletResource = ParamUtil.getString(
55              req, "portletResource");
56  
57          PortletPreferences prefs =
58              PortletPreferencesFactoryUtil.getPortletSetup(req, portletResource);
59  
60          if (cmd.equals("remove-footer-article")) {
61              removeFooterArticle(req, prefs);
62          }
63          else if (cmd.equals("remove-header-article")) {
64              removeHeaderArticle(req, prefs);
65          }
66          else if (cmd.equals("set-footer-article")) {
67              setFooterArticle(req, prefs);
68          }
69          else if (cmd.equals("set-header-article")) {
70              setHeaderArticle(req, prefs);
71          }
72          else if (cmd.equals(Constants.UPDATE)) {
73              updateConfiguration(req, prefs);
74          }
75  
76          if (SessionErrors.isEmpty(req)) {
77              try {
78                  prefs.store();
79              }
80              catch (ValidatorException ve) {
81                  SessionErrors.add(req, ValidatorException.class.getName(), ve);
82  
83                  return;
84              }
85  
86              SessionMessages.add(req, config.getPortletName() + ".doConfigure");
87          }
88      }
89  
90      public String render(
91              PortletConfig config, RenderRequest req, RenderResponse res)
92          throws Exception {
93  
94          return "/html/portlet/rss/configuration.jsp";
95      }
96  
97      protected void removeFooterArticle(
98              ActionRequest req, PortletPreferences prefs)
99          throws Exception {
100 
101         prefs.setValues(
102             "footer-article-resource-values", new String[] {"0", ""});
103     }
104 
105     protected void removeHeaderArticle(
106             ActionRequest req, PortletPreferences prefs)
107         throws Exception {
108 
109         prefs.setValues(
110             "header-article-resource-values", new String[] {"0", ""});
111     }
112 
113     protected void setFooterArticle(ActionRequest req, PortletPreferences prefs)
114         throws Exception {
115 
116         String footerArticleResourcePrimKey = ParamUtil.getString(
117             req, "resourcePrimKey");
118         String footerArticleResouceTitle = ParamUtil.getString(
119             req, "resourceTitle");
120 
121         prefs.setValues(
122             "footer-article-resource-values",
123             new String[] {
124                 footerArticleResourcePrimKey, footerArticleResouceTitle
125             });
126     }
127 
128     protected void setHeaderArticle(ActionRequest req, PortletPreferences prefs)
129         throws Exception {
130 
131         String headerArticleResourcePrimKey = ParamUtil.getString(
132             req, "resourcePrimKey");
133         String headerArticleResouceTitle = ParamUtil.getString(
134             req, "resourceTitle");
135 
136         prefs.setValues(
137             "header-article-resource-values",
138         new String[] {headerArticleResourcePrimKey, headerArticleResouceTitle});
139     }
140 
141     protected void updateConfiguration(
142             ActionRequest req, PortletPreferences prefs)
143         throws Exception {
144 
145         String[] urls = req.getParameterValues("url");
146         String[] titles = req.getParameterValues("title");
147         int entriesPerFeed = ParamUtil.getInteger(req, "entriesPerFeed", 4);
148         boolean showFeedTitle = ParamUtil.getBoolean(req, "showFeedTitle");
149         boolean showFeedPublishedDate = ParamUtil.getBoolean(
150             req, "showFeedPublishedDate");
151         boolean showFeedDescription = ParamUtil.getBoolean(
152             req, "showFeedDescription");
153         boolean showFeedImage = ParamUtil.getBoolean(req, "showFeedImage");
154         String feedImageAlignment = ParamUtil.getString(
155             req, "feedImageAlignment");
156 
157         if (urls != null && titles != null) {
158             prefs.setValues("urls", urls);
159             prefs.setValues("titles", titles);
160         }
161         else {
162             prefs.setValues("urls", new String[0]);
163             prefs.setValues("titles", new String[0]);
164         }
165 
166         prefs.setValue("items-per-channel", String.valueOf(entriesPerFeed));
167         prefs.setValue("show-feed-title", String.valueOf(showFeedTitle));
168         prefs.setValue(
169             "show-feed-published-date", String.valueOf(showFeedPublishedDate));
170         prefs.setValue(
171             "show-feed-description", String.valueOf(showFeedDescription));
172         prefs.setValue("show-feed-image", String.valueOf(showFeedImage));
173         prefs.setValue(
174             "feed-image-alignment", String.valueOf(feedImageAlignment));
175     }
176 
177 }