1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.reverendfun.util;
16  
17  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
18  import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
19  import com.liferay.portal.kernel.util.HttpUtil;
20  import com.liferay.portal.kernel.util.StringComparator;
21  import com.liferay.portal.kernel.util.Time;
22  import com.liferay.portal.kernel.webcache.WebCacheException;
23  import com.liferay.portal.kernel.webcache.WebCacheItem;
24  
25  import java.text.DateFormat;
26  
27  import java.util.Calendar;
28  import java.util.Set;
29  import java.util.TreeSet;
30  
31  /**
32   * <a href="ReverendFunWebCacheItem.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class ReverendFunWebCacheItem implements WebCacheItem {
37  
38      public ReverendFunWebCacheItem(String date) {
39          _date = date;
40      }
41  
42      public Object convert(String key) throws WebCacheException {
43          Set<String> dates = new TreeSet<String>(
44              new StringComparator(false, true));
45  
46          try {
47              DateFormat dateFormatYMD =
48                  DateFormatFactoryUtil.getSimpleDateFormat("yyyyMMdd");
49              DateFormat dateFormatYM =
50                  DateFormatFactoryUtil.getSimpleDateFormat("yyyyMM");
51  
52              Calendar cal = CalendarFactoryUtil.getCalendar();
53  
54              cal.setTime(dateFormatYMD.parse(_date));
55              cal.set(Calendar.DATE, 1);
56  
57              Calendar now = CalendarFactoryUtil.getCalendar();
58  
59              String url = "http://www.reverendfun.com/artchives/?search=";
60  
61              while (cal.before(now)) {
62                  String text = HttpUtil.URLtoString(
63                      url + dateFormatYM.format(cal.getTime()));
64  
65                  int x = text.indexOf("date=");
66                  int y = text.indexOf("\"", x);
67  
68                  while (x != -1 && y != -1) {
69                      String fromDateString = text.substring(x + 5, y);
70  
71                      dates.add(fromDateString);
72  
73                      x = text.indexOf("date=", y);
74                      y = text.indexOf("\"", x);
75                  }
76  
77                  cal.add(Calendar.MONTH, 1);
78              }
79          }
80          catch (Exception e) {
81              throw new WebCacheException(_date + " " + e.toString());
82          }
83  
84          return dates;
85      }
86  
87      public long getRefreshTime() {
88          return _REFRESH_TIME;
89      }
90  
91      private static final long _REFRESH_TIME = Time.DAY;
92  
93      private String _date;
94  
95  }