1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.DateFormatFactoryUtil;
27  import com.liferay.portal.kernel.util.HttpUtil;
28  import com.liferay.portal.kernel.util.StringComparator;
29  import com.liferay.portal.kernel.util.Time;
30  import com.liferay.portal.kernel.webcache.WebCacheException;
31  import com.liferay.portal.kernel.webcache.WebCacheItem;
32  
33  import java.text.DateFormat;
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  public class ReverendFunWebCacheItem implements WebCacheItem {
45  
46      public ReverendFunWebCacheItem(String date) {
47          _date = date;
48      }
49  
50      public Object convert(String key) throws WebCacheException {
51          Set<String> dates = new TreeSet<String>(
52              new StringComparator(false, true));
53  
54          try {
55              DateFormat dateFormatYMD =
56                  DateFormatFactoryUtil.getSimpleDateFormat("yyyyMMdd");
57              DateFormat dateFormatYM =
58                  DateFormatFactoryUtil.getSimpleDateFormat("yyyyMM");
59  
60              Calendar cal = CalendarFactoryUtil.getCalendar();
61  
62              cal.setTime(dateFormatYMD.parse(_date));
63              cal.set(Calendar.DATE, 1);
64  
65              Calendar now = CalendarFactoryUtil.getCalendar();
66  
67              String url = "http://www.reverendfun.com/artchives/?search=";
68  
69              while (cal.before(now)) {
70                  String text = HttpUtil.URLtoString(
71                      url + dateFormatYM.format(cal.getTime()));
72  
73                  int x = text.indexOf("date=");
74                  int y = text.indexOf("\"", x);
75  
76                  while (x != -1 && y != -1) {
77                      String fromDateString = text.substring(x + 5, y);
78  
79                      dates.add(fromDateString);
80  
81                      x = text.indexOf("date=", y);
82                      y = text.indexOf("\"", x);
83                  }
84  
85                  cal.add(Calendar.MONTH, 1);
86              }
87          }
88          catch (Exception e) {
89              throw new WebCacheException(_date + " " + e.toString());
90          }
91  
92          return dates;
93      }
94  
95      public long getRefreshTime() {
96          return _REFRESH_TIME;
97      }
98  
99      private static final long _REFRESH_TIME = Time.DAY;
100 
101     private String _date;
102 
103 }