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.util;
24  
25  import com.liferay.portal.kernel.webcache.WebCacheItem;
26  import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
27  import com.liferay.portlet.news.model.Feed;
28  import com.liferay.portlet.news.model.News;
29  import com.liferay.util.CollectionFactory;
30  
31  import java.util.ArrayList;
32  import java.util.Iterator;
33  import java.util.LinkedHashSet;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Set;
37  
38  import javax.portlet.PortletPreferences;
39  
40  /**
41   * <a href="NewsUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class NewsUtil {
47  
48      public static Map getCategoryMap() {
49          return _instance._categoryMap;
50      }
51  
52      public static Set getCategorySet() {
53          return _instance._categorySet;
54      }
55  
56      public static Map getFeedMap() {
57          return _instance._feedMap;
58      }
59  
60      public static Set getFeedSet() {
61          return _instance._feedSet;
62      }
63  
64      public static News getNews(String xml) {
65          Feed feed = (Feed)NewsUtil.getFeedMap().get(xml);
66  
67          if (feed == null) {
68              return null;
69          }
70          else {
71              WebCacheItem wci = new NewsWebCacheItem();
72  
73              return (News)WebCachePoolUtil.get(
74                  feed.getShortName() + ";" + xml, wci);
75          }
76      }
77  
78      public static List getNews(PortletPreferences prefs) {
79          List news = new ArrayList();
80  
81          Iterator itr = NewsUtil.getSelFeeds(prefs).iterator();
82  
83          while (itr.hasNext()) {
84              Feed feed = (Feed)itr.next();
85  
86              news.add(NewsUtil.getNews(feed.getFeedURL()));
87          }
88  
89          return news;
90      }
91  
92      public static Map getSelCategories(Set selFeeds) {
93          Map selCategories = CollectionFactory.getHashMap();
94  
95          Iterator itr = selFeeds.iterator();
96  
97          while (itr.hasNext()) {
98              Feed feed = (Feed)itr.next();
99  
100             String categoryName = feed.getCategoryName();
101 
102             if (selCategories.containsKey(categoryName)) {
103                 List feedList = (List)selCategories.get(categoryName);
104 
105                 feedList.add(feed);
106             }
107             else {
108                 List feedList = new ArrayList();
109 
110                 feedList.add(feed);
111 
112                 selCategories.put(categoryName, feedList);
113             }
114         }
115 
116         return selCategories;
117     }
118 
119     public static Set getSelFeeds(PortletPreferences prefs) {
120         Map feedMap = getFeedMap();
121 
122         Set selFeeds = new LinkedHashSet();
123 
124         String[] selFeedsArray = prefs.getValues("sel-feeds", new String[0]);
125 
126         for (int i = 0; i < selFeedsArray.length; i++) {
127             Feed feed = (Feed)feedMap.get(selFeedsArray[i]);
128 
129             selFeeds.add(feed);
130         }
131 
132         return selFeeds;
133     }
134 
135     public static String[] getSelFeeds(Set selFeeds) {
136         List list = new ArrayList();
137 
138         Iterator itr = selFeeds.iterator();
139 
140         while (itr.hasNext()) {
141             Feed feed = (Feed)itr.next();
142 
143             list.add(feed.getFeedURL());
144         }
145 
146         return (String[])list.toArray(new String[0]);
147     }
148 
149     private NewsUtil() {
150         try {
151             WebCacheItem wci = new CategoryWebCacheItem();
152 
153             List list = (List)WebCachePoolUtil.get(
154                 "http://w.moreover.com/categories/category_list.tsv2", wci);
155 
156             _categoryMap = (Map)list.get(0);
157             _categorySet = (Set)list.get(1);
158             _feedMap = (Map)list.get(2);
159             _feedSet = (Set)list.get(3);
160         }
161         catch (Exception e) {
162             e.printStackTrace();
163         }
164     }
165 
166     private static NewsUtil _instance = new NewsUtil();
167 
168     private Map _categoryMap;
169     private Set _categorySet;
170     private Map _feedMap;
171     private Set _feedSet;
172 
173 }