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.util;
022:
023: import com.liferay.portal.kernel.webcache.WebCacheItem;
024: import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
025: import com.liferay.portlet.news.model.Feed;
026: import com.liferay.portlet.news.model.News;
027: import com.liferay.util.CollectionFactory;
028:
029: import java.util.ArrayList;
030: import java.util.Iterator;
031: import java.util.LinkedHashSet;
032: import java.util.List;
033: import java.util.Map;
034: import java.util.Set;
035:
036: import javax.portlet.PortletPreferences;
037:
038: /**
039: * <a href="NewsUtil.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class NewsUtil {
045:
046: public static Map getCategoryMap() {
047: return _instance._categoryMap;
048: }
049:
050: public static Set getCategorySet() {
051: return _instance._categorySet;
052: }
053:
054: public static Map getFeedMap() {
055: return _instance._feedMap;
056: }
057:
058: public static Set getFeedSet() {
059: return _instance._feedSet;
060: }
061:
062: public static News getNews(String xml) {
063: Feed feed = (Feed) NewsUtil.getFeedMap().get(xml);
064:
065: if (feed == null) {
066: return null;
067: } else {
068: WebCacheItem wci = new NewsWebCacheItem();
069:
070: return (News) WebCachePoolUtil.get(feed.getShortName()
071: + ";" + xml, wci);
072: }
073: }
074:
075: public static List getNews(PortletPreferences prefs) {
076: List news = new ArrayList();
077:
078: Iterator itr = NewsUtil.getSelFeeds(prefs).iterator();
079:
080: while (itr.hasNext()) {
081: Feed feed = (Feed) itr.next();
082:
083: news.add(NewsUtil.getNews(feed.getFeedURL()));
084: }
085:
086: return news;
087: }
088:
089: public static Map getSelCategories(Set selFeeds) {
090: Map selCategories = CollectionFactory.getHashMap();
091:
092: Iterator itr = selFeeds.iterator();
093:
094: while (itr.hasNext()) {
095: Feed feed = (Feed) itr.next();
096:
097: String categoryName = feed.getCategoryName();
098:
099: if (selCategories.containsKey(categoryName)) {
100: List feedList = (List) selCategories.get(categoryName);
101:
102: feedList.add(feed);
103: } else {
104: List feedList = new ArrayList();
105:
106: feedList.add(feed);
107:
108: selCategories.put(categoryName, feedList);
109: }
110: }
111:
112: return selCategories;
113: }
114:
115: public static Set getSelFeeds(PortletPreferences prefs) {
116: Map feedMap = getFeedMap();
117:
118: Set selFeeds = new LinkedHashSet();
119:
120: String[] selFeedsArray = prefs.getValues("sel-feeds",
121: new String[0]);
122:
123: for (int i = 0; i < selFeedsArray.length; i++) {
124: Feed feed = (Feed) feedMap.get(selFeedsArray[i]);
125:
126: selFeeds.add(feed);
127: }
128:
129: return selFeeds;
130: }
131:
132: public static String[] getSelFeeds(Set selFeeds) {
133: List list = new ArrayList();
134:
135: Iterator itr = selFeeds.iterator();
136:
137: while (itr.hasNext()) {
138: Feed feed = (Feed) itr.next();
139:
140: list.add(feed.getFeedURL());
141: }
142:
143: return (String[]) list.toArray(new String[0]);
144: }
145:
146: private NewsUtil() {
147: try {
148: WebCacheItem wci = new CategoryWebCacheItem();
149:
150: List list = (List) WebCachePoolUtil
151: .get(
152: "http://w.moreover.com/categories/category_list.tsv2",
153: wci);
154:
155: _categoryMap = (Map) list.get(0);
156: _categorySet = (Set) list.get(1);
157: _feedMap = (Map) list.get(2);
158: _feedSet = (Set) list.get(3);
159: } catch (Exception e) {
160: e.printStackTrace();
161: }
162: }
163:
164: private static NewsUtil _instance = new NewsUtil();
165:
166: private Map _categoryMap;
167: private Set _categorySet;
168: private Map _feedMap;
169: private Set _feedSet;
170:
171: }
|