001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2002 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: * Builds a simple weblog.
031: *
032: * @since 1.9.21
033: */
034: public class WeblogEntryPlugin implements WikiPlugin {
035: private static Logger log = Logger
036: .getLogger(WeblogEntryPlugin.class);
037:
038: public static final int MAX_BLOG_ENTRIES = 10000; // Just a precaution.
039:
040: public static final String PARAM_ENTRYTEXT = "entrytext";
041:
042: public String getNewEntryPage(WikiEngine engine, String blogName)
043: throws ProviderException {
044: SimpleDateFormat fmt = new SimpleDateFormat(
045: WeblogPlugin.DEFAULT_DATEFORMAT);
046: String today = fmt.format(new Date());
047:
048: int entryNum = findFreeEntry(engine.getPageManager(), blogName,
049: today);
050:
051: String blogPage = WeblogPlugin.makeEntryPage(blogName, today,
052: "" + entryNum);
053:
054: return blogPage;
055: }
056:
057: public String execute(WikiContext context, Map params)
058: throws PluginException {
059: String weblogName = context.getPage().getName();
060: WikiEngine engine = context.getEngine();
061:
062: StringBuffer sb = new StringBuffer();
063:
064: String entryText = (String) params.get(PARAM_ENTRYTEXT);
065: if (entryText == null)
066: entryText = "New entry";
067:
068: try {
069: String blogPage = getNewEntryPage(engine, weblogName);
070:
071: // FIXME: Generate somehow else.
072: //sb.append("<a href=\""+engine.getEditURL(blogPage)+"\">New entry</a>");
073: //sb.append("<a href=\""+engine.getBaseURL()+"NewBlogEntry.jsp?page="+engine.encodeName(weblogName)+"\">"+entryText+"</a>");
074: String url = engine.getEditURL(
075: engine.encodeName(weblogName)).replaceAll(
076: "Edit.jsp", "NewBlogEntry.jsp");
077: sb.append("<a href=\"" + url + "\">" + entryText + "</a>");
078: } catch (ProviderException e) {
079: log.error("Could not locate blog entries", e);
080: throw new PluginException("Could not locate blog entries: "
081: + e.getMessage());
082: }
083:
084: return sb.toString();
085: }
086:
087: private int findFreeEntry(PageManager mgr, String baseName,
088: String date) throws ProviderException {
089: Collection everyone = mgr.getAllPages();
090: int max = 0;
091:
092: String startString = WeblogPlugin.makeEntryPage(baseName, date,
093: "");
094:
095: for (Iterator i = everyone.iterator(); i.hasNext();) {
096: WikiPage p = (WikiPage) i.next();
097:
098: if (p.getName().startsWith(startString)) {
099: try {
100: String probableId = p.getName().substring(
101: startString.length());
102:
103: int id = Integer.parseInt(probableId);
104:
105: if (id > max) {
106: max = id;
107: }
108: } catch (NumberFormatException e) {
109: log.debug("Was not a log entry: " + p.getName());
110: }
111: }
112: }
113:
114: //
115: // Find the first page that has no page lock.
116: //
117: int idx = max + 1;
118:
119: while (idx < MAX_BLOG_ENTRIES) {
120: WikiPage page = new WikiPage(WeblogPlugin.makeEntryPage(
121: baseName, date, Integer.toString(idx)));
122: PageLock lock = mgr.getCurrentLock(page);
123:
124: if (lock == null) {
125: break;
126: }
127:
128: idx++;
129: }
130:
131: return idx;
132: }
133:
134: }
|