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 is produced from page data stored in and retrieved
 * from a DBMS table.
 * 
 * @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 DataSetToPage 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");

      conn = defaults.login();

      TableDataSet ds = new TableDataSet(conn, "formdata");
      ds.order("fd_order")
        .fetchRecords();

      LiteralElement tbl = new LiteralElement(ds);
      FormElement fe     = getForm(ds);
      ServletPage hp     = new ServletPage("DataSet to Page");

      ds.close();

      hp.getBodyElement()
	.setAttribute(BodyElement.bgColor, HtmlColor.white);
      hp.getBody()
        .addElement(MarkupElement.HorizontalRule)
        .addElement(new HeadingElement("This Metadata:", 2).asCenteredElement())
        .addElement(tbl)
        .addElement(MarkupElement.HorizontalRule)
        .addElement(new HeadingElement("produces this HTML form:", 2).asCenteredElement())
        .addElement(fe)
        .addElement(MarkupElement.HorizontalRule)
	.addElement("Copyright 1996-2000 by BEA Systems, Inc. All Rights Reserved.");

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

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

  public static FormElement  getForm(TableDataSet ds) throws Exception {
    FormElement fe = new FormElement(defaults.prefix() + "DataSetToPage", "POST");

    for (int i = 0; i < ds.size(); i++) {
      Record rec  = ds.getRecord(i);
      String type = rec.getValue("fd_object").asString();

      if (type.equals("BoldElement")) {
        fe.addElement(new BoldElement(rec.getValue("fd_value").asString()));
        continue;
      }

      if (type.equals("ItalicElement")) {
        fe.addElement(new ItalicElement(rec.getValue("fd_value").asString()));
        continue;
      }

      if (type.equals("MarkupElement")) {
        String typestr = rec.getValue("fd_type").asString();
        MarkupElement me = MarkupElement.BeginParagraph;
        if (typestr.equals("BeginParagraph")) me = MarkupElement.BeginParagraph;
        if (typestr.equals("Break"))          me = MarkupElement.Break;
        fe.addElement(me);
        continue;
      }
      
      if (type.equals("InputElement")) {
        String typestr = rec.getValue("fd_type").asString();
        FieldType ft = FieldType.text;
        if (typestr.equals("text"))     ft = FieldType.text;
        if (typestr.equals("password")) ft = FieldType.password;
        if (typestr.equals("submit"))   ft = FieldType.submit;

        fe.addElement(new InputElement(rec.getValue("fd_name").asString(), ft)
                        .setValue(rec.getValue("fd_value").asString())
                        .setSize(rec.getValue("fd_length").asInt())
                        .setMaxlen(rec.getValue("fd_maxlength").asInt()));
        continue;
      }
    }
    return fe;
  }
}



