package com.ecyrd.jspwiki.plugin; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; import com.ecyrd.jspwiki.PageManager; import com.ecyrd.jspwiki.TextUtil; import com.ecyrd.jspwiki.WikiContext; import com.ecyrd.jspwiki.WikiEngine; import com.ecyrd.jspwiki.WikiPage; import com.ecyrd.jspwiki.WikiProvider; import com.ecyrd.jspwiki.providers.ProviderException; /** *
GategoryWeblogPlugin.java
* ** View Source *
* * @author $Author$ * @version $Reversion$ $Date$ */ public class WeblogCategoryPlugin implements WikiPlugin { private static Logger log = Logger.getLogger(WeblogPlugin.class); public static final int DEFAULT_DAYS = 7; //public static final String DEFAULT_PAGEFORMAT = "%p_blogentry_"; public static final String DEFAULT_DATEFORMAT = "ddMMyy"; //emptry for all category public static final String DEFAULT_CATEGORY = ""; public static final String PARAM_STARTDATE = "startDate"; public static final String PARAM_DAYS = "days"; public static final String PARAM_ALLOWCOMMENTS = "allowComments"; public static final String PARAM_MAXENTRIES = "maxEntries"; //public static final String PARAM_PAGE = "page"; //category param key public static final String PARAM_CATEGORY = "category"; //public static final String PARAM_MAXENTRYLENGTH = "maxEntryLength"; // public static String makeEntryPage( // String pageName, // String date, // String entryNum) { // return TextUtil.replaceString(DEFAULT_PAGEFORMAT, "%p", pageName) // + date // + "_" // + entryNum; // } // // public static String makeEntryPage(String pageName) { // return TextUtil.replaceString(DEFAULT_PAGEFORMAT, "%p", pageName); // // } // // public static String makeEntryPage(String pageName, String date) { // return TextUtil.replaceString(DEFAULT_PAGEFORMAT, "%p", pageName) // + date; // } public String execute(WikiContext context, Map params) throws PluginException { Calendar startTime; Calendar stopTime; int numDays; WikiEngine engine = context.getEngine(); // // Parse parameters. // String days; String startDay = null; boolean hasComments = false; int maxEntries; String weblogName; int maxEntryLength; // if ((weblogName = (String) params.get(PARAM_PAGE)) == null) { // weblogName = context.getPage().getName(); // } if ((days = context.getHttpParameter("weblog." + PARAM_DAYS)) == null) { days = (String) params.get(PARAM_DAYS); } if (days != null && days.equalsIgnoreCase("all")) { numDays = Integer.MAX_VALUE; } else { numDays = TextUtil.parseIntParameter(days, DEFAULT_DAYS); } if ((startDay = (String) params.get(PARAM_STARTDATE)) == null) { startDay = context.getHttpParameter("weblog." + PARAM_STARTDATE); } if (TextUtil.isPositive((String) params.get(PARAM_ALLOWCOMMENTS))) { hasComments = true; } maxEntries = TextUtil.parseIntParameter( (String) params.get(PARAM_MAXENTRIES), Integer.MAX_VALUE); //Category String category = (String) params.get(PARAM_CATEGORY); if (null == category || 0 >= category.length()) { //all category category = ""; } else { // // ;General;Java;Art; // for compare the page is in category list or not... // such as:category.indexOf(";Java;");... // category = ";" + category + ";"; } // maxEntryLength = // TextUtil.parseIntParameter( // (String) params.get(PARAM_MAXENTRYLENGTH), // Integer.MAX_VALUE); // // Determine the date range which to include. // startTime = Calendar.getInstance(); stopTime = Calendar.getInstance(); if (startDay != null) { SimpleDateFormat fmt = new SimpleDateFormat(DEFAULT_DATEFORMAT); try { Date d = fmt.parse(startDay); startTime.setTime(d); stopTime.setTime(d); } catch (ParseException e) { return "Illegal time format: " + startDay; } } // // We make a wild guess here that nobody can do millisecond // accuracy here. // startTime.add(Calendar.DAY_OF_MONTH, -numDays); startTime.set(Calendar.HOUR, 0); startTime.set(Calendar.MINUTE, 0); startTime.set(Calendar.SECOND, 0); stopTime.set(Calendar.HOUR, 23); stopTime.set(Calendar.MINUTE, 59); stopTime.set(Calendar.SECOND, 59); String result = ""; try { List blogEntries = findBlogEntries( engine.getPageManager(), //weblogName, startTime.getTime(), stopTime.getTime(), category); //generate blog entry list... result = generateBlogEntryList( engine, hasComments, maxEntries, blogEntries, category); } catch (ProviderException e) { log.error("Could not locate blog entries", e); throw new PluginException( "Could not locate blog entries: " + e.getMessage()); } //Result of blog entry list... return result; } /** * Generate blog entry list * * @param engine * @param hasComments * @param maxEntries * @param blogEntries * @return * @throws ProviderException */ private String generateBlogEntryList( WikiEngine engine, boolean hasComments, int maxEntries, List blogEntries, String categoryCfg) throws ProviderException { StringBuffer sb = new StringBuffer(); Collections.sort(blogEntries, new PageDateComparator()); SimpleDateFormat entryDateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // category set,if the category is not seted... Set categorySet = new HashSet(); //blog entry list content sb.append("