1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. 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.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  /**
47   * <a href="CategoryWebCacheItem.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
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 }