1   /**
2    * Copyright (c) 2000-2009 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.reverendfun.util;
24  
25  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
26  import com.liferay.portal.kernel.util.HttpUtil;
27  import com.liferay.portal.kernel.util.StringComparator;
28  import com.liferay.portal.kernel.util.Time;
29  import com.liferay.portal.kernel.webcache.WebCacheException;
30  import com.liferay.portal.kernel.webcache.WebCacheItem;
31  
32  import java.text.DateFormat;
33  import java.text.SimpleDateFormat;
34  
35  import java.util.Calendar;
36  import java.util.Set;
37  import java.util.TreeSet;
38  
39  /**
40   * <a href="ReverendFunWebCacheItem.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class ReverendFunWebCacheItem implements WebCacheItem {
46  
47      public ReverendFunWebCacheItem(String date) {
48          _date = date;
49      }
50  
51      public Object convert(String key) throws WebCacheException {
52          Set<String> dates = new TreeSet<String>(
53              new StringComparator(false, true));
54  
55          try {
56              DateFormat dateFormatYMD = new SimpleDateFormat("yyyyMMdd");
57              DateFormat dateFormatYM = new SimpleDateFormat("yyyyMM");
58  
59              Calendar cal = CalendarFactoryUtil.getCalendar();
60  
61              cal.setTime(dateFormatYMD.parse(_date));
62              cal.set(Calendar.DATE, 1);
63  
64              Calendar now = CalendarFactoryUtil.getCalendar();
65  
66              String url = "http://www.reverendfun.com/artchives/?search=";
67  
68              while (cal.before(now)) {
69                  String text = HttpUtil.URLtoString(
70                      url + dateFormatYM.format(cal.getTime()));
71  
72                  int x = text.indexOf("date=");
73                  int y = text.indexOf("\"", x);
74  
75                  while (x != -1 && y != -1) {
76                      String fromDateString = text.substring(x + 5, y);
77  
78                      dates.add(fromDateString);
79  
80                      x = text.indexOf("date=", y);
81                      y = text.indexOf("\"", x);
82                  }
83  
84                  cal.add(Calendar.MONTH, 1);
85              }
86          }
87          catch (Exception e) {
88              throw new WebCacheException(_date + " " + e.toString());
89          }
90  
91          return dates;
92      }
93  
94      public long getRefreshTime() {
95          return _REFRESH_TIME;
96      }
97  
98      private static final long _REFRESH_TIME = Time.DAY;
99  
100     private String _date;
101 
102 }