1   /**
2    * Copyright (c) 2000-2009 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.BaseConfigurationAction;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.servlet.SessionMessages;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portlet.PortletPreferencesFactoryUtil;
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 extends BaseConfigurationAction {
47  
48      public void processAction(
49              PortletConfig portletConfig, ActionRequest actionRequest,
50              ActionResponse actionResponse)
51          throws Exception {
52  
53          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
54  
55          String portletResource = ParamUtil.getString(
56              actionRequest, "portletResource");
57  
58          PortletPreferences prefs =
59              PortletPreferencesFactoryUtil.getPortletSetup(
60                  actionRequest, portletResource);
61  
62          if (cmd.equals("remove-footer-article")) {
63              removeFooterArticle(actionRequest, prefs);
64          }
65          else if (cmd.equals("remove-header-article")) {
66              removeHeaderArticle(actionRequest, prefs);
67          }
68          else if (cmd.equals("set-footer-article")) {
69              setFooterArticle(actionRequest, prefs);
70          }
71          else if (cmd.equals("set-header-article")) {
72              setHeaderArticle(actionRequest, prefs);
73          }
74          else if (cmd.equals(Constants.UPDATE)) {
75              updateConfiguration(actionRequest, prefs);
76          }
77  
78          if (SessionErrors.isEmpty(actionRequest)) {
79              try {
80                  prefs.store();
81              }
82              catch (ValidatorException ve) {
83                  SessionErrors.add(
84                      actionRequest, ValidatorException.class.getName(), ve);
85  
86                  return;
87              }
88  
89              SessionMessages.add(
90                  actionRequest, portletConfig.getPortletName() + ".doConfigure");
91          }
92      }
93  
94      public String render(
95              PortletConfig portletConfig, RenderRequest renderRequest,
96              RenderResponse renderResponse)
97          throws Exception {
98  
99          return "/html/portlet/rss/configuration.jsp";
100     }
101 
102     protected void removeFooterArticle(
103             ActionRequest actionRequest, PortletPreferences prefs)
104         throws Exception {
105 
106         prefs.setValues(
107             "footer-article-resource-values", new String[] {"0", ""});
108     }
109 
110     protected void removeHeaderArticle(
111             ActionRequest actionRequest, PortletPreferences prefs)
112         throws Exception {
113 
114         prefs.setValues(
115             "header-article-resource-values", new String[] {"0", ""});
116     }
117 
118     protected void setFooterArticle(
119             ActionRequest actionRequest, PortletPreferences prefs)
120         throws Exception {
121 
122         String footerArticleResourcePrimKey = ParamUtil.getString(
123             actionRequest, "resourcePrimKey");
124         String footerArticleResouceTitle = ParamUtil.getString(
125             actionRequest, "resourceTitle");
126 
127         prefs.setValues(
128             "footer-article-resource-values",
129             new String[] {
130                 footerArticleResourcePrimKey, footerArticleResouceTitle
131             });
132     }
133 
134     protected void setHeaderArticle(
135             ActionRequest actionRequest, PortletPreferences prefs)
136         throws Exception {
137 
138         String headerArticleResourcePrimKey = ParamUtil.getString(
139             actionRequest, "resourcePrimKey");
140         String headerArticleResouceTitle = ParamUtil.getString(
141             actionRequest, "resourceTitle");
142 
143         prefs.setValues(
144             "header-article-resource-values",
145         new String[] {headerArticleResourcePrimKey, headerArticleResouceTitle});
146     }
147 
148     protected void updateConfiguration(
149             ActionRequest actionRequest, PortletPreferences prefs)
150         throws Exception {
151 
152         String[] urls = actionRequest.getParameterValues("url");
153         String[] titles = actionRequest.getParameterValues("title");
154         int entriesPerFeed = ParamUtil.getInteger(
155             actionRequest, "entriesPerFeed", 4);
156         int expandedEntriesPerFeed = ParamUtil.getInteger(
157             actionRequest, "expandedEntriesPerFeed", 1);
158         boolean showFeedTitle = ParamUtil.getBoolean(
159             actionRequest, "showFeedTitle");
160         boolean showFeedPublishedDate = ParamUtil.getBoolean(
161             actionRequest, "showFeedPublishedDate");
162         boolean showFeedDescription = ParamUtil.getBoolean(
163             actionRequest, "showFeedDescription");
164         boolean showFeedImage = ParamUtil.getBoolean(
165             actionRequest, "showFeedImage");
166         String feedImageAlignment = ParamUtil.getString(
167             actionRequest, "feedImageAlignment");
168         boolean showFeedItemAuthor = ParamUtil.getBoolean(
169             actionRequest, "showFeedItemAuthor");
170 
171         if (urls != null && titles != null) {
172             prefs.setValues("urls", urls);
173             prefs.setValues("titles", titles);
174         }
175         else {
176             prefs.setValues("urls", new String[0]);
177             prefs.setValues("titles", new String[0]);
178         }
179 
180         prefs.setValue("items-per-channel", String.valueOf(entriesPerFeed));
181         prefs.setValue(
182             "expanded-items-per-channel",
183             String.valueOf(expandedEntriesPerFeed));
184         prefs.setValue("show-feed-title", String.valueOf(showFeedTitle));
185         prefs.setValue(
186             "show-feed-published-date", String.valueOf(showFeedPublishedDate));
187         prefs.setValue(
188             "show-feed-description", String.valueOf(showFeedDescription));
189         prefs.setValue("show-feed-image", String.valueOf(showFeedImage));
190         prefs.setValue(
191             "feed-image-alignment", String.valueOf(feedImageAlignment));
192         prefs.setValue(
193             "show-feed-item-author", String.valueOf(showFeedItemAuthor));
194     }
195 
196 }