1
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
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 }