KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > util > BlogUtils


1 /*
2  * $Id: BlogUtils.java,v 1.24 2005/01/17 21:36:45 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.util;
27
28 import java.io.File JavaDoc;
29 import java.io.FileFilter JavaDoc;
30 import java.text.SimpleDateFormat JavaDoc;
31 import java.util.Calendar JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import javax.servlet.http.HttpSession JavaDoc;
41
42 import org.apache.commons.lang.StringUtils;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46 import com.j2biz.blogunity.BlogunityManager;
47 import com.j2biz.blogunity.IConstants;
48 import com.j2biz.blogunity.dao.CalendarEntryDAO;
49 import com.j2biz.blogunity.dao.UserDAO;
50 import com.j2biz.blogunity.exception.BlogunityException;
51 import com.j2biz.blogunity.exception.BlogunityRuntimeException;
52 import com.j2biz.blogunity.i18n.I18N;
53 import com.j2biz.blogunity.i18n.I18NMessageManager;
54 import com.j2biz.blogunity.i18n.I18NStatusFactory;
55 import com.j2biz.blogunity.pojo.Blog;
56 import com.j2biz.blogunity.pojo.CalendarEntry;
57 import com.j2biz.blogunity.pojo.Category;
58 import com.j2biz.blogunity.pojo.Comment;
59 import com.j2biz.blogunity.pojo.Entry;
60 import com.j2biz.blogunity.pojo.User;
61 import com.j2biz.blogunity.web.FormError;
62 import com.j2biz.blogunity.web.FormErrorList;
63 import com.j2biz.blogunity.web.IActionResult;
64 import com.j2biz.blogunity.web.NavigationStack;
65
66 /**
67  * @author michelson
68  * @version $$
69  * @since 0.1
70  *
71  *
72  */

73 public class BlogUtils {
74     /**
75      * Logger for this class
76      */

77     private static final Log log = LogFactory.getLog(BlogUtils.class);
78
79     private static BlogUtils INSTANCE = null;
80
81     private BlogUtils() {
82     }
83
84     public static final BlogUtils getInstance() {
85         if (INSTANCE == null) {
86             INSTANCE = new BlogUtils();
87         }
88         return INSTANCE;
89     }
90
91     public static final String JavaDoc getVersion() {
92         return IConstants.VERSION;
93     }
94
95     public static final String JavaDoc getBuild() {
96         return IConstants.BUILD;
97     }
98
99     public static final String JavaDoc getCodename() {
100         return IConstants.CODENAME;
101     }
102
103     public String JavaDoc getSiteTitle() {
104         return BlogunityManager.getSystemConfiguration().getSiteTitle();
105     }
106
107     public String JavaDoc getSiteKeywords() {
108
109         return BlogunityManager.getSystemConfiguration().getSiteKeywords();
110     }
111
112     public String JavaDoc getSiteDescription() {
113
114         return BlogunityManager.getSystemConfiguration().getSiteDescription();
115     }
116
117     public int getNumberOfIndividualBlogsPerUser() {
118         return BlogunityManager.getSystemConfiguration().getIndividualBlogsPerUser();
119     }
120
121     public int getNumberOfCommunityBlogsPerUser() {
122         return BlogunityManager.getSystemConfiguration().getCommunityBlogsPerUser();
123     }
124
125     public int getTotalNumberOfBlogsPerUser() {
126
127         int individual = BlogunityManager.getSystemConfiguration().getIndividualBlogsPerUser();
128         if (individual == -1) return -1;
129
130         int community = BlogunityManager.getSystemConfiguration().getCommunityBlogsPerUser();
131         if (community == -1) return -1;
132
133         return (community + individual);
134     }
135
136     public String JavaDoc formatDate(Date JavaDoc date) {
137
138         String JavaDoc format = BlogunityManager.getSystemConfiguration().getDateFormat();
139
140         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc(format);
141         return f.format(date).toString();
142     }
143
144     public String JavaDoc formatTime(Date JavaDoc date) {
145
146         String JavaDoc format = BlogunityManager.getSystemConfiguration().getTimeFormat();
147
148         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc(format);
149         return f.format(date).toString();
150     }
151
152     public String JavaDoc formatDateTime(Date JavaDoc date) {
153
154         String JavaDoc format = BlogunityManager.getSystemConfiguration().getDatetimeFormat();
155
156         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc(format);
157         return f.format(date).toString();
158     }
159
160     /**
161      * This method is used in velocity-templates for the blog.
162      *
163      * @param date
164      * @return
165      */

166     public String JavaDoc formatYear(HttpServletRequest JavaDoc request, Date JavaDoc date) {
167         Locale JavaDoc locale = (Locale JavaDoc) request.getAttribute(IConstants.Request.LOCALE);
168         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc("yyyy", locale);
169         return f.format(date).toString();
170     }
171
172     /**
173      * This method is used in velocity-templates for the blog.
174      *
175      * @param date
176      * @return
177      */

178     public String JavaDoc formatMonth(HttpServletRequest JavaDoc request, Date JavaDoc date) {
179         Locale JavaDoc locale = (Locale JavaDoc) request.getAttribute(IConstants.Request.LOCALE);
180         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc("MMMMM yyyy", locale);
181         return f.format(date).toString();
182     }
183
184     /**
185      * This method is used in velocity-templates for the blog.
186      *
187      * @param date
188      * @return
189      */

190     public String JavaDoc formatDay(HttpServletRequest JavaDoc request, Date JavaDoc date) {
191         Locale JavaDoc locale = (Locale JavaDoc) request.getAttribute(IConstants.Request.LOCALE);
192
193         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc("EEE, dd MMMMM yyyy", locale);
194         return f.format(date).toString();
195     }
196
197     /**
198      * This method is used in velocity-templates for the blog.
199      *
200      * @param date
201      * @return
202      */

203     public String JavaDoc formatDayShort(Date JavaDoc date) {
204
205         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc("dd.MM");
206         return f.format(date).toString();
207     }
208
209     /**
210      * This method is used in velocity-templates for the blog.
211      *
212      * @param date
213      * @return
214      */

215     public String JavaDoc formatDayLink(Date JavaDoc date) {
216
217         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc("/yyyy/MM/dd");
218         return f.format(date).toString();
219     }
220
221     public boolean isValidEmailAddress(String JavaDoc email) {
222
223         String JavaDoc regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[_A-Za-z0-9-]+)";
224         return email.matches(regex);
225
226     }
227
228     public boolean isValidNickname(String JavaDoc nick) {
229
230         String JavaDoc regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*";
231         return nick.matches(regex);
232     }
233
234     /**
235      * Checks if the given name is a valid blog's name, that can be simply used
236      * within URL-address.
237      *
238      * @param name
239      * @return
240      */

241     public boolean isValidBlogname(String JavaDoc name) {
242         String JavaDoc regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*";
243         return name.matches(regex);
244     }
245
246     /**
247      * Checks if the given name is a valid entry's aliasname, that can be used
248      * within URL-address instead of entry's ID to find this entry.
249      *
250      * @param name
251      * @return
252      */

253     public boolean isValidAliasname(String JavaDoc name) {
254         String JavaDoc regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*";
255         return name.matches(regex);
256     }
257
258     public NavigationStack getNavigationStack(HttpServletRequest JavaDoc request) {
259         HttpSession JavaDoc session = request.getSession(false);
260         if (session == null) return new NavigationStack();
261
262         NavigationStack stack = (NavigationStack) session
263                 .getAttribute(IConstants.Session.NAVIGATION_STACK);
264         if (stack == null) return new NavigationStack();
265         else
266             return stack;
267     }
268
269     public String JavaDoc peekLastActionFromStack(HttpServletRequest JavaDoc request) {
270         NavigationStack stack = getNavigationStack(request);
271
272         if (stack == null || stack.empty()) return null;
273         IActionResult result = stack.peek();
274         return request.getContextPath() + result.getPath();
275     }
276
277     public String JavaDoc peekNextToLastActionFromStack(HttpServletRequest JavaDoc request) {
278         NavigationStack stack = getNavigationStack(request);
279
280         if (stack == null || stack.empty()) return null;
281         IActionResult result = stack.peekNextToLast();
282
283         if (result == null) return null;
284
285         return request.getContextPath() + result.getPath();
286     }
287
288     /**
289      * Renders html output to show requested user.
290      *
291      * @param user
292      * @param request
293      * @return
294      */

295     public String JavaDoc renderUser(User user, HttpServletRequest JavaDoc request) {
296         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
297
298         if (user == null) {
299             out.append("anonymous");
300             return out.toString();
301         }
302
303         out.append("<nobr>");
304         out.append("<img SRC=\"" + request.getContextPath() + "/images/"
305                 + ((user.getSex() == User.MALE) ? "male.gif" : "female.gif") + "\">");
306         out.append("<a HREF=\"" + request.getContextPath() + "/users/" + user.getNickname()
307                 + "\" title=\"" + user.getFirstname() + " " + user.getLastname() + "\">");
308         out.append(user.getNickname());
309         out.append("</a>");
310
311         // render rank for user
312
if (BlogunityManager.getSystemConfiguration().isRankingOn()) {
313             out.append("<sup>");
314             out.append("<img SRC=\"");
315             out.append(request.getContextPath() + "/images/ranks/newbie1.gif");
316             out.append("\" title=\"just a simple user ranking\">");
317             out.append("</sup>");
318         }
319
320         User loggedUser = null;
321         HttpSession JavaDoc session = request.getSession(false);
322         if (session != null) loggedUser = (User) session.getAttribute(IConstants.Session.USER);
323
324         if (loggedUser != null && loggedUser.getId().longValue() != user.getId().longValue()) {
325
326             try {
327                 // get logged user from session
328
UserDAO dao = new UserDAO();
329                 loggedUser = dao.getUserByID(loggedUser.getId());
330             } catch (BlogunityException e) {
331                 return out.toString();
332             }
333
334             if (loggedUser.containsAsFriend(user)) {
335                 out.append("<sup><a HREF=\"" + request.getContextPath()
336                         + "/my/deleteFriendConfirm?id=" + user.getId()
337                         + "\" title=\"remove from friends\">" + "<img SRC=\""
338                         + request.getContextPath() + "/images/friend_remove.gif\" border=\"0\">"
339                         + "</a></sup>");
340             } else {
341                 out.append("<sup><a HREF=\"" + request.getContextPath() + "/my/addFriend?nickname="
342                         + user.getNickname() + "\" title=\"add to friends\">" + "<img SRC=\""
343                         + request.getContextPath() + "/images/friend_add.gif\" border=\"0\">"
344                         + "</a></sup>");
345             }
346         }
347         out.append("</nobr>");
348         return out.toString();
349
350     }
351
352     /**
353      * Renders html output to show requested category.
354      *
355      * @param b
356      * @param category
357      * @param request
358      * @return
359      */

360     public String JavaDoc renderCategory(Blog b, Category category, HttpServletRequest JavaDoc request) {
361
362         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
363         out.append("<nobr>");
364         out.append("<a HREF=\""
365                 + BlogunityManager.getBase()
366                 + "/blogs/"
367                 + b.getUrlName()
368                 + "/category/"
369                 + category.getId()
370                 + "\" title=\""
371                 + (StringUtils.isEmpty(category.getDescription()) ? category.getName() : category
372                         .getDescription()) + "\">");
373         out.append(category.getName());
374
375         out.append("</a>");
376         out.append("<sup>");
377         out.append((category.getType() == Category.GLOBAL) ? "<img SRC=\""
378                 + request.getContextPath()
379                 + "/images/category_global.gif\" title=\"global blogunity category\">"
380                 : "<img SRC=\"" + request.getContextPath()
381                         + "/images/category_local.gif\" title=\"local blog category\">");
382         out.append("</sup>");
383         out.append("</nobr>");
384         return out.toString();
385     }
386
387     /**
388      * Renders html output to show requested blog.
389      *
390      * @param blog
391      * @param request
392      * @return
393      */

394     public String JavaDoc renderBlog(Blog blog, HttpServletRequest JavaDoc request) {
395
396         String JavaDoc style = "individual_blog";
397         String JavaDoc title = "individual blog";
398         if (blog.getType() == Blog.COMMUNITY_BLOG) {
399             if (blog.getCommunityType() == Blog.PUBLIC_COMMUNTIY) {
400                 style = "public_community_blog";
401                 title = "public community blog";
402             } else if (blog.getCommunityType() == Blog.PRIVATE_COMMUNTIY) {
403                 style = "private_community_blog";
404                 title = "private community blog";
405             }
406         }
407
408         String JavaDoc icon = BlogunityManager.getBase() + "/images/" + style + ".gif";
409         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
410         out.append("<nobr>");
411         out.append("<img SRC=\"" + icon + "\"/>&nbsp;");
412         out.append("<a class=\"" + style + "\" HREF=\"" + BlogunityManager.getBase() + "/blogs/"
413                 + blog.getUrlName() + "\" title=\"" + title + "\">");
414         out.append(blog.getFullName());
415         out.append("</a>");
416         out.append("</nobr>");
417         return out.toString();
418
419     }
420
421     /**
422      * Renders html output to show blog's entry.
423      *
424      * @param entry
425      * @param request
426      * @return
427      */

428     public String JavaDoc renderEntry(Entry entry, HttpServletRequest JavaDoc request) {
429         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
430         out.append("<nobr>");
431         String JavaDoc icon = BlogunityManager.getBase() + "/images/entry.gif";
432         out.append("<img SRC=\"" + icon + "\"/>&nbsp;");
433         out.append("<a class=\"entry\" HREF=\"" + BlogunityManager.getBase() + "/blogs/"
434                 + entry.getBlog().getUrlName() + entry.getPermalink() + "\" title=\""
435                 + entry.getTitle() + "\">");
436         out.append(StringUtils.abbreviate(entry.getTitle(), 100));
437         out.append("</a>");
438         out.append("</nobr>");
439         return out.toString();
440     }
441
442     /**
443      * Renders html output to show entry's comment.
444      *
445      * @param comment
446      * @param request
447      * @return
448      */

449     public String JavaDoc renderComment(Comment comment, HttpServletRequest JavaDoc request) {
450         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
451         out.append("<nobr>");
452
453         String JavaDoc icon = BlogunityManager.getBase() + "/images/comment.gif";
454         out.append("<img SRC=\"" + icon + "\"/>&nbsp;");
455         out.append("<a class=\"comment\" HREF=\"" + BlogunityManager.getBase() + "/blogs/"
456                 + comment.getCommentedEntry().getBlog().getUrlName()
457                 + comment.getCommentedEntry().getPermalink() + "#" + comment.getId()
458                 + "\" title=\"" + comment.getTitle() + "\">");
459
460         out.append(StringUtils.abbreviate(comment.getTitle(), 100));
461         out.append("</a>");
462         out.append("</nobr>");
463         return out.toString();
464     }
465
466     public String JavaDoc showErrors(FormErrorList errors, String JavaDoc key) {
467         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
468         if (errors != null && errors.size() > 0) {
469             for (Iterator JavaDoc iter = errors.get(key).iterator(); iter.hasNext();) {
470                 FormError err = (FormError) iter.next();
471                 out.append(err.getValue() + "<br/>");
472             }
473         }
474         return out.toString();
475     }
476
477     public String JavaDoc showErrorsLayer(FormErrorList errors, String JavaDoc key) {
478         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
479         if (errors != null && errors.containsErrorsForKey(key)) {
480             out.append("<div id=\"");
481             out.append("errorLayer");
482             out.append(StringUtils.capitalize(key));
483             out.append("\"");
484             out
485                     .append("style=\"display: none; visibility: hidden; position: relative; padding-bottom: 5px;");
486             out
487                     .append("padding-top: 0px; padding-left: 7px; margin-bottom: 5px; margin-top: 0px; color: #A1231F;\">");
488             out.append(showErrors(errors, key));
489             out.append("</div>");
490         }
491
492         return out.toString();
493     }
494
495     public String JavaDoc drawCalendar(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
496
497         Locale JavaDoc locale = (Locale JavaDoc) request.getAttribute(IConstants.Request.LOCALE);
498
499         // String era = request.getParameter("era");
500
Blog blog = (Blog) request.getAttribute("blog");
501         if (blog == null) {
502             return I18NMessageManager.getInstance().getError(I18N.ERRORS.DRAWING_CALENDAR_FAILED);
503         }
504
505         Date JavaDoc now = new Date JavaDoc();
506         Date JavaDoc requestedDate = new Date JavaDoc();
507
508         // now, try to find, what the date was requested
509

510         String JavaDoc baseWithBlogname = BlogunityManager.getBase() + "/blogs/" + blog.getUrlName();
511         String JavaDoc url = StringUtils.substringAfter(request.getRequestURL().toString(),
512                 baseWithBlogname);
513
514         if (url.matches(IConstants.PATTERNS.GLOBAL_CALENDAR_PATTERN)) {
515
516             if (url.startsWith("/")) url = url.substring(1);
517
518             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(url, "/");
519
520             int year = 2005;
521             int month = Calendar.JANUARY;
522             int day = 1;
523
524             // year token
525
if (st.hasMoreTokens()) {
526                 String JavaDoc yearStr = st.nextToken();
527                 try {
528                     year = Integer.parseInt(yearStr);
529                 } catch (NumberFormatException JavaDoc e) {
530                     year = 2005;
531                 }
532             }
533
534             // month token
535
if (st.hasMoreTokens()) {
536                 String JavaDoc monthStr = st.nextToken();
537                 try {
538                     month = Integer.parseInt(monthStr) - 1;
539                     if (month < Calendar.JANUARY || month > Calendar.DECEMBER)
540                             month = Calendar.JANUARY;
541                 } catch (NumberFormatException JavaDoc e) {
542                     month = Calendar.JANUARY;
543                 }
544             }
545
546             // day token
547
if (st.hasMoreTokens()) {
548                 String JavaDoc dayStr = st.nextToken();
549                 try {
550                     day = Integer.parseInt(dayStr);
551                     if (day < 1 || day > 31) day = 1;
552                 } catch (NumberFormatException JavaDoc e) {
553                     day = 1;
554                 }
555             }
556
557             Calendar JavaDoc dayObj = Calendar.getInstance();
558             dayObj.set(year, month, day, 0, 0, 0);
559             requestedDate = dayObj.getTime();
560         }
561
562         Calendar JavaDoc currentMonth = Calendar.getInstance();
563         currentMonth.setTime(requestedDate);
564
565         Calendar JavaDoc prevMonth = Calendar.getInstance();
566         prevMonth.setTime(requestedDate);
567         prevMonth.add(Calendar.MONTH, -1);
568
569         Calendar JavaDoc nextMonth = Calendar.getInstance();
570         nextMonth.setTime(requestedDate);
571         nextMonth.add(Calendar.MONTH, 1);
572
573         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
574
575         out.append("<table width=\"100%\" cellpadding=\"1\" cellspacing=\"1\">");
576
577         // ----------- CALENDAR NAVIGATION -------------------
578
out.append("<tr>");
579         out.append("<td colspan=\"7\" align=\"center\">");
580         int temp = (prevMonth.get(Calendar.MONTH) + 1);
581         out.append("<a HREF=\"" + baseWithBlogname + "/" + prevMonth.get(Calendar.YEAR) + "/"
582                 + ((temp < 10) ? "0" + temp : "" + temp) + "\">&lt;</a>&nbsp;&nbsp;");
583
584         temp = (currentMonth.get(Calendar.MONTH) + 1);
585         out.append("<a HREF=\"" + baseWithBlogname + "/" + currentMonth.get(Calendar.YEAR) + "/"
586                 + ((temp < 10) ? "0" + temp : "" + temp) + "\">"
587                 + new SimpleDateFormat JavaDoc("MMMM", locale).format(currentMonth.getTime())
588                 + "</a>&nbsp;");
589
590         String JavaDoc monthFormat = new SimpleDateFormat JavaDoc("yyyy", locale).format(currentMonth.getTime());
591         out.append("<a HREF=\"" + baseWithBlogname + "/" + currentMonth.get(Calendar.YEAR) + "\">"
592                 + monthFormat + "</a>&nbsp;&nbsp;");
593
594         temp = (nextMonth.get(Calendar.MONTH) + 1);
595         out.append("<a HREF=\"" + baseWithBlogname + "/" + nextMonth.get(Calendar.YEAR) + "/"
596                 + ((temp < 10) ? "0" + temp : "" + temp) + "\">&gt;</a>&nbsp;&nbsp;");
597
598         out.append("</td>");
599         out.append("</tr>");
600
601         // ---------------- CALENDAR HEADER
602
SimpleDateFormat JavaDoc fmt = new SimpleDateFormat JavaDoc("EE", locale);
603         Calendar JavaDoc daysOfWeek = Calendar.getInstance();
604         daysOfWeek.setTime(currentMonth.getTime());
605
606         int FIRST_DAY_OF_WEEK = daysOfWeek.getFirstDayOfWeek();
607
608         for (int i = 1; i < 20; i++) {
609             daysOfWeek.add(Calendar.DAY_OF_MONTH, 1);
610             if (daysOfWeek.get(Calendar.DAY_OF_WEEK) == FIRST_DAY_OF_WEEK) break;
611         }
612         out.append("<tr>\n");
613         out.append("<td><b>" + fmt.format(daysOfWeek.getTime()) + "</b></td>\n");
614         daysOfWeek.add(Calendar.DAY_OF_MONTH, 1);
615         out.append("<td><b>" + fmt.format(daysOfWeek.getTime()) + "</b></td>\n");
616         daysOfWeek.add(Calendar.DAY_OF_MONTH, 1);
617         out.append("<td><b>" + fmt.format(daysOfWeek.getTime()) + "</b></td>\n");
618         daysOfWeek.add(Calendar.DAY_OF_MONTH, 1);
619         out.append("<td><b>" + fmt.format(daysOfWeek.getTime()) + "</b></td>\n");
620         daysOfWeek.add(Calendar.DAY_OF_MONTH, 1);
621         out.append("<td><b>" + fmt.format(daysOfWeek.getTime()) + "</b></td>\n");
622         daysOfWeek.add(Calendar.DAY_OF_MONTH, 1);
623         out.append("<td><b>" + fmt.format(daysOfWeek.getTime()) + "</b></td>\n");
624         daysOfWeek.add(Calendar.DAY_OF_MONTH, 1);
625         out.append("<td><b>" + fmt.format(daysOfWeek.getTime()) + "</b></td>\n");
626         out.append("</tr>\n");
627
628         // -------- CALENDAR BODY
629
Calendar JavaDoc today = Calendar.getInstance();
630         today.setTime(currentMonth.getTime());
631         today.set(Calendar.DAY_OF_MONTH, 1);
632
633         int spacer = (today.get(Calendar.DAY_OF_WEEK) - FIRST_DAY_OF_WEEK);
634
635         int month = today.get(Calendar.MONTH);
636         if (spacer < 0) spacer = 7 + spacer;
637
638         out.append("<tr>\n");
639
640         if (spacer > 0) out.append("<td colspan=\"" + spacer + "\">&nbsp;</td>\n");
641
642         List JavaDoc calendarEntries;
643         try {
644             CalendarEntryDAO dao = new CalendarEntryDAO();
645             calendarEntries = dao.getCalendarEntryForBlogByMonthYear(blog, today
646                     .get(Calendar.MONTH) + 1, today.get(Calendar.YEAR));
647         } catch (BlogunityException e) {
648             throw new BlogunityRuntimeException(I18NStatusFactory.create(
649                     I18N.ERRORS.DRAWING_CALENDAR_FAILED, e));
650         }
651
652         int currentWeek = today.get(Calendar.WEEK_OF_MONTH);
653         while (today.get(Calendar.MONTH) == month) {
654
655             if (currentWeek != today.get(Calendar.WEEK_OF_MONTH)) {
656                 out.append("</tr>\n");
657                 out.append("<tr>\n");
658                 currentWeek = today.get(Calendar.WEEK_OF_MONTH);
659             }
660
661             out.append("<td>");
662
663             String JavaDoc _day = (today.get(Calendar.DAY_OF_MONTH) < 10) ? "0"
664                     + today.get(Calendar.DAY_OF_MONTH) : "" + today.get(Calendar.DAY_OF_MONTH);
665
666             CalendarEntry e = findEntryForDay(calendarEntries, today.get(Calendar.DAY_OF_MONTH));
667             if (e != null) {
668                 // link to day
669
String JavaDoc _year = String.valueOf(today.get(Calendar.YEAR));
670
671                 int x = today.get(Calendar.MONTH) + 1;
672                 String JavaDoc _month = (x < 10) ? "0" + x : "" + x;
673
674                 out.append("<a HREF=\"" + baseWithBlogname + "/" + _year + "/" + _month + "/"
675                         + _day + "\" title=\"" + e.getNumberOfMessages() + " entries\">");
676                 out.append(_day);
677                 out.append("</a>");
678             } else {
679                 out.append(_day);
680             }
681             out.append("</td>\n");
682
683             today.add(Calendar.DAY_OF_MONTH, 1);
684         }
685
686         spacer = (today.get(Calendar.DAY_OF_WEEK) - today.getFirstDayOfWeek());
687         if (spacer < 0) spacer = 7 + spacer;
688         if (spacer > 0) out.append("<td colspan=\"" + (7 - spacer) + "\">&nbsp;</td>\n");
689         out.append("</tr>\n");
690
691         // ----------- CALENDAR FOOTER -------------------
692

693         out.append("<tr>");
694         out.append("<td colspan=\"7\" align=\"center\">");
695         out.append("<a HREF=\"" + baseWithBlogname
696                 + new SimpleDateFormat JavaDoc("/yyyy/MM/dd").format(now) + "\">"
697                 + I18NMessageManager.getInstance().getMessage("CALENDAR_TODAY", locale)
698                 + "</a>&nbsp;");
699         out.append("|&nbsp;<a HREF=\"" + baseWithBlogname + "\">"
700                 + I18NMessageManager.getInstance().getMessage("CALENDAR_FRONTPAGE", locale)
701                 + "</a>");
702         out.append("</td></tr>");
703
704         out.append("</table>");
705
706         return out.toString();
707
708     }
709
710     public File JavaDoc[] getThemeDirectories() throws BlogunityException {
711         File JavaDoc defaultThemesDir = new File JavaDoc(BlogunityManager.getServletContext().getRealPath(
712                 IConstants.BLOG_THEMES_DIRECTORY));
713
714         if (!defaultThemesDir.exists() || !defaultThemesDir.isDirectory()
715                 || !defaultThemesDir.canRead()) { throw new BlogunityException(I18NStatusFactory
716                 .create(I18N.ERRORS.THEME_READ_ERROR)); }
717
718         File JavaDoc[] themeDirs = defaultThemesDir.listFiles(new FileFilter JavaDoc() {
719             public boolean accept(File JavaDoc f) {
720                 if (f.isDirectory() && f.canRead()) {
721                     if (f.list().length <= 0) return false;
722                     return true;
723                 }
724                 return false;
725             }
726         });
727
728         return themeDirs;
729     }
730
731     private CalendarEntry findEntryForDay(List JavaDoc entries, int day) {
732
733         for (Iterator JavaDoc it = entries.iterator(); it.hasNext();) {
734             CalendarEntry e = (CalendarEntry) it.next();
735             if (e.getDay() == day) return e;
736         }
737
738         return null;
739     }
740
741 }
Popular Tags