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