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(
59                  req, portletResource, true, true);
60  
61          if (cmd.equals("remove-footer-article")) {
62              removeFooterArticle(req, prefs);
63          }
64          else if (cmd.equals("remove-header-article")) {
65              removeHeaderArticle(req, prefs);
66          }
67          else if (cmd.equals("set-footer-article")) {
68              setFooterArticle(req, prefs);
69          }
70          else if (cmd.equals("set-header-article")) {
71              setHeaderArticle(req, prefs);
72          }
73          else if (cmd.equals(Constants.UPDATE)) {
74              updateConfiguration(req, prefs);
75          }
76  
77          if (SessionErrors.isEmpty(req)) {
78              try {
79                  prefs.store();
80              }
81              catch (ValidatorException ve) {
82                  SessionErrors.add(req, ValidatorException.class.getName(), ve);
83  
84                  return;
85              }
86  
87              SessionMessages.add(req, config.getPortletName() + ".doConfigure");
88          }
89      }
90  
91      public String render(
92              PortletConfig config, RenderRequest req, RenderResponse res)
93          throws Exception {
94  
95          return "/html/portlet/rss/configuration.jsp";
96      }
97  
98      protected void removeFooterArticle(
99              ActionRequest req, PortletPreferences prefs)
100         throws Exception {
101 
102         prefs.setValues(
103             "footer-article-resouce-values", new String[] {"0", ""});
104     }
105 
106     protected void removeHeaderArticle(
107             ActionRequest req, PortletPreferences prefs)
108         throws Exception {
109 
110         prefs.setValues(
111             "header-article-resouce-values", new String[] {"0", ""});
112     }
113 
114     protected void setFooterArticle(ActionRequest req, PortletPreferences prefs)
115         throws Exception {
116 
117         String footerArticleResouceId = ParamUtil.getString(req, "resourceId");
118         String footerArticleResouceTitle = ParamUtil.getString(
119             req, "resourceTitle");
120 
121         prefs.setValues(
122             "footer-article-resouce-values",
123             new String[] {footerArticleResouceId, footerArticleResouceTitle});
124     }
125 
126     protected void setHeaderArticle(ActionRequest req, PortletPreferences prefs)
127         throws Exception {
128 
129         String headerArticleResouceId = ParamUtil.getString(req, "resourceId");
130         String headerArticleResouceTitle = ParamUtil.getString(
131             req, "resourceTitle");
132 
133         prefs.setValues(
134             "header-article-resouce-values",
135         new String[] {headerArticleResouceId, headerArticleResouceTitle});
136     }
137 
138     protected void updateConfiguration(
139             ActionRequest req, PortletPreferences prefs)
140         throws Exception {
141 
142         String[] urls = req.getParameterValues("url");
143         String[] titles = req.getParameterValues("title");
144         int entriesPerFeed = ParamUtil.getInteger(req, "entriesPerFeed", 4);
145         boolean showFeedTitle = ParamUtil.getBoolean(req, "showFeedTitle");
146         boolean showFeedPublishedDate = ParamUtil.getBoolean(
147             req, "showFeedPublishedDate");
148         boolean showFeedDescription = ParamUtil.getBoolean(
149             req, "showFeedDescription");
150         boolean showFeedImage = ParamUtil.getBoolean(req, "showFeedImage");
151         String feedImageAlignment = ParamUtil.getString(
152             req, "feedImageAlignment");
153         long headerArticleResouceId = ParamUtil.getLong(
154             req, "headerArticleResouceId");
155         long footerArticleResouceId = ParamUtil.getLong(
156             req, "footerArticleResouceId");
157 
158         if (urls != null && titles != null) {
159             prefs.setValues("urls", urls);
160             prefs.setValues("titles", titles);
161         }
162         else {
163             prefs.setValues("urls", new String[0]);
164             prefs.setValues("titles", new String[0]);
165         }
166 
167         prefs.setValue("items-per-channel", String.valueOf(entriesPerFeed));
168         prefs.setValue("show-feed-title", String.valueOf(showFeedTitle));
169         prefs.setValue(
170             "show-feed-published-date", String.valueOf(showFeedPublishedDate));
171         prefs.setValue(
172             "show-feed-description", String.valueOf(showFeedDescription));
173         prefs.setValue("show-feed-image", String.valueOf(showFeedImage));
174         prefs.setValue(
175             "feed-image-alignment", String.valueOf(feedImageAlignment));
176         prefs.setValue(
177             "header-article-resouce-id",
178             String.valueOf(headerArticleResouceId));
179         prefs.setValue(
180             "footer-article-resouce-id",
181             String.valueOf(footerArticleResouceId));
182     }
183 
184 }