package dk.zqz.blojsom.plugins.filetime;

import java.util.Arrays;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.blojsom.blog.BlogEntry;
import org.blojsom.blog.BlogUser;
import org.blojsom.blog.BlojsomConfiguration;
import org.blojsom.plugin.BlojsomPlugin;
import org.blojsom.plugin.BlojsomPluginException;

/**
 * @author Lars Chr. Hausmann <jazz@zqz.dk>
 * 
 * Small plugin to sort the entries, after a time stamp in the filename, or 
 * after the filenames, last modified time,.
 * slight modification to account for single blog entry
 * made by Jay Fienberg, April 9, 2004
 */
public class FiletimePlugin implements BlojsomPlugin {

  public void init(ServletConfig arg0, BlojsomConfiguration arg1)
    throws BlojsomPluginException {
     // Not doing anything
  }

  /**
   * Call the FileTimeComparator(),
   */
  public BlogEntry[] process(HttpServletRequest request,
    HttpServletResponse response,
    BlogUser user,
    Map context,
    BlogEntry[] entries)
    throws BlojsomPluginException {
    
    if (entries.length == 0) {
      return entries;
    } else if (entries.length ==1) {
	FileTimeComparator ftc = new FileTimeComparator();
	ftc.getDate(entries[0]);
	return entries;	    
    } else {
      Arrays.sort(entries, new FileTimeComparator());
      return entries;
    }
  }
  public void cleanup() throws BlojsomPluginException {
  }

  public void destroy() throws BlojsomPluginException {
  }
}
