package examples.htmlkona;

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

/**
 * Illustrates how to retrieve an audio clip from a DBMS using
 * a WebLogic jDriver JDBC driver and dbKona.
 * 
 * @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 AudioDataDisplay extends HttpServlet {

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

    try {
      res.setStatus(HttpServletResponse.SC_OK);
      String name  = req.getParameter("audioname");
      conn = defaults.login();

      if (name != null) {
        TableDataSet td = new TableDataSet(conn, "audiotable");    
        td.selectStmt().setQbe("name", name);
        td.fetchRecords(); 

        Record rec = td.getRecord(0);
     
	res.setContentType("audio/" + rec.getValue("type").asString());

        // Retrieve the audioclips from the database
        AudioPage hp = new AudioPage(rec.getValue("data").asBytes());

        hp.output(res.getOutputStream());
      }
      else {
        // Invoking with no arguments will populate the database
        //  This is the code that deletes and loads the audioclips from the database
/*
        // Clear the existing audioclips
        conn.executeSql("delete from audiotable");

        // Add some images to the database
        TableDataSet td = new TableDataSet(conn, "audiotable");
        Record rec = td.addRecord();
        rec.setValue("name", "train")    
           .setValue("type", "basic")    
           .setValue("data", new Blob("F:/hotjava/demo/audio/train.au"));   

        rec = td.addRecord();
        rec.setValue("name", "whoopy")    
           .setValue("type", "basic")    
           .setValue("data", new Blob("F:/hotjava/demo/audio/whoopy.au"));   
   
        rec = td.addRecord();
        rec.setValue("name", "scream")    
           .setValue("type", "basic")    
           .setValue("data", new Blob("F:/hotjava/demo/audio/scream.au"));   
   
        td.save();
        td.close();
*/  

        TableElement tab = new TableElement();
	tab.addElement(new TableRowElement()
		       .addElement(new AnchorElement(defaults.prefix() +
						     "AudioDataDisplay?audioname=train",
						     "Train")))
	   .addElement(new TableRowElement()
		       .addElement(new AnchorElement(defaults.prefix() +
						     "AudioDataDisplay?audioname=whoopy",
						     "Whoopy")))
	   .addElement(new TableRowElement()
		       .addElement(new AnchorElement(defaults.prefix() +
						     "AudioDataDisplay?audioname=scream",
						     "Scream")));
        ServletPage hp = new ServletPage("AudioDataDisplay");
	hp.getBodyElement()
	  .setAttribute(BodyElement.bgColor, HtmlColor.white);
        hp.getBody()
          .addElement(new HeadingElement("Click on a sound title " + 
                                         "and retrieve the audio clip from the database", 2))
	  .addElement(MarkupElement.HorizontalRule)
          .addElement(new CenteredElement(tab));
	
	res.setContentType("text/html");

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

