001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.news.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.struts.PortletAction;
028: import com.liferay.portlet.news.model.Feed;
029: import com.liferay.portlet.news.util.NewsUtil;
030: import com.liferay.util.servlet.SessionMessages;
031:
032: import java.util.Iterator;
033: import java.util.LinkedHashSet;
034: import java.util.Map;
035: import java.util.Set;
036: import java.util.StringTokenizer;
037:
038: import javax.portlet.ActionRequest;
039: import javax.portlet.ActionResponse;
040: import javax.portlet.PortletConfig;
041: import javax.portlet.PortletPreferences;
042: import javax.portlet.RenderRequest;
043: import javax.portlet.RenderResponse;
044:
045: import org.apache.struts.action.ActionForm;
046: import org.apache.struts.action.ActionForward;
047: import org.apache.struts.action.ActionMapping;
048:
049: /**
050: * <a href="EditPreferencesAction.java.html"><b><i>View Source</i></b></a>
051: *
052: * @author Brian Wing Shun Chan
053: *
054: */
055: public class EditPreferencesAction extends PortletAction {
056:
057: public void processAction(ActionMapping mapping, ActionForm form,
058: PortletConfig config, ActionRequest req, ActionResponse res)
059: throws Exception {
060:
061: String cmd = ParamUtil.getString(req, Constants.CMD);
062:
063: if (!cmd.equals(Constants.UPDATE)) {
064: return;
065: }
066:
067: PortletPreferences prefs = req.getPreferences();
068:
069: String tabs1 = ParamUtil.getString(req, "tabs1");
070:
071: if (tabs1.equals("news-selections")) {
072: String categoryName = req.getParameter("category_name");
073: String list = req.getParameter("feeds");
074:
075: Set selFeeds = NewsUtil.getSelFeeds(prefs);
076:
077: Iterator itr = selFeeds.iterator();
078:
079: while (itr.hasNext()) {
080: Feed feed = (Feed) itr.next();
081:
082: String feedURL = feed.getFeedURL();
083:
084: int pos = list.indexOf(feedURL);
085:
086: if ((pos == -1)
087: && (feed.getCategoryName().equals(categoryName))) {
088:
089: itr.remove();
090: } else {
091: list = StringUtil.replace(list, feedURL
092: + StringPool.COMMA, StringPool.BLANK);
093: }
094: }
095:
096: Map feedMap = NewsUtil.getFeedMap();
097:
098: StringTokenizer st = new StringTokenizer(list,
099: StringPool.COMMA);
100:
101: while (st.hasMoreTokens()) {
102: Feed feed = (Feed) feedMap.get(st.nextToken());
103:
104: if (feed != null) {
105: selFeeds.add(feed);
106: }
107: }
108:
109: prefs
110: .setValues("sel-feeds", NewsUtil
111: .getSelFeeds(selFeeds));
112: } else if (tabs1.equals("display-settings")) {
113: String list = req.getParameter("feeds");
114: int articlesPerNews = ParamUtil.get(req, "apn", 4);
115:
116: prefs.setValue("articles-per-news", Integer
117: .toString(articlesPerNews));
118:
119: Set selFeeds = new LinkedHashSet();
120:
121: Map feedMap = NewsUtil.getFeedMap();
122:
123: StringTokenizer st = new StringTokenizer(list,
124: StringPool.COMMA);
125:
126: while (st.hasMoreTokens()) {
127: Feed feed = (Feed) feedMap.get(st.nextToken());
128:
129: if (feed != null) {
130: selFeeds.add(feed);
131: }
132: }
133:
134: prefs
135: .setValues("sel-feeds", NewsUtil
136: .getSelFeeds(selFeeds));
137: }
138:
139: prefs.store();
140:
141: SessionMessages.add(req, config.getPortletName() + ".doEdit");
142: }
143:
144: public ActionForward render(ActionMapping mapping, ActionForm form,
145: PortletConfig config, RenderRequest req, RenderResponse res)
146: throws Exception {
147:
148: return mapping.findForward("portlet.news.edit");
149: }
150:
151: }
|