package examples.htmlkona;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import weblogic.db.jdbc.*;
import java.sql.*;
import weblogic.html.*;

/**
 * This servlet merges an entire table's data into a prepared
 * HTML document.
 * 
 * @author Copyright (c) 1996-98 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class TableDataMerge extends HttpServlet {

  public synchronized void service(HttpServletRequest req, HttpServletResponse res) 
    throws IOException
  {
    Connection conn = null;

    try {
      res.setStatus(HttpServletResponse.SC_OK);
      res.setContentType("text/html");

      String externalfile = defaults.fileloc() + "extdoc.html";

      conn = defaults.login();

      QueryDataSet ds = new QueryDataSet(conn, "select * from emp");
      ds.fetchRecords();

      StringElement elem = new StringElement();

      // The two strings $table1 and $table2 are replaced by the two tables
      elem.setContentsWithFile(externalfile)
          .replace("$table1", new LiteralElement(ds).toString())
          .replace("$table2", new TableElement(ds).toString());

      ServletPage hp = new ServletPage("TableDataMerge");
      hp.getBodyElement()
	.setAttribute(BodyElement.bgColor, HtmlColor.white);
      hp.getBody()
        .addElement(elem)
	.addElement(MarkupElement.HorizontalRule)
	.addElement("Copyright 1996-2000 by BEA Systems, Inc. All Rights Reserved.");

      hp.output(res.getOutputStream());
      ds.close();

    }
    catch (Exception e) {
      defaults.showException(e, res.getOutputStream());
    }

    // Always close the connection in a finally block.
    finally {
      try {conn.close();} catch (Exception e2) {;}
    }
  }
}
