 /*
 * Copyright (C) 09-sept.-2003  Jean-Yves Beaujean 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,  59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 */
package jxtray.dump;



import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Connection;

import jxtray.util.HTMLUtilities;
import jxtray.util.TimeUtilities;

import jxtray.Jxtray;


import jxtray.io.WriterContext;


public class JxtHTMLDumpStrategy extends JxtDumpStrategy
{
	public JxtHTMLDumpStrategy(String filename,DumperListener listener,Connection connection,String lib, String table,WriterContext writer)
	{
		super(filename,listener,connection,lib,table,writer);
	}
	
	public void run()
	{
		_finished=false;
		try
		{
			double t = System.currentTimeMillis();
			_output.write("<htm><head></head><body>"); 
			dumpStructure();
			dumpData();
			_output.write("</body>");
			double diff = System.currentTimeMillis()-t;
			_listener.dumpLog("\n"+Jxtray.getLang("dump.operation_time")+ TimeUtilities.smartTime(diff/1000) );
			_output.close();
		}
		catch ( Exception e )
		{
			_listener.dumpError(e.getMessage());
		}
		catch ( Error e )
		{
			_listener.dumpError(e.getMessage());
		}		
		_listener.dumpEnded();
		_finished=true;
		_thread=null;
	}
	
	public String getName() { return "HTML Dumper";}
		
	protected void dumpData() 
	{
		 String query = new String();
		 try
		 {
			 _output.write("<table border='1' cellpadding='0' cellspacing='0' width='100%'>\n");

		 	if ( _connection.getMetaData().supportsCatalogsInDataManipulation() || _connection.getMetaData().supportsSchemasInDataManipulation() )
				query ="select * from "+_lib+"."+ _table;
			else
				query ="select * from "+_table;		

		 	_listener.dumpLog("\n"+Jxtray.getLang("dump.execute_query")+query);
			_listener.dumpState(Jxtray.getLang("dump.select_datas"));
			Statement stmt =   _connection.createStatement();
		 	ResultSet res =stmt.executeQuery(query);
			ResultSetMetaData rmtd =  res.getMetaData();
			StringBuffer record = null;
			
			while ( res.next())
			{
					_cpt++; 
					_listener.dumpNextRecord(_cpt);
					record = new StringBuffer();	
					record.append("\t<tr>\n");
					if ( _cpt==1)
					{
						// Generate table header
						for ( int i = 0 ; i < rmtd.getColumnCount();i++)
						{
							record.append("\t\t<td><b>"+rmtd.getColumnName(i+1)+"</b></td>\n");
						}
						record.append("\t</tr>\n\t<tr>\n");
					}
					
					for ( int i = 0 ; i < rmtd.getColumnCount();i++)
					{
							record.append("\t\t<td>"+HTMLUtilities.nl2br(res.getString(i+1))+"</td>\n");
					}
					record.append("\t</tr>\n");
					_output.write(record.toString());
			}
			_output.write("\n</table></body></html>");
			res.close();
			stmt.close();
		}
		catch ( SQLException e )
		{
			_listener.dumpError(e.getMessage());
		}
		catch ( Exception e )
		{
			_listener.dumpError(e.getMessage());
		}
		
	}
	
	protected void dumpStructure()
	{
		
		_listener.dumpState(Jxtray.getLang("dump.structure"));
		try
		{
			_output.write("<table border='1' cellpadding='0' cellspacing='0' width='100%'>\n");
			java.sql.ResultSet res = _connection.getMetaData().getColumns(_lib,null,_table,null);
			StringBuffer field = null;
			while ( res.next() )
			{
				field = new StringBuffer();
				field.append("<tr>\n<td>");
				field.append(res.getString(4)).append("</td>");
				field.append("<td>").append(res.getString(6)).append("</td><td>").append(res.getString(7)).append("</td>");
				field.append("</tr>\n");
			}
			_output.write(field.toString());
			_output.write("</table>");
			res.close();
								
		}
		catch ( SQLException e )
		{
			_listener.dumpError(e.getMessage());
		}
		catch ( Exception e )
		{
			_listener.dumpError(e.getMessage());
		}
		
		
	}	
}
