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