1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.reverendfun.util;
21  
22  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
23  import com.liferay.portal.kernel.util.HttpUtil;
24  import com.liferay.portal.kernel.util.StringComparator;
25  import com.liferay.portal.kernel.util.Time;
26  import com.liferay.portal.kernel.webcache.WebCacheException;
27  import com.liferay.portal.kernel.webcache.WebCacheItem;
28  
29  import java.text.DateFormat;
30  import java.text.SimpleDateFormat;
31  
32  import java.util.Calendar;
33  import java.util.Set;
34  import java.util.TreeSet;
35  
36  /**
37   * <a href="ReverendFunWebCacheItem.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class ReverendFunWebCacheItem implements WebCacheItem {
43  
44      public ReverendFunWebCacheItem(String date) {
45          _date = date;
46      }
47  
48      public Object convert(String key) throws WebCacheException {
49          Set<String> dates = new TreeSet<String>(
50              new StringComparator(false, true));
51  
52          try {
53              DateFormat dateFormatYMD = new SimpleDateFormat("yyyyMMdd");
54              DateFormat dateFormatYM = new SimpleDateFormat("yyyyMM");
55  
56              Calendar cal = CalendarFactoryUtil.getCalendar();
57  
58              cal.setTime(dateFormatYMD.parse(_date));
59              cal.set(Calendar.DATE, 1);
60  
61              Calendar now = CalendarFactoryUtil.getCalendar();
62  
63              String url = "http://www.reverendfun.com/artchives/?search=";
64  
65              while (cal.before(now)) {
66                  String text = HttpUtil.URLtoString(
67                      url + dateFormatYM.format(cal.getTime()));
68  
69                  int x = text.indexOf("date=");
70                  int y = text.indexOf("\"", x);
71  
72                  while (x != -1 && y != -1) {
73                      String fromDateString = text.substring(x + 5, y);
74  
75                      dates.add(fromDateString);
76  
77                      x = text.indexOf("date=", y);
78                      y = text.indexOf("\"", x);
79                  }
80  
81                  cal.add(Calendar.MONTH, 1);
82              }
83          }
84          catch (Exception e) {
85              throw new WebCacheException(_date + " " + e.toString());
86          }
87  
88          return dates;
89      }
90  
91      public long getRefreshTime() {
92          return _REFRESH_TIME;
93      }
94  
95      private static final long _REFRESH_TIME = Time.DAY;
96  
97      private String _date;
98  
99  }