KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > date > Month


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.date;
26
27 import org.radeox.util.i18n.ResourceManager;
28 import org.snipsnap.app.Application;
29 import org.snipsnap.config.Configuration;
30 import org.snipsnap.snip.Snip;
31 import org.snipsnap.snip.SnipLink;
32 import org.snipsnap.snip.SnipSpace;
33 import org.snipsnap.snip.SnipSpaceFactory;
34
35 import java.text.DateFormatSymbols JavaDoc;
36 import java.util.Calendar JavaDoc;
37 import java.util.GregorianCalendar JavaDoc;
38 import java.util.HashSet JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Locale JavaDoc;
42 import java.util.Set JavaDoc;
43
44 /**
45  * Object that generates a View of the month
46  *
47  * @author Stephan J. Schmidt
48  * @version $Id: Month.java 1735 2004-08-31 12:00:18Z leo $
49  */

50 public class Month {
51
52
53 // private String[] months = {
54
// "Januar", "Februar", "Maerz", "April",
55
// "Mai", "Juni", "Juli", "August",
56
// "September", "Oktober", "November", "Dezember"
57
// };
58
private String JavaDoc[] monthsValue = {
59     "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"
60   };
61
62   // @TODO Use locale
63
private String JavaDoc[] months;
64   private String JavaDoc[] weekDaysShort;
65   private String JavaDoc[] weekDaysLong;
66   private Locale JavaDoc locale;
67
68   /**
69    * The days in each month.
70    */

71   public final static int dom[] = {
72     31, 28, 31, 30, /* jan feb mar apr */
73     31, 30, 31, 31, /* may jun jul aug */
74     30, 31, 30, 31 /* sep oct nov dec */
75   };
76
77   public Month() {
78     locale = ResourceManager.getLocale("i18n.messages");
79     DateFormatSymbols JavaDoc symbols = new DateFormatSymbols JavaDoc(locale);
80     months = symbols.getMonths();
81     weekDaysLong = symbols.getWeekdays();
82     weekDaysShort = symbols.getShortWeekdays();
83   }
84
85   public static String JavaDoc toKey(Calendar JavaDoc calendar) {
86     return toKey(calendar.get(Calendar.YEAR),
87                  calendar.get(Calendar.MONTH) + 1,
88                  calendar.get(Calendar.DAY_OF_MONTH));
89   }
90
91   public static String JavaDoc toKey(int year, int month, int day) {
92     return year + "-" + (month < 10 ? "0" + month : "" + month) + "-" + (day < 10 ? "0" + day : "" + day);
93   }
94
95   /**
96    * Returns a set of days (String) in yyyy-mm-dd format with
97    * posts in this weblog
98    */

99   public Set JavaDoc getDays(String JavaDoc namespace, int month, int year) {
100     String JavaDoc start = toKey(year, month, 1);
101     String JavaDoc end = toKey(year, month, 31);
102     List JavaDoc snips = SnipSpaceFactory.getInstance().getByDate(namespace, start, end);
103     Iterator JavaDoc iterator = snips.iterator();
104
105     Set JavaDoc days = new HashSet JavaDoc();
106
107     while (iterator.hasNext()) {
108       Snip snip = (Snip) iterator.next();
109       days.add(snip.getName());
110     }
111     return days;
112   }
113
114   public String JavaDoc getView(boolean navigation) {
115     Calendar JavaDoc today = new GregorianCalendar JavaDoc(locale);
116     today.setTime(new java.util.Date JavaDoc());
117     return getView(today.get(Calendar.MONTH) + 1, today.get(Calendar.YEAR), navigation);
118   }
119
120   public String JavaDoc getView(int month, int year, boolean navigation) {
121
122     int nextYear = year;
123     int nextMonth = month + 1;
124     if (nextMonth == 13) {
125       nextYear++;
126       nextMonth = 1;
127     }
128     int prevYear = year;
129     int prevMonth = month - 1;
130     if (prevMonth == 0) {
131       prevYear--;
132       prevMonth = 12;
133     }
134
135     Application app = Application.get();
136     Configuration config = app.getConfiguration();
137     Snip viewedSnip = (Snip) app.getParameters().get("viewed");
138     String JavaDoc weblogName = (String JavaDoc) app.getParameters().get("calsnip");
139     if (weblogName != null) {
140       SnipSpace space = SnipSpaceFactory.getInstance();
141       if (space.exists(weblogName)) {
142         viewedSnip = SnipSpaceFactory.getInstance().load(weblogName);
143       }
144     }
145     String JavaDoc viewed = viewedSnip != null && viewedSnip.isWeblog() ? viewedSnip.getName() : config.getStartSnip();
146
147     StringBuffer JavaDoc view = new StringBuffer JavaDoc();
148     view.append("<div class=\"calendar\">");
149     view.append("<table summary=\"");
150     view.append(ResourceManager.getString("i18n.messages", "month.summary"));
151     view.append("\">");
152     view.append("<caption>");
153
154
155     if (navigation) {
156       view.append("<a HREF=\"");
157       view.append(SnipLink.getSpaceRoot()).append("/");
158       view.append(viewed);
159       view.append("?calsnip=");
160       view.append(SnipLink.encode(viewed));
161       view.append("&amp;calmonth=");
162       view.append(prevMonth);
163       view.append("&amp;calyear=");
164       view.append(prevYear);
165       view.append("\">&lt;</a> ");
166     }
167     view.append(months[month - 1]);
168     view.append(" ");
169     view.append(year);
170     view.append(" ");
171     if (navigation) {
172       view.append("<a HREF=\"");
173       view.append(SnipLink.getSpaceRoot()).append("/");
174       view.append(viewed);
175       view.append("?calsnip=");
176       view.append(SnipLink.encode(viewed));
177       view.append("&amp;calmonth=");
178       view.append(nextMonth);
179       view.append("&amp;calyear=");
180       view.append(nextYear);
181       view.append("\">&gt;</a>");
182     }
183     if (viewedSnip != null && !viewed.equals(config.getStartSnip())) {
184       view.append(" (");
185       view.append(SnipLink.cutLength(viewedSnip.getTitle(), 20));
186       view.append(")");
187     }
188     view.append("</caption>");
189
190     if (month < 1 || month > 12) {
191       throw new IllegalArgumentException JavaDoc("Month " + month + " bad, must be 1-12");
192     }
193
194     Calendar JavaDoc today = new GregorianCalendar JavaDoc(locale);
195     today.setTime(new java.util.Date JavaDoc());
196     int todayNumber = today.get(Calendar.DAY_OF_MONTH);
197
198     GregorianCalendar JavaDoc calendar = new GregorianCalendar JavaDoc(locale);
199     calendar.set(year, month - 1, 1);
200
201     // Compute how much to leave before the first day.
202
// getDay() returns 0 for Sunday, which is just right.
203
int leadGap = calendar.get(Calendar.DAY_OF_WEEK);
204     leadGap = leadGap - calendar.getFirstDayOfWeek();
205     if (leadGap < 0) {
206       leadGap = 6;
207     }
208
209     int daysInMonth = dom[month - 1];
210     if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && month == 2) {
211       ++daysInMonth;
212     }
213
214     view.append(getHeader(calendar.getFirstDayOfWeek()));
215     // view.append("<tr><td>Mo</td><td>Di</td><td>Mi</td><td>Do</td><td>Fr</td><td>Sa</td><td>So</td></tr>");
216

217     StringBuffer JavaDoc week = new StringBuffer JavaDoc();
218     // Blank out the labels before 1st day of month
219
for (int i = 0; i < leadGap; i++) {
220       week.append("<td></td>");
221     }
222
223     // Fill in numbers for the day of month.
224
Set JavaDoc days = getDays(viewed, month, year);
225
226     for (int i = 1; i <= daysInMonth; i++) {
227       String JavaDoc day = "" + i;
228
229       String JavaDoc calBlogOld = toKey(year, month, i);
230       String JavaDoc calBlogNew = viewed + "/" + calBlogOld + "/1";
231
232       if (days.contains(calBlogNew)) {
233         day = makeLink(SnipLink.encode(calBlogNew) + "?calsnip=" + SnipLink.encode(viewed) + "&amp;calmonth=" + month + "&amp;calyear=" + year, day);
234       } else if (days.contains(calBlogOld)) {
235         day = makeLink(SnipLink.encode(calBlogOld) + "?calsnip=" + SnipLink.encode(viewed) + "&amp;calmonth=" + month + "&amp;calyear=" + year, day);
236       }
237
238       if (i == todayNumber && month == today.get(Calendar.MONTH) + 1 && year == today.get(Calendar.YEAR)) {
239         day = "<span class=\"today\">" + day + "</span>";
240       }
241       week.append("<td>");
242       week.append(day);
243       week.append("</td>");
244
245       // wrap if end of line.
246
if ((leadGap + i) % 7 == 0) {
247         view.append("<tr>");
248         view.append(week.toString());
249         week.setLength(0);
250         view.append("</tr>");
251       }
252
253     }
254     view.append("<tr>");
255     view.append(week);
256     view.append("</tr>");
257     view.append("</table></div>");
258     return view.toString();
259   }
260
261   private String JavaDoc getHeader(int offset) {
262     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
263     buffer.append("<tr>");
264     for (int i = offset - 1; i < weekDaysShort.length - 2 + offset; i++) {
265       buffer.append("<th abbr=\"");
266       buffer.append(weekDaysLong[i % 7 + 1]);
267       buffer.append("\">");
268       buffer.append(weekDaysShort[i % 7 + 1]);
269       buffer.append("</th>");
270     }
271     buffer.append("</tr>");
272     return buffer.toString();
273   }
274
275   private String JavaDoc makeLink(String JavaDoc snipName, String JavaDoc view) {
276     StringBuffer JavaDoc linkBuffer = new StringBuffer JavaDoc();
277     return SnipLink.appendLinkWithRoot(linkBuffer, SnipLink.getSpaceRoot(), snipName, view).toString();
278   }
279 }
280
Popular Tags