1
22
23 package com.liferay.portlet.news.util;
24
25 import com.liferay.portal.kernel.util.HttpUtil;
26 import com.liferay.portal.kernel.util.Time;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.kernel.webcache.WebCacheException;
29 import com.liferay.portal.kernel.webcache.WebCacheItem;
30 import com.liferay.portal.util.ContentUtil;
31 import com.liferay.portlet.news.model.Feed;
32
33 import java.io.BufferedReader;
34 import java.io.IOException;
35 import java.io.StringReader;
36
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.Map;
40 import java.util.Set;
41 import java.util.TreeSet;
42
43
49 public class CategoryWebCacheItem implements WebCacheItem {
50
51 public Object convert(String url) throws WebCacheException {
52 try {
53 Map<String, Set<Feed>> categoryMap =
54 new HashMap<String, Set<Feed>>();
55 Set<String> categorySet = new TreeSet<String>();
56 Map<String, Feed> feedMap = new HashMap<String, Feed>();
57 Set<Feed> feedSet = new TreeSet<Feed>();
58
59 String xml = null;
60
61 try {
62 xml = HttpUtil.URLtoString(url);
63 }
64 catch (IOException ioe) {
65 xml = ContentUtil.get(
66 "com/liferay/portlet/news/dependencies/categories.tsv");
67 }
68
69 BufferedReader br = new BufferedReader(new StringReader(xml));
70
71 String s = null;
72
73 while ((s = br.readLine()) != null) {
74 String feedURL;
75 String fullName;
76 String shortName;
77 String categoryName;
78
79 int pos;
80
81 pos = s.indexOf("\t");
82 feedURL = s.substring(0, pos);
83
84 s = s.substring(pos + 1, s.length());
85 pos = s.indexOf("\t");
86 fullName = s.substring(0, pos);
87
88 s = s.substring(pos + 1, s.length());
89 pos = s.indexOf("\t");
90 shortName = s.substring(0, pos);
91
92 categoryName = s.substring(pos + 1, s.length());
93
94 Feed feed =
95 new Feed(feedURL, fullName, shortName, categoryName);
96
97 categorySet.add(feed.getCategoryName());
98 feedMap.put(feedURL, feed);
99 feedSet.add(feed);
100 }
101
102 categoryMap = new HashMap<String, Set<Feed>>();
103
104 String temp = "";
105 Set<Feed> tempSet = null;
106
107 for (Feed feed : feedSet) {
108 if (Validator.isNull(feed.getCategoryName())) {
109 continue;
110 }
111
112 if (temp.equals(feed.getCategoryName())) {
113 tempSet.add(feed);
114 }
115 else {
116 tempSet = new TreeSet<Feed>();
117
118 categoryMap.put(feed.getCategoryName(), tempSet);
119 tempSet.add(feed);
120 }
121
122 temp = feed.getCategoryName();
123 }
124
125 categoryMap = Collections.unmodifiableMap(categoryMap);
126 categorySet = Collections.unmodifiableSet(categorySet);
127
128 feedMap = Collections.unmodifiableMap(feedMap);
129 feedSet = Collections.unmodifiableSet(feedSet);
130
131 Object[] objArray = new Object[4];
132
133 objArray[0] = categoryMap;
134 objArray[1] = categorySet;
135 objArray[2] = feedMap;
136 objArray[3] = feedSet;
137
138 return objArray;
139 }
140 catch (Exception e) {
141 throw new WebCacheException(e);
142 }
143 }
144
145 public long getRefreshTime() {
146 return _REFRESH_TIME;
147 }
148
149 private static final long _REFRESH_TIME = Time.DAY * 3;
150
151 }