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