/*
 * 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-02 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.view;

import java.io.IOException;
import java.util.*;
import javax.servlet.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.HTMLRendering;
import com.silverwrist.venice.ui.servlet.RequestImpl;

public class AuditView implements ContentDirect
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String REPLACE_TOKEN = "${offset}";

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private int menu_sel = MENU_SELECTOR_NOCHANGE;
  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;
  private String return_url;
  private String return_text;

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

  public AuditView(VeniceEngine engine, List audit_list, int offset, int total_count, String title,
		   String template_url, String return_url, String return_text)
  {
    this.audit_list = audit_list;
    this.offset = offset;
    this.total_count = total_count;
    int npage = engine.getNumAuditRecordsPerPage();
    this.last_index = Math.min(offset + npage,total_count);
    this.title = title;
    if (this.last_index<total_count)
      this.next_url = StringUtil.replaceAllInstances(template_url,REPLACE_TOKEN,String.valueOf(offset+npage));
    else
      this.next_url = null;
    if (offset>0)
      this.prev_url = StringUtil.replaceAllInstances(template_url,REPLACE_TOKEN,
                                                     String.valueOf(Math.max(offset-npage,0)));
    else
      this.prev_url = null;
    this.return_url = return_url;
    this.return_text = return_text;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface Content
   *--------------------------------------------------------------------------------
   */

  public boolean needFrame()
  {
    return true;

  } // end needFrame

  public int getMenuSelector()
  {
    return menu_sel;

  } // end getMenuSelector

  public String getPageTitle(RequestOutput ro)
  {
    return title;

  } // end getPageTitle

  public String getPageQID()
  {
    return null;

  } // end getPageQID

  /*--------------------------------------------------------------------------------
   * Implementations from interface ContentDirect
   *--------------------------------------------------------------------------------
   */

  public void render(RequestOutput out) throws IOException
  {
    HTMLRendering html = (HTMLRendering)(out.queryService(HTMLRendering.class));
    out.writeContentHeader(title,null);

    // Write the return URL
    out.write("<DIV CLASS=\"content\">" + html.getFontTag(html.CONTENT_FOREGROUND,"content") + "<A HREF=\""
              + html.formatURL(return_url,html.SERVLET) + "\">" + StringUtil.encodeHTML(return_text)
              + "</A></FONT></DIV>\n");

    // Write the informational and navigational table
    out.write("<TABLE WIDTH=\"100%\" BORDER=0><TR VALIGN=MIDDLE><TD ALIGN=LEFT CLASS=\"content\">"
              + html.getFontTag(html.CONTENT_FOREGROUND,"content") + "\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(html.getButtonVisual("_null_"));
    else
      out.write("<A HREF=\"" + html.formatURL(prev_url,html.SERVLET) + "\">" + html.getButtonVisual("previous")
		+ "</A>");
    out.write("&nbsp;");
    if (next_url==null)
      out.write(html.getButtonVisual("_null_"));
    else
      out.write("<A HREF=\"" + html.formatURL(next_url,html.SERVLET) + "\">" + html.getButtonVisual("next")
                + "</A>");
    out.write("\n</TD></TR></TABLE>\n");

    // Start writing the table containing the actual audit records.
    String tb_font = html.getFontTag(html.CONTENT_FOREGROUND,"content");
    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 + html.formatDate(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 render

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public final void setMenuSelector(int sel)
  {
    menu_sel = sel;

  } // end setMenuSelector

} // end class AuditView
