1   /**
2    * Copyright (c) 2000-2007 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.util.WebCachePool;
26  import com.liferay.portal.util.WebCacheable;
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              WebCacheable wc = new NewsConverter();
72  
73              return (News)WebCachePool.get(feed.getShortName() + ";" + xml, wc);
74          }
75      }
76  
77      public static List getNews(PortletPreferences prefs) {
78          List news = new ArrayList();
79  
80          Iterator itr = NewsUtil.getSelFeeds(prefs).iterator();
81  
82          while (itr.hasNext()) {
83              Feed feed = (Feed)itr.next();
84  
85              news.add(NewsUtil.getNews(feed.getFeedURL()));
86          }
87  
88          return news;
89      }
90  
91      public static Map getSelCategories(Set selFeeds) {
92          Map selCategories = CollectionFactory.getHashMap();
93  
94          Iterator itr = selFeeds.iterator();
95  
96          while (itr.hasNext()) {
97              Feed feed = (Feed)itr.next();
98  
99              String categoryName = feed.getCategoryName();
100 
101             if (selCategories.containsKey(categoryName)) {
102                 List feedList = (List)selCategories.get(categoryName);
103 
104                 feedList.add(feed);
105             }
106             else {
107                 List feedList = new ArrayList();
108 
109                 feedList.add(feed);
110 
111                 selCategories.put(categoryName, feedList);
112             }
113         }
114 
115         return selCategories;
116     }
117 
118     public static Set getSelFeeds(PortletPreferences prefs) {
119         Map feedMap = getFeedMap();
120 
121         Set selFeeds = new LinkedHashSet();
122 
123         String[] selFeedsArray = prefs.getValues("sel-feeds", new String[0]);
124 
125         for (int i = 0; i < selFeedsArray.length; i++) {
126             Feed feed = (Feed)feedMap.get(selFeedsArray[i]);
127 
128             selFeeds.add(feed);
129         }
130 
131         return selFeeds;
132     }
133 
134     public static String[] getSelFeeds(Set selFeeds) {
135         List list = new ArrayList();
136 
137         Iterator itr = selFeeds.iterator();
138 
139         while (itr.hasNext()) {
140             Feed feed = (Feed)itr.next();
141 
142             list.add(feed.getFeedURL());
143         }
144 
145         return (String[])list.toArray(new String[0]);
146     }
147 
148     private NewsUtil() {
149         try {
150             WebCacheable wc = new CategoryConverter();
151 
152             List list = (List)WebCachePool.get(
153                 "http://w.moreover.com/categories/category_list.tsv2", wc);
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         }
160         catch (Exception e) {
161             e.printStackTrace();
162         }
163     }
164 
165     private static NewsUtil _instance = new NewsUtil();
166 
167     private Map _categoryMap;
168     private Set _categorySet;
169     private Map _feedMap;
170     private Set _feedSet;
171 
172 }