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.news.action;
24  
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portlet.news.model.Feed;
31  import com.liferay.portlet.news.util.NewsUtil;
32  import com.liferay.util.servlet.SessionMessages;
33  
34  import java.util.Iterator;
35  import java.util.LinkedHashSet;
36  import java.util.Map;
37  import java.util.Set;
38  import java.util.StringTokenizer;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  import javax.portlet.PortletPreferences;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="EditPreferencesAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class EditPreferencesAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig config,
61              ActionRequest req, ActionResponse res)
62          throws Exception {
63  
64          String cmd = ParamUtil.getString(req, Constants.CMD);
65  
66          if (!cmd.equals(Constants.UPDATE)) {
67              return;
68          }
69  
70          PortletPreferences prefs = req.getPreferences();
71  
72          String tabs1 = ParamUtil.getString(req, "tabs1");
73  
74          if (tabs1.equals("news-selections")) {
75              String categoryName = req.getParameter("categoryName");
76              String list = req.getParameter("feeds");
77  
78              Set<Feed> selFeeds = NewsUtil.getSelFeeds(prefs);
79  
80              Iterator<Feed> itr = selFeeds.iterator();
81  
82              while (itr.hasNext()) {
83                  Feed feed = itr.next();
84  
85                  String feedURL = feed.getFeedURL();
86  
87                  int pos = list.indexOf(feedURL);
88  
89                  if ((pos == -1) &&
90                      (feed.getCategoryName().equals(categoryName))) {
91  
92                      itr.remove();
93                  }
94                  else {
95                      list = StringUtil.replace(
96                          list, feedURL + StringPool.COMMA, StringPool.BLANK);
97                  }
98              }
99  
100             Map<String, Feed> feedMap = NewsUtil.getFeedMap();
101 
102             StringTokenizer st = new StringTokenizer(list, StringPool.COMMA);
103 
104             while (st.hasMoreTokens()) {
105                 Feed feed = feedMap.get(st.nextToken());
106 
107                 if (feed != null) {
108                     selFeeds.add(feed);
109                 }
110             }
111 
112             prefs.setValues("sel-feeds", NewsUtil.getSelFeeds(selFeeds));
113         }
114         else if (tabs1.equals("display-settings")) {
115             String list = req.getParameter("feeds");
116             int articlesPerNews = ParamUtil.get(req, "apn", 4);
117 
118             prefs.setValue(
119                 "articles-per-news", Integer.toString(articlesPerNews));
120 
121             Set<Feed> selFeeds = new LinkedHashSet<Feed>();
122 
123             Map<String, Feed> feedMap = NewsUtil.getFeedMap();
124 
125             StringTokenizer st = new StringTokenizer(list, StringPool.COMMA);
126 
127             while (st.hasMoreTokens()) {
128                 Feed feed = feedMap.get(st.nextToken());
129 
130                 if (feed != null) {
131                     selFeeds.add(feed);
132                 }
133             }
134 
135             prefs.setValues("sel-feeds", NewsUtil.getSelFeeds(selFeeds));
136         }
137 
138         prefs.store();
139 
140         SessionMessages.add(req, config.getPortletName() + ".doEdit");
141     }
142 
143     public ActionForward render(
144             ActionMapping mapping, ActionForm form, PortletConfig config,
145             RenderRequest req, RenderResponse res)
146         throws Exception {
147 
148         return mapping.findForward("portlet.news.edit");
149     }
150 
151 }