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