View Javadoc

1   package net.sourceforge.blogentis.utils;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: HtmlCalendar.java,v 1.1 2004/10/22 17:34:05 tassos Exp $
23  //
24  
25  import java.text.FieldPosition;
26  import java.text.MessageFormat;
27  import java.text.ParseException;
28  import java.text.SimpleDateFormat;
29  import java.util.Calendar;
30  import java.util.Date;
31  import java.util.GregorianCalendar;
32  import java.util.Iterator;
33  import java.util.List;
34  
35  import net.sourceforge.blogentis.feed.Feed;
36  import net.sourceforge.blogentis.rss.RSSItem;
37  import net.sourceforge.blogentis.turbine.BlogParameterParser;
38  
39  import org.apache.ecs.html.A;
40  import org.apache.torque.TorqueException;
41  import org.apache.turbine.util.RunData;
42  
43  /***
44   * @author abas
45   */
46  public class HtmlCalendar {
47      private static final SimpleDateFormat calFormat = new SimpleDateFormat(
48          "yyyy-MM");
49  
50      private static final SimpleDateFormat monthNameFormat = new SimpleDateFormat(
51          "MMMMM");
52  
53      public static final String MONTH_PARAMETER = "calendar";
54  
55      protected RunData data;
56  
57      protected Feed feed;
58  
59      protected String weekStart, weekEnd;
60  
61      protected MessageFormat normalDayFormat = null;
62  
63      protected MessageFormat currentDayFormat = null;
64  
65      protected String next = ">>";
66  
67      protected String prev = "<<";
68  
69      protected Calendar currentMonth = null;
70  
71      private static final Object[] emptyString = {""};
72  
73      private static final FieldPosition startPosition = new FieldPosition(0);
74  
75      public HtmlCalendar(RunData data, Feed feed) {
76          this.data = data;
77          this.feed = feed;
78          feed.setLimit(0);
79          BlogParameterParser pp = (BlogParameterParser)data.getParameters();
80          if (pp.containsKey(MONTH_PARAMETER)) {
81              try {
82                  Date month = calFormat.parse(pp.getString(MONTH_PARAMETER, ""));
83                  currentMonth = new GregorianCalendar();
84                  currentMonth.setTime(month);
85              } catch (ParseException ignored) {}
86          }
87          if (currentMonth == null) {
88              if (pp.getDate() != null) {
89                  currentMonth = pp.getDate().getStart();
90              } else {
91                  currentMonth = new GregorianCalendar();
92                  currentMonth.setTime(new Date());
93              }
94          }
95      }
96  
97      public String toString() {
98          return "";
99      }
100 
101     public HtmlCalendar setWeekDelim(String start, String end) {
102         weekStart = start;
103         weekEnd = end;
104         return this;
105     }
106 
107     public HtmlCalendar setDay(String format) {
108         normalDayFormat = new MessageFormat(format);
109         return this;
110     }
111 
112     public HtmlCalendar setCurrentDay(String format) {
113         currentDayFormat = new MessageFormat(format);
114         return this;
115     }
116 
117     public HtmlCalendar setNext(String next) {
118         this.next = next;
119         return this;
120     }
121 
122     public HtmlCalendar setPrev(String prev) {
123         this.prev = prev;
124         return this;
125     }
126 
127     public String getHeading(String format) {
128         MessageFormat mf = new MessageFormat(format);
129         StringBuffer sb = new StringBuffer();
130         Calendar c = new GregorianCalendar();
131         c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
132 
133         for(int i = 0; i < 7; i++) {
134             mf.format(new Object[] {c.getTime()}, sb, startPosition);
135             c.add(Calendar.DAY_OF_WEEK, 1);
136         }
137         return sb.toString();
138     }
139 
140     private void initFormats() {
141         if (normalDayFormat == null)
142             setDay("{0}");
143         if (currentDayFormat == null)
144             setCurrentDay("*{0}*");
145     }
146 
147     private static Calendar getCopy(Calendar orig) {
148         Calendar c = new GregorianCalendar();
149         c.clear();
150         c.set(orig.get(Calendar.YEAR), orig.get(Calendar.MONTH),
151                 orig.get(Calendar.DAY_OF_MONTH));
152         return c;
153     }
154 
155     private int[] daysWithPosts(Calendar month) {
156         int[] days = new int[month.getMaximum(Calendar.DAY_OF_MONTH) + 1];
157         feed.clear();
158         feed.setLimit(0);
159         feed.setDateSpecification(new DateSpecification(
160             month.get(Calendar.YEAR), month.get(Calendar.MONTH) + 1));
161         List l = null;
162         try {
163             l = feed.getItems();
164         } catch (TorqueException e) {
165             return days;
166         }
167         Calendar c = new GregorianCalendar();
168         for(Iterator i = l.iterator(); i.hasNext();) {
169             RSSItem pi = (RSSItem)i.next();
170             c.setTime(pi.getPostedTime());
171             days[c.get(Calendar.DAY_OF_MONTH)]++;
172         }
173         return days;
174     }
175 
176     public String getHtml() {
177         initFormats();
178         StringBuffer sb = new StringBuffer(weekStart);
179         Calendar now = getCopy(new GregorianCalendar());
180         Calendar c = getCopy(currentMonth);
181         BlogParameterParser pp = (BlogParameterParser)data.getParameters();
182         int month = c.get(Calendar.MONTH);
183         int weekStartDay = c.getFirstDayOfWeek();
184         c.set(Calendar.DAY_OF_MONTH, 1);
185         AbsoluteLinkURL link = new AbsoluteLinkURL();
186         int[] days = daysWithPosts(c);
187 
188         for(int i = 1; i < c.get(Calendar.DAY_OF_WEEK); i++) {
189             normalDayFormat.format(emptyString, sb, startPosition);
190         }
191         for(int i = 0; month == c.get(Calendar.MONTH); c.add(
192                 Calendar.DAY_OF_MONTH, 1), i++) {
193             if (i > 0 && (c.get(Calendar.DAY_OF_WEEK) == weekStartDay)) {
194                 sb.append(weekEnd);
195                 sb.append(weekStart);
196             }
197             int curDay = c.get(Calendar.DAY_OF_MONTH);
198 
199             String cont = Integer.toString(curDay);
200 
201             if (days[c.get(Calendar.DAY_OF_MONTH)] > 0) {
202                 link.setBlog(pp.getBlog()).setDate(new DateSpecification(c));
203                 if (pp.getSection() != null)
204                     link.setSection(pp.getSection());
205                 cont = new A(link.toString()).addElement(cont).toString();
206             }
207 
208             MessageFormat which = now.equals(c) ? currentDayFormat
209                     : normalDayFormat;
210             which.format(new Object[] {cont}, sb, startPosition);
211         }
212         while (c.get(Calendar.DAY_OF_WEEK) != c.getFirstDayOfWeek()) {
213             normalDayFormat.format(emptyString, sb, startPosition);
214             c.add(Calendar.DAY_OF_WEEK, 1);
215         }
216         sb.append(weekEnd);
217         return sb.toString();
218     }
219 
220     public String getMonthName() {
221         return monthNameFormat.format(currentMonth.getTime());
222     }
223 
224     public String getPrevLink() {
225         Calendar c = getCopy(currentMonth);
226         c.add(Calendar.MONTH, -1);
227         return new A(new AbsoluteLinkURL().thisPage(data).addQueryData(
228                 MONTH_PARAMETER, calFormat.format(c.getTime())).toString())
229                                                                            .addElement(
230                                                                                    prev)
231                                                                                .toString();
232     }
233 
234     public String getNextLink() {
235         Calendar c = getCopy(currentMonth);
236         c.add(Calendar.MONTH, 1);
237         return new A(new AbsoluteLinkURL().thisPage(data).addQueryData(
238                 MONTH_PARAMETER, calFormat.format(c.getTime())).toString())
239                                                                            .addElement(
240                                                                                    next)
241                                                                                .toString();
242     }
243 }