1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.rss.action;
21  
22  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
23  import com.liferay.portal.kernel.servlet.SessionErrors;
24  import com.liferay.portal.kernel.servlet.SessionMessages;
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portlet.PortletPreferencesFactoryUtil;
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   */
43  public class ConfigurationActionImpl extends BaseConfigurationAction {
44  
45      public void processAction(
46              PortletConfig portletConfig, ActionRequest actionRequest,
47              ActionResponse actionResponse)
48          throws Exception {
49  
50          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
51  
52          String portletResource = ParamUtil.getString(
53              actionRequest, "portletResource");
54  
55          PortletPreferences preferences =
56              PortletPreferencesFactoryUtil.getPortletSetup(
57                  actionRequest, portletResource);
58  
59          if (cmd.equals("remove-footer-article")) {
60              removeFooterArticle(actionRequest, preferences);
61          }
62          else if (cmd.equals("remove-header-article")) {
63              removeHeaderArticle(actionRequest, preferences);
64          }
65          else if (cmd.equals("set-footer-article")) {
66              setFooterArticle(actionRequest, preferences);
67          }
68          else if (cmd.equals("set-header-article")) {
69              setHeaderArticle(actionRequest, preferences);
70          }
71          else if (cmd.equals(Constants.UPDATE)) {
72              updateConfiguration(actionRequest, preferences);
73          }
74  
75          if (SessionErrors.isEmpty(actionRequest)) {
76              try {
77                  preferences.store();
78              }
79              catch (ValidatorException ve) {
80                  SessionErrors.add(
81                      actionRequest, ValidatorException.class.getName(), ve);
82  
83                  return;
84              }
85  
86              SessionMessages.add(
87                  actionRequest, portletConfig.getPortletName() + ".doConfigure");
88          }
89      }
90  
91      public String render(
92              PortletConfig portletConfig, RenderRequest renderRequest,
93              RenderResponse renderResponse)
94          throws Exception {
95  
96          return "/html/portlet/rss/configuration.jsp";
97      }
98  
99      protected void removeFooterArticle(
100             ActionRequest actionRequest, PortletPreferences preferences)
101         throws Exception {
102 
103         preferences.setValues(
104             "footer-article-resource-values", new String[] {"0", ""});
105     }
106 
107     protected void removeHeaderArticle(
108             ActionRequest actionRequest, PortletPreferences preferences)
109         throws Exception {
110 
111         preferences.setValues(
112             "header-article-resource-values", new String[] {"0", ""});
113     }
114 
115     protected void setFooterArticle(
116             ActionRequest actionRequest, PortletPreferences preferences)
117         throws Exception {
118 
119         String footerArticleResourcePrimKey = ParamUtil.getString(
120             actionRequest, "resourcePrimKey");
121         String footerArticleResouceTitle = ParamUtil.getString(
122             actionRequest, "resourceTitle");
123 
124         preferences.setValues(
125             "footer-article-resource-values",
126             new String[] {
127                 footerArticleResourcePrimKey, footerArticleResouceTitle
128             });
129     }
130 
131     protected void setHeaderArticle(
132             ActionRequest actionRequest, PortletPreferences preferences)
133         throws Exception {
134 
135         String headerArticleResourcePrimKey = ParamUtil.getString(
136             actionRequest, "resourcePrimKey");
137         String headerArticleResouceTitle = ParamUtil.getString(
138             actionRequest, "resourceTitle");
139 
140         preferences.setValues(
141             "header-article-resource-values",
142         new String[] {headerArticleResourcePrimKey, headerArticleResouceTitle});
143     }
144 
145     protected void updateConfiguration(
146             ActionRequest actionRequest, PortletPreferences preferences)
147         throws Exception {
148 
149         String[] urls = actionRequest.getParameterValues("url");
150         String[] titles = actionRequest.getParameterValues("title");
151         int entriesPerFeed = ParamUtil.getInteger(
152             actionRequest, "entriesPerFeed", 4);
153         int expandedEntriesPerFeed = ParamUtil.getInteger(
154             actionRequest, "expandedEntriesPerFeed", 1);
155         boolean showFeedTitle = ParamUtil.getBoolean(
156             actionRequest, "showFeedTitle");
157         boolean showFeedPublishedDate = ParamUtil.getBoolean(
158             actionRequest, "showFeedPublishedDate");
159         boolean showFeedDescription = ParamUtil.getBoolean(
160             actionRequest, "showFeedDescription");
161         boolean showFeedImage = ParamUtil.getBoolean(
162             actionRequest, "showFeedImage");
163         String feedImageAlignment = ParamUtil.getString(
164             actionRequest, "feedImageAlignment");
165         boolean showFeedItemAuthor = ParamUtil.getBoolean(
166             actionRequest, "showFeedItemAuthor");
167 
168         if (urls != null && titles != null) {
169             preferences.setValues("urls", urls);
170             preferences.setValues("titles", titles);
171         }
172         else {
173             preferences.setValues("urls", new String[0]);
174             preferences.setValues("titles", new String[0]);
175         }
176 
177         preferences.setValue(
178             "items-per-channel", String.valueOf(entriesPerFeed));
179         preferences.setValue(
180             "expanded-items-per-channel",
181             String.valueOf(expandedEntriesPerFeed));
182         preferences.setValue("show-feed-title", String.valueOf(showFeedTitle));
183         preferences.setValue(
184             "show-feed-published-date", String.valueOf(showFeedPublishedDate));
185         preferences.setValue(
186             "show-feed-description", String.valueOf(showFeedDescription));
187         preferences.setValue("show-feed-image", String.valueOf(showFeedImage));
188         preferences.setValue(
189             "feed-image-alignment", String.valueOf(feedImageAlignment));
190         preferences.setValue(
191             "show-feed-item-author", String.valueOf(showFeedItemAuthor));
192     }
193 
194 }