/*
 * Copyright (C) 15-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.swing;



import java.awt.Cursor;

import java.awt.event.*; 
 
import jxtray.dump.*;
import jxtray.Messages;
import jxtray.Jxtray;

import jxtray.CustomFileFilter;
import jxtray.util.FileUtilities;
import jxtray.io.*;
import java.io.File;

import jxtray.connection.JxtConnection;


public class JxtDumpPanelListener implements ActionListener
{
	
	private JxtDumpPanel parent;
	private JxtConnection con ;
	private JxtDumpContext dumpContext;
	private String format;
	private String lib="",table=""; 
	private boolean dumpall = false;

	
	public JxtDumpPanelListener(JxtDumpPanel parent)
	{
			this.parent = parent;
			con = (JxtConnection) Jxtray.getConnectionManager().get(parent.getConnectionName());
	} 
		
	public void actionPerformed ( ActionEvent e )
	{
		if ( "dump".equals(e.getActionCommand()))
		{
			try
			{
				
				if ( con.getConnection().getMetaData().supportsSchemasInDataManipulation() )
					lib=parent.getSchemaName();
				else
					lib=parent.getCatalogName();
			}
			catch ( Exception ex )
			{
				ex.printStackTrace();
				Messages.error("JxtDumpPanelListener : erreur SQL : "+ ex );
			}
			format = (String) parent.getDumpFormat();
			table=parent.getTableName();
			if ( "*".equals(table))
			{
				dumpall=true;
				System.out.println("full dump");
				int num = parent.getComboTable().getItemCount();
				try
				{
					File dbDir = new File(Jxtray.HOME+lib);
					if ( !dbDir.exists())
						dbDir.mkdir();
				}
				catch ( Exception ex )
				{
					System.err.println(ex.getMessage());
				}
				for ( int i=1; i < num; i++)
				{
					System.out.println("i ="+i);
					//parent.getComboTable().setSelectedIndex(i);
					table=(String)parent.getComboTable().getItemAt(i);
					dump();
				}
			}
			else
			{
				dumpall = false;
				dump();
			}
		}
				
	}
		
	private void dump()
	{
		String name = null;
		if ( !dumpall )
		{
			CustomFileFilter fileFilter = null;
			CustomFileFilter zipfileFilter[] = {new CustomFileFilter(Jxtray.getLang("file.jar"),FileUtilities.JAR),new CustomFileFilter(Jxtray.getLang("file.zip"),FileUtilities.ZIP),new CustomFileFilter(Jxtray.getLang("file.gzip"),FileUtilities.GZIP)};
			if ( !parent.isCompressChecked() )
			{
				if ( "CSV".equals(format) )
				{
					fileFilter = new CustomFileFilter(Jxtray.getLang("file.csv"),FileUtilities.CSV);
				}
				else if ( "SQL".equals(format))
				{
					fileFilter = new CustomFileFilter(Jxtray.getLang("file.sql"),FileUtilities.SQL);
				}
				else if ( "HTML".equals(format))
				{	
					fileFilter = new CustomFileFilter(Jxtray.getLang("file.html"),FileUtilities.HTML);
				}
				else if ( "XML".equals(format))
				{
					fileFilter = new CustomFileFilter(Jxtray.getLang("file.xml"),FileUtilities.XML);
				}
			}				
			
			if ( parent.isCompressChecked() )
				name = Messages.chooseFile(zipfileFilter);
			else
				name = Messages.chooseFile(fileFilter);
		}
		else
			name = Jxtray.HOME+lib+File.separator+table.toLowerCase()+"."+format.toLowerCase();
			
		System.out.println(name);
			
		if ( name != null )
		{
			WriterContext writer = new WriterContext(name);
			if ( parent.isCompressChecked() )
			{
				String ext = name.substring(name.lastIndexOf(".")+1,name.length()).toLowerCase();
				name = name.substring(0,name.lastIndexOf("."))+"."+format.toLowerCase();
				
				if ( FileUtilities.GZIP.equals(ext) )
				{
					writer.setGunzipWriterStrategy(name);
				}
				else if ( FileUtilities.JAR.equals(ext) )
				{
					writer.setJarWriterStrategy(name);
				}
				else if ( FileUtilities.ZIP.equals(ext) )
				{
					writer.setZipWriterStrategy(name);
				}
			}

			dumpContext = new JxtDumpContext(name,parent,con.getConnection(),lib,table);
			
			if ( "CSV".equals(format) )
			{
				dumpContext.setCSVDumpStrategy(name,parent,con.getConnection(),lib,table, writer);
			}
			else if ( "SQL".equals(format))
			{
				dumpContext.setSQLDumpStrategy(name,parent,con.getConnection(),lib,table, writer);
			}
			else if ( "HTML".equals(format))
			{	
				dumpContext.setHTMLDumpStrategy(name,parent,con.getConnection(),lib,table, writer);
			}
			else if ( "XML".equals(format))
			{
				dumpContext.setXMLDumpStrategy(name,parent,con.getConnection(),lib,table, writer);
			}
			
			Jxtray.getMainFrame().setCursor(new Cursor(Cursor.WAIT_CURSOR));
			try
			{
				writer.open();
				dumpContext.dump();
				// 22 nov test
				while ( !dumpContext.isFinished() ) {}
				writer.close();
				// ------
			}
			catch ( Exception ex )
			{
				ex.printStackTrace();
				Messages.error("JxtDumpPanelListener : erreur SQL : "+ ex );
			}	
				
		} // end if name != null 
	}
		
			

}
