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