/*
* $Id: WeblogCalendarPlugin.java,v 1.0 2008/06/05 burden Exp $
*/
package com.ecyrd.jspwiki.plugin;
import java.util.Map;
import com.ecyrd.jspwiki.WikiContext;
import com.ecyrd.jspwiki.WikiEngine;
import javax.servlet.http.HttpSession;
/**
* WeblogCalendarPlugin is a WikiPlugin that displays a calendar for a blog.
* Most of the code is taken from the CalendarPlugin created by Christoph Grimmer,
* Lewin Chan, and Connie Chan. It is currently coded to only show for page
* names that end in 'Blog'. This was to prevent the calendar from displaying
* on pages that didn't contain a blog. This can be changed to suit your needs.
* Days that contain a blog entry are highlighted on the calendar. The days have
* hyperlinks that filter the WeblogPlugin using the http parameters,
* weblog.startDate and weblog.days. The best use of the system is achieved by
* placing the weblog calendar plugin in something like the LeftMenu WikiPage.
*
*
* The calendar object is placed in the user's HttpSession to avoid creating and
* rerendering it unnecessarily. This is a conscious design choice, and it means
* that cookieless, nonauthenticated users won't be able to enjoy
* CalendarPlugin. (This said, if the calendar is located e.g. in the LeftMenu,
* it is rerendered every time the user changes the page. Ouch.)
*
*
* The small HTML calendar display contains links to the previous and next month
* and the previous and next year. This information is coded as HTTP parameters
* into the links, and lets the user change year/month and still stay on the
* same page.
*
*
* The calendar title's year and month link to a WikiPage with the same name as
* the calendar, with information sufficient for displaying all entries for a
* year or month when using the CalendarPlugin in 'summary' mode.
*
*
*
* Examples of usage:
*
* Display a small HTML calendar, customized for ebu:
*
*
* [{WeblogCalendarPlugin}]
*
*
*
*
*
*
* CSS styles used:
*
* - calendar - (div) surrounding calendar entry (monthview mode)
*
- calendartitle - (div) surrounding calendar title bar (monthview mode)
*
- today - (a) today's day (monthview mode)
*
- selected - (a) selected day (monthview mode)
*
*
*
* @author ebu
*/
public class WeblogCalendarPlugin implements WikiPlugin {
public static final String PLUGINPARAM_VIEW = "view";
public static final String PLUGINPARAM_NAME = "name";
public static final String PLUGINPARAM_CURRENT = "current";
public static final String PLUGINPARAM_CLASS = "class";
public static final String PLUGINPARAM_LINKTO = "linkto";
public static final String PLUGINPARAM_MONTH = "month";
public static final String PLUGINPARAM_YEAR = "year";
public static final String PLUGINPARAM_DAY = "day";
public static final String PLUGINPARAM_MONTHDELTA = "dm";
public static final String PLUGINPARAM_DAYS = "days";
public static final String PLUGINPARAM_ORDER = "order";
public static final String PLUGINPARAM_DIRECTION = "direction";
public static final String EXT_MONTH = ".m";
public static final String EXT_DAY = ".d";
public static final String EXT_YEAR = ".y";
public static final String EXT_MONTHDELTA = ".dm";
public static final String VALUE_ASCENDING = "ascending";
public static final String VALUE_SUMMARY = "summary";
public static final String VALUE_MONTHVIEW = "monthview";
public static final String VALUE_UPCOMING = "upcoming";
public static final String SESSION_PREFIX = "CalPlug.";
public static final String STYLE_SUMMARY = "calendarentry";
public static final String STYLE_DHEADER = "header";
public static final String STYLE_DBODY = "body";
public static final String STYLE_DFOOTER = "footer";
/*
*
*/
public String execute(WikiContext context, Map params) throws PluginException {
String pageName = context.getPage().getName();
if (!pageName.endsWith("Blog") || pageName.equals("Blog") || pageName.equals("TemplateBlog")) {
return "";
}
WeblogCalendarPrinter cal = null;
try {
cal = getCalendar(context, pageName);
}
catch (PluginException e) {
return (e.getMessage());
}
cal.setLinkingMode(WeblogCalendarPrinter.LINK_TO_DAY);
// Does the request specify a change to the month or year? The information
// is looked up first in plugin parameters (so this plugin can be used with
// the WikiForms plugin), secondarily in the http request, and finally
// defaults
// to the current time.
String dm = (String) params.get(PLUGINPARAM_MONTHDELTA);
if (dm == null)
dm = context.getHttpParameter(pageName + EXT_MONTHDELTA);
if (dm == null)
dm = "0";
String month = (String) params.get(PLUGINPARAM_MONTH);
if (month == null)
month = context.getHttpParameter(pageName + EXT_MONTH);
String year = (String) params.get(PLUGINPARAM_YEAR);
if (year == null)
year = context.getHttpParameter(pageName + EXT_YEAR);
String day = (String) params.get(PLUGINPARAM_DAY);
if (day == null)
day = context.getHttpParameter(pageName + EXT_DAY);
// Protect agains multiple invocations per page. This happens easily if
// you have a plugin for the same calendar e.g. in a menu bar and a summary
// page.
if (!cal.requestHandled(context.getHttpRequest())) {
if (year != null)
cal.setYear(year);
if (month != null)
cal.setMonth(month);
//cal.addMonths( deltaMonth );
if (day != null)
cal.setDay(day);
}
String rval = "";
try {
rval = cal.printMonthView(context);
} catch (Exception e) {
throw new PluginException(e.toString());
}
cal.setHandledRequest(context.getHttpRequest());
return (rval);
}
/**
* Looks for a CalendarPrinter in the session, and creates one if it is
* missing.
*/
private WeblogCalendarPrinter getCalendar(WikiContext context, String pageName)
throws PluginException {
// Grab the calendar item that may exist in the context already.
WeblogCalendarPrinter cal = null;
WikiEngine engine = context.getEngine();
HttpSession session = null;
if (context.getHttpRequest() != null && (session = context.getHttpRequest().getSession()) != null) {
Object contextItem = session.getAttribute(SESSION_PREFIX + pageName);
if (contextItem == null || !(contextItem instanceof WeblogCalendarPrinter)) {
cal = new WeblogCalendarPrinter(engine, context, pageName);
} else {
cal = (WeblogCalendarPrinter) contextItem;
if (!pageName.equals(cal.getName())) {
// Whoa, interesting. Create new?
cal = new WeblogCalendarPrinter(engine, context, pageName);
}
}
session.setAttribute(SESSION_PREFIX + pageName, cal);
return (cal);
}
throw new PluginException(
"CalendarPlugin requires session information. Are your cookies enabled?");
}
}