001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2003 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.plugin;
021:
022: import com.ecyrd.jspwiki.*;
023: import com.ecyrd.jspwiki.providers.ProviderException;
024: import org.apache.log4j.Logger;
025:
026: import java.text.SimpleDateFormat;
027: import java.util.*;
028:
029: /**
030: * Creates a list of all weblog entries on a monthly basis.
031: *
032: * @since 1.9.21
033: */
034: public class WeblogArchivePlugin implements WikiPlugin {
035: private static Logger log = Logger
036: .getLogger(WeblogArchivePlugin.class);
037:
038: public static final String PARAM_PAGE = "page";
039:
040: private SimpleDateFormat m_monthUrlFormat;
041:
042: public String execute(WikiContext context, Map params)
043: throws PluginException {
044: WikiEngine engine = context.getEngine();
045:
046: //
047: // Parameters
048: //
049: String weblogName = (String) params.get(PARAM_PAGE);
050:
051: if (weblogName == null)
052: weblogName = context.getPage().getName();
053:
054: m_monthUrlFormat = new SimpleDateFormat("'"
055: + context.getURL(WikiContext.VIEW, weblogName,
056: "weblog.startDate='ddMMyy'&weblog.days=%d")
057: + "'");
058:
059: StringBuffer sb = new StringBuffer();
060:
061: sb.append("<div class=\"weblogarchive\">\n");
062:
063: //
064: // Collect months that have blog entries
065: //
066:
067: try {
068: Collection months = collectMonths(engine, weblogName);
069: int year = 0;
070:
071: //
072: // Output proper HTML.
073: //
074:
075: sb.append("<ul>\n");
076:
077: if (months.size() > 0) {
078: year = ((Calendar) months.iterator().next())
079: .get(Calendar.YEAR);
080:
081: sb.append("<li class=\"archiveyear\">" + year
082: + "</li>\n");
083: }
084:
085: for (Iterator i = months.iterator(); i.hasNext();) {
086: Calendar cal = (Calendar) i.next();
087:
088: if (cal.get(Calendar.YEAR) != year) {
089: year = cal.get(Calendar.YEAR);
090:
091: sb.append("<li class=\"archiveyear\">" + year
092: + "</li>\n");
093: }
094:
095: sb.append(" <li>");
096:
097: sb.append(getMonthLink(cal));
098:
099: sb.append("</li>\n");
100: }
101:
102: sb.append("</ul>\n");
103: sb.append("</div>\n");
104: } catch (ProviderException ex) {
105: log.info("Cannot get archive", ex);
106: sb.append("Cannot get archive: " + ex.getMessage());
107: }
108:
109: return sb.toString();
110: }
111:
112: private SortedSet collectMonths(WikiEngine engine, String page)
113: throws ProviderException {
114: Comparator comp = new ArchiveComparator();
115: TreeSet res = new TreeSet(comp);
116:
117: WeblogPlugin pl = new WeblogPlugin();
118:
119: List blogEntries = pl.findBlogEntries(engine.getPageManager(),
120: page, new Date(0L), new Date());
121:
122: for (Iterator i = blogEntries.iterator(); i.hasNext();) {
123: WikiPage p = (WikiPage) i.next();
124:
125: // FIXME: Not correct, should parse page creation time.
126:
127: Date d = p.getLastModified();
128: Calendar cal = Calendar.getInstance();
129: cal.setTime(d);
130: res.add(cal);
131: }
132:
133: return res;
134: }
135:
136: private String getMonthLink(Calendar day) {
137: SimpleDateFormat monthfmt = new SimpleDateFormat("MMMM");
138: String result;
139:
140: if (m_monthUrlFormat == null) {
141: result = monthfmt.format(day.getTime());
142: } else {
143: Calendar cal = (Calendar) day.clone();
144: int firstDay = cal.getActualMinimum(Calendar.DATE);
145: int lastDay = cal.getActualMaximum(Calendar.DATE);
146:
147: cal.set(Calendar.DATE, lastDay);
148: String url = m_monthUrlFormat.format(cal.getTime());
149:
150: url = TextUtil.replaceString(url, "%d", Integer
151: .toString(lastDay - firstDay + 1));
152:
153: result = "<a href=\"" + url + "\">"
154: + monthfmt.format(cal.getTime()) + "</a>";
155: }
156:
157: return result;
158:
159: }
160:
161: /**
162: * This is a simple comparator for ordering weblog archive entries.
163: * Two dates in the same month are considered equal.
164: */
165: private static class ArchiveComparator implements Comparator {
166:
167: public int compare(Object a, Object b) {
168: if (a == null || b == null || !(a instanceof Calendar)
169: || !(b instanceof Calendar)) {
170: throw new ClassCastException(
171: "Invalid calendar supplied for comparison.");
172: }
173:
174: Calendar ca = (Calendar) a;
175: Calendar cb = (Calendar) b;
176: if (ca.get(Calendar.YEAR) == cb.get(Calendar.YEAR)
177: && ca.get(Calendar.MONTH) == cb.get(Calendar.MONTH)) {
178: return 0;
179: }
180:
181: return cb.getTime().before(ca.getTime()) ? 1 : -1;
182: }
183: }
184: }
|