1
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
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 }