Software developer at [Scandiatransplant|http://www.scandiatransplant.org]

[Personal page there|http://biobase.dk/~tra]

----

Quick hack for sorted list of a Wiki repository.  Add "?c=100" to get the 100 latest.  Path to repository is hardcoded.  

{{{
<%@page
   import="java.io.*, java.util.*"
   contentType="text/html"
%><%!

  String[] fileurlmappings = { 
      "/export/spare/home/ravn/wiki/tra", "/wiki/Wiki.jsp?page="
  };

  String dir = ".";

  %><%

  class Single_Entry {
    File f;
    String url;
    Single_Entry( File f, String url) {
      this.f = f;  
      this.url = url;
    }
  }

  int requested_number_of_items;

  List<Single_Entry> entries = new ArrayList<Single_Entry>();

  try {
    requested_number_of_items = Integer.parseInt(request.getParameter("c"));
  } catch (NumberFormatException e) {
    requested_number_of_items = 10;
 }

  // Gather file info

  for( int i = 0; i < fileurlmappings.length; i += 2) {

    File dir = new File( fileurlmappings[i]);
    if (dir.isDirectory()) {
      File[] files = dir.listFiles();
      for( int j = 0; j < files.length; j++) {
//        System.out.println( j + " " + files[j].getName());
        // TODO: URL is not protected
        if (! files[j].isDirectory()) {

          entries.add( new Single_Entry( files[j], fileurlmappings[ i+1] + files[j].getName()));
        }
      }
   } else {
    System.out.println( "error: " + dir.getName() + " is not a directory");
   }
  }

  // Sort.  Put newest first

  java.util.Collections.sort( entries, new Comparator<Single_Entry>() {
      public int compare( Single_Entry a, Single_Entry b) {
         if (a.f.lastModified() < b.f.lastModified())
           return 1;
         if (a.f.lastModified() > b.f.lastModified())
           return -1;
         return 0;
      }

      });
      
  int number_to_show = Math.min(entries.size(), requested_number_of_items);

%>
<dl>
<%
  for( int i = 0; i < number_to_show; i++) {
    Single_Entry se = entries.get(i);
    String entryName = se.f.getName();
    if (entryName.endsWith(".txt")) {
      entryName = entryName.substring( 0, entryName.length() - ".txt".length());
      se.url = se.url.substring( 0, se.url.length() - ".txt".length());
    }
    if (entryName.length() > 23) {
      entryName = entryName.substring( 0, 20) + "...";
    }



%>
   <dt>
     <a href="<%= se.url %>"><%= entryName %></a>
   </dt>
   <dt>
      <font size="-2" color="#999999"><%

// Voodoo to get short times for today and yesterday

 Calendar midnight_today = Calendar.getInstance();
 midnight_today.set( Calendar.HOUR_OF_DAY, 0);
 midnight_today.set( Calendar.MINUTE, 0);
 Calendar midnight_yesteday = Calendar.getInstance();
 midnight_yesteday.set( Calendar.HOUR_OF_DAY, 0);
 midnight_yesteday.set( Calendar.MINUTE, 0);
 midnight_yesteday.add( Calendar.DAY_OF_MONTH, -1);

 Calendar c = Calendar.getInstance();
 c.setTimeInMillis( se.f.lastModified());
 if ( c.before( midnight_yesteday)) {
%><%= new java.util.Date( se.f.lastModified()) %><% } else if (c.before(midnight_today)) {
%>Yesteday <%= c.get(Calendar.HOUR_OF_DAY) %>:<%= c.get(Calendar.MINUTE) %><% } else {
%><%= c.get(Calendar.HOUR_OF_DAY) %>:<%= c.get(Calendar.MINUTE) %><% } %>
</font>
   </dt>
<% } %>
</dl>


}}}

I am not claiming this is pretty or complete, but it is an alternative which might be interesting for some.


----
CategoryHomepage