001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.news.util;
022:
023: import com.liferay.portal.kernel.util.Validator;
024: import com.liferay.portal.kernel.webcache.WebCacheException;
025: import com.liferay.portal.kernel.webcache.WebCacheItem;
026: import com.liferay.portal.util.ContentUtil;
027: import com.liferay.portlet.news.model.Feed;
028: import com.liferay.util.CollectionFactory;
029: import com.liferay.util.Http;
030: import com.liferay.util.Time;
031:
032: import java.io.BufferedReader;
033: import java.io.IOException;
034: import java.io.StringReader;
035:
036: import java.util.ArrayList;
037: import java.util.Collections;
038: import java.util.Iterator;
039: import java.util.List;
040: import java.util.Map;
041: import java.util.Set;
042: import java.util.TreeSet;
043:
044: /**
045: * <a href="CategoryWebCacheItem.java.html"><b><i>View Source</i></b></a>
046: *
047: * @author Brian Wing Shun Chan
048: *
049: */
050: public class CategoryWebCacheItem implements WebCacheItem {
051:
052: public Object convert(String url) throws WebCacheException {
053: try {
054: Map categoryMap = CollectionFactory.getHashMap();
055: Set categorySet = new TreeSet();
056: Map feedMap = CollectionFactory.getHashMap();
057: Set feedSet = new TreeSet();
058:
059: String xml = null;
060:
061: try {
062: xml = Http.URLtoString(url);
063: } catch (IOException ioe) {
064: xml = ContentUtil
065: .get("com/liferay/portlet/news/dependencies/categories.tsv");
066: }
067:
068: BufferedReader br = new BufferedReader(
069: new StringReader(xml));
070:
071: String s = null;
072:
073: while ((s = br.readLine()) != null) {
074: String feedURL;
075: String fullName;
076: String shortName;
077: String categoryName;
078:
079: int pos;
080:
081: pos = s.indexOf("\t");
082: feedURL = s.substring(0, pos);
083:
084: s = s.substring(pos + 1, s.length());
085: pos = s.indexOf("\t");
086: fullName = s.substring(0, pos);
087:
088: s = s.substring(pos + 1, s.length());
089: pos = s.indexOf("\t");
090: shortName = s.substring(0, pos);
091:
092: categoryName = s.substring(pos + 1, s.length());
093:
094: Feed feed = new Feed(feedURL, fullName, shortName,
095: categoryName);
096:
097: categorySet.add(feed.getCategoryName());
098: feedMap.put(feedURL, feed);
099: feedSet.add(feed);
100: }
101:
102: categoryMap = CollectionFactory.getHashMap();
103:
104: String temp = "";
105: Set tempSet = null;
106:
107: Iterator itr = feedSet.iterator();
108:
109: while (itr.hasNext()) {
110: Feed feed = (Feed) itr.next();
111:
112: if (Validator.isNull(feed.getCategoryName())) {
113: continue;
114: }
115:
116: if (temp.equals(feed.getCategoryName())) {
117: tempSet.add(feed);
118: } else {
119: tempSet = new TreeSet();
120:
121: categoryMap.put(feed.getCategoryName(), tempSet);
122: tempSet.add(feed);
123: }
124:
125: temp = feed.getCategoryName();
126: }
127:
128: categoryMap = Collections.unmodifiableMap(categoryMap);
129: categorySet = Collections.unmodifiableSet(categorySet);
130:
131: feedMap = Collections.unmodifiableMap(feedMap);
132: feedSet = Collections.unmodifiableSet(feedSet);
133:
134: List list = new ArrayList();
135:
136: list.add(categoryMap);
137: list.add(categorySet);
138: list.add(feedMap);
139: list.add(feedSet);
140:
141: return list;
142: } catch (Exception e) {
143: throw new WebCacheException(e);
144: }
145: }
146:
147: public long getRefreshTime() {
148: return _REFRESH_TIME;
149: }
150:
151: private static final long _REFRESH_TIME = Time.DAY * 3;
152:
153: }
|