/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.servlets.format;

import java.io.*;
import java.util.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.core.*;

public class AuditDataViewer implements ContentRender, ColorSelectors
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private List audit_list;
  private int offset;
  private int total_count;
  private int last_index;
  private String title;
  private String next_url;
  private String prev_url;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public AuditDataViewer(VeniceEngine engine, List audit_list, int offset, int total_count, String title,
			 String template_url)
  {
    this.audit_list = audit_list;
    this.offset = offset;
    this.total_count = total_count;
    int npage = engine.getNumAuditRecordsPerPage();
    this.last_index = offset + npage;
    if (this.last_index>total_count)
      this.last_index = total_count;
    this.title = title;
    if (this.last_index<total_count)
      this.next_url = StringUtil.replaceAllInstances(template_url,"%",String.valueOf(offset+npage));
    else
      this.next_url = null;
    if (offset>0)
      this.prev_url = StringUtil.replaceAllInstances(template_url,"%",String.valueOf(offset-npage));
    else
      this.prev_url = null;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface VeniceContent
   *--------------------------------------------------------------------------------
   */

  public String getPageTitle(RenderData rdat)
  {
    return title;

  } // end getPageTitle

  public String getPageQID()
  {
    return null;

  } // end getPageQID

  /*--------------------------------------------------------------------------------
   * Implementations from interface ContentRender
   *--------------------------------------------------------------------------------
   */

  public void renderHere(Writer out, RenderData rdat) throws IOException
  {
    rdat.writeContentHeader(out,title,null);
    
    // Write the informational and navigational table
    out.write("<TABLE WIDTH=\"100%\" BORDER=0><TR VALIGN=MIDDLE><TD ALIGN=LEFT CLASS=\"content\">"
	      + rdat.getStdFontTag(CONTENT_FOREGROUND,2) + "\nDisplaying records <B>" + (offset+1)
	      + "</B> to <B>" + last_index + "</B> of <B>" + total_count + "</B>\n"
	      + "</FONT></TD><TD ALIGN=RIGHT>\n");
    if (prev_url==null)
      out.write("<IMG SRC=\"" + rdat.getFullImagePath("bn_transparent.gif")
		+ "\" ALT=\"\" WIDTH=80 HEIGHT=24 BORDER=0>\n");
    else
      out.write("<A HREF=\"" + rdat.getEncodedServletPath(prev_url) + "\"><IMG SRC=\""
		+ rdat.getFullImagePath("bn_ar_previous.gif")
		+ "\" ALT=\"Previous\" WIDTH=80 HEIGHT=24 BORDER=0></A>");
    out.write("&nbsp;");
    if (next_url==null)
      out.write("<IMG SRC=\"" + rdat.getFullImagePath("bn_transparent.gif")
		+ "\" ALT=\"\" WIDTH=80 HEIGHT=24 BORDER=0>\n");
    else
      out.write("<A HREF=\"" + rdat.getEncodedServletPath(next_url) + "\"><IMG SRC=\""
		+ rdat.getFullImagePath("bn_ar_next.gif")
		+ "\" ALT=\"Next\" WIDTH=80 HEIGHT=24 BORDER=0></A>");
    out.write("\n</TD></TR></TABLE>\n");

    // Start writing the table containing the actual audit records.
    String tb_font = rdat.getStdFontTag(CONTENT_FOREGROUND,2);
    out.write("<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=3>\n<TR>\n<TH ALIGN=LEFT CLASS=\"content\" NOWRAP>"
	      + tb_font + "<B>Date/Time</B></FONT></TH>\n<TH ALIGN=LEFT CLASS=\"content\" NOWRAP>"
	      + tb_font + "<B>Description</B></FONT></TH>\n<TH ALIGN=LEFT CLASS=\"content\" NOWRAP>"
	      + tb_font + "<B>User</B></FONT></TH>\n<TH ALIGN=LEFT CLASS=\"content\" NOWRAP>" + tb_font
	      + "<B>Community</B></FONT></TH>\n<TH ALIGN=LEFT CLASS=\"content\" NOWRAP>" + tb_font
	      + "<B>IP Address</B></FONT></TH>\n<TH ALIGN=LEFT CLASS=\"content\" COLSPAN=4 NOWRAP>"
	      + tb_font + "<B>Additional Data</B></FONT></TH>\n</TR>\n");
    Iterator it = audit_list.iterator();
    while (it.hasNext())
    { // display each record in turn
      AuditData dat = (AuditData)(it.next());
      out.write("<TR>\n<TD ALIGN=LEFT CLASS=\"content\" NOWRAP>" + tb_font
		+ rdat.formatDateForDisplay(dat.getDateTime())
		+ "</FONT></TD>\n<TD ALIGN=LEFT CLASS=\"content\" NOWRAP>" + tb_font
		+ StringUtil.encodeHTML(dat.getDescription())
		+ "</FONT></TD>\n<TD ALIGN=LEFT CLASS=\"content\" NOWRAP>" + tb_font
		+ StringUtil.encodeHTML(dat.getUserName())
		+ "</FONT></TD>\n<TD ALIGN=LEFT CLASS=\"content\" NOWRAP>" + tb_font
		+ StringUtil.encodeHTML(dat.getCommunityName())
		+ "</FONT></TD>\n<TD ALIGN=LEFT CLASS=\"content\" NOWRAP>" + tb_font
		+ StringUtil.encodeHTML(dat.getIPAddress()) + "</FONT></TD>\n");
      for (int i=0; i<AuditData.DATA_COUNT; i++)
      { // write the data values
	out.write("<TD ALIGN=LEFT CLASS=\"content\" NOWRAP>" + tb_font);
	if (dat.getData(i)!=null)
	  out.write(StringUtil.encodeHTML(dat.getData(i)));
	else
	  out.write("&nbsp;");
	out.write("</FONT></TD>\n");

      } // end for

      out.write("</TR>\n");

    } // end while

    out.write("</TABLE>\n");

  } // end renderHere

} // end class AuditDataViewer
