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.ConfigurationAction;
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 implements ConfigurationAction {
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 preferences =
59              PortletPreferencesFactoryUtil.getPortletSetup(
60                  actionRequest, portletResource);
61  
62          if (cmd.equals("remove-footer-article")) {
63              removeFooterArticle(actionRequest, preferences);
64          }
65          else if (cmd.equals("remove-header-article")) {
66              removeHeaderArticle(actionRequest, preferences);
67          }
68          else if (cmd.equals("set-footer-article")) {
69              setFooterArticle(actionRequest, preferences);
70          }
71          else if (cmd.equals("set-header-article")) {
72              setHeaderArticle(actionRequest, preferences);
73          }
74          else if (cmd.equals(Constants.UPDATE)) {
75              updateConfiguration(actionRequest, preferences);
76          }
77  
78          if (SessionErrors.isEmpty(actionRequest)) {
79              try {
80                  preferences.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 preferences)
104         throws Exception {
105 
106         preferences.setValues(
107             "footer-article-resource-values", new String[] {"0", ""});
108     }
109 
110     protected void removeHeaderArticle(
111             ActionRequest actionRequest, PortletPreferences preferences)
112         throws Exception {
113 
114         preferences.setValues(
115             "header-article-resource-values", new String[] {"0", ""});
116     }
117 
118     protected void setFooterArticle(
119             ActionRequest actionRequest, PortletPreferences preferences)
120         throws Exception {
121 
122         String footerArticleResourcePrimKey = ParamUtil.getString(
123             actionRequest, "resourcePrimKey");
124         String footerArticleResouceTitle = ParamUtil.getString(
125             actionRequest, "resourceTitle");
126 
127         preferences.setValues(
128             "footer-article-resource-values",
129             new String[] {
130                 footerArticleResourcePrimKey, footerArticleResouceTitle
131             });
132     }
133 
134     protected void setHeaderArticle(
135             ActionRequest actionRequest, PortletPreferences preferences)
136         throws Exception {
137 
138         String headerArticleResourcePrimKey = ParamUtil.getString(
139             actionRequest, "resourcePrimKey");
140         String headerArticleResouceTitle = ParamUtil.getString(
141             actionRequest, "resourceTitle");
142 
143         preferences.setValues(
144             "header-article-resource-values",
145         new String[] {headerArticleResourcePrimKey, headerArticleResouceTitle});
146     }
147 
148     protected void updateConfiguration(
149             ActionRequest actionRequest, PortletPreferences preferences)
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         boolean showFeedTitle = ParamUtil.getBoolean(
157             actionRequest, "showFeedTitle");
158         boolean showFeedPublishedDate = ParamUtil.getBoolean(
159             actionRequest, "showFeedPublishedDate");
160         boolean showFeedDescription = ParamUtil.getBoolean(
161             actionRequest, "showFeedDescription");
162         boolean showFeedImage = ParamUtil.getBoolean(
163             actionRequest, "showFeedImage");
164         String feedImageAlignment = ParamUtil.getString(
165             actionRequest, "feedImageAlignment");
166         boolean showFeedItemAuthor = ParamUtil.getBoolean(
167             actionRequest, "showFeedItemAuthor");
168 
169         if (urls != null && titles != null) {
170             preferences.setValues("urls", urls);
171             preferences.setValues("titles", titles);
172         }
173         else {
174             preferences.setValues("urls", new String[0]);
175             preferences.setValues("titles", new String[0]);
176         }
177 
178         preferences.setValue(
179             "items-per-channel", String.valueOf(entriesPerFeed));
180         preferences.setValue("show-feed-title", String.valueOf(showFeedTitle));
181         preferences.setValue(
182             "show-feed-published-date", String.valueOf(showFeedPublishedDate));
183         preferences.setValue(
184             "show-feed-description", String.valueOf(showFeedDescription));
185         preferences.setValue("show-feed-image", String.valueOf(showFeedImage));
186         preferences.setValue(
187             "feed-image-alignment", String.valueOf(feedImageAlignment));
188         preferences.setValue(
189             "show-feed-item-author", String.valueOf(showFeedItemAuthor));
190     }
191 
192 }