package examples.htmlkona;

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

/**
 * This servlet uses a dbKona KeyDef to update and delete records
 * in a database. The page display is generated by htmlKona.
 * 
 * @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 DisplayByKeyDef 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");

      defaults.setPageDefaults();
      conn = defaults.login();

      // Define the key to used by UPDATE and DELETE statements
      KeyDef key = new KeyDef().addAttrib("empno");

      // Instantiate the TableDataSet with a table name and the key
      TableDataSet ts  = new TableDataSet(conn, "emp", key);

      // Perform an INSERT
      Record rec = ts.addRecord();

      rec.setValue("empno",  8000)
         .setValue("ename",  "MURPHY")
         .setValue("job",    "SALESMAN")
         .setValue("deptno", 10);
      String insert = rec.getSaveString();
      ts.save();

      // Verify results
      TableDataSet ds1 = new TableDataSet(conn, "emp");
      ds1.where("empno = 8000");
      ds1.fetchRecords();

      // Perform an UPDATE
      rec.setValue("ename",  "SMITH")
         .setValue("job",    "MANAGER");
      String update = rec.getSaveString();
      ts.save();

      // Verify results
      TableDataSet ds2 = new TableDataSet(conn, "emp");
      ds2.where("empno = 8000");
      ds2.fetchRecords();

      // Perform a DELETE
      rec.markToBeDeleted();
      String delete = rec.getSaveString();
      ts.save();

      // Verify results
      TableDataSet ds3 = new TableDataSet(conn, "emp");
      ds3.where("empno = 8000");
      ds3.fetchRecords();

      ServletPage hp = new ServletPage("Display By KeyDef");
      hp.getBodyElement()
	.setAttribute(BodyElement.bgColor, HtmlColor.white);
      hp.getBody()
        .addElement(MarkupElement.HorizontalLine)
        .addElement(new HeadingElement("Query results after INSERT", 2))
        .addElement(new HeadingElement("Using SQL:", 3))
        .addElement(new LiteralElement(insert))
        .addElement(new LiteralElement(ds1))
        .addElement(MarkupElement.HorizontalLine)
        .addElement(new HeadingElement("Query results after UPDATE", 2))
        .addElement(new HeadingElement("Using SQL:", 3))
        .addElement(new LiteralElement(update))
        .addElement(new LiteralElement(ds2))
        .addElement(MarkupElement.HorizontalLine)
        .addElement(new HeadingElement("Query results after DELETE", 2))
        .addElement(new HeadingElement("Using SQL:", 3))
        .addElement(new LiteralElement(delete))
        .addElement(new LiteralElement(ds3))
        .addElement(MarkupElement.HorizontalLine)
	.addElement("Copyright 1996-2000 by BEA Systems, Inc. All Rights Reserved");
  
      hp.output(res.getOutputStream());

      // Close each DataSet.
      ts.close();    
      ds1.close();    
      ds2.close();    
      ds3.close();    

    }
    catch (Exception e) {
      defaults.showException(e, res.getOutputStream());
    }
    
    // Always close the connection in a finally block.
    finally {
      try {conn.close();} catch (Exception e2) {;}
    }
  }
}

