/*
 *
 * Copyright (C) 16-mar-2004 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
 * Tem0ple Place - Suite 330, Boston, MA 02111-1307, USA.

 */
package jxtray;


import org.xml.sax.ContentHandler;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;

import org.jdom.output.*;
import org.jdom.*;

import org.xml.sax.helpers.XMLReaderFactory;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import java.io.File;
import java.io.FileOutputStream;



public class SQLHistoryCache extends Hashtable 
{

	private static boolean isInstanceAlive = false;
	
	public static SQLHistoryCache getInstance()
	{
		if ( isInstanceAlive )
			return null;
		else
		{
			isInstanceAlive = true;
			return new SQLHistoryCache();
		}
	}

	public void update( SQLHistoryEntry entry) throws Exception
	{
		this.put(entry.getQuery(),entry);
	}
	
	public Object[] getAllQueries() throws Exception
	{
		
		Vector queries = new Vector();
		Enumeration enumEl = elements();
		SQLHistoryEntry info = null;
		
		while ( enumEl.hasMoreElements())
			queries.add(((SQLHistoryEntry) enumEl.nextElement()).getQuery()) ;
		
		Object elmt[] = queries.toArray();
		Arrays.sort(elmt);
		return elmt;
		
	}

	/** Load from the XML file */
	public void load() throws Exception
	{

		XMLReader reader = XMLReaderFactory.createXMLReader(JxtrayConstants.XMLPARSER);
		ContentHandler sqlhistHandler = new SQLHistoryContentHandler(this);
		reader.setContentHandler(sqlhistHandler);
		InputSource inputSource = new 	InputSource("file:///"+Jxtray.HOME+"conf"+File.separator+"sqlhistory.xml");
		reader.parse(inputSource);
	}

	/** Print the contain of the Hastable */
	public void printState() throws Exception
	{
			Enumeration enumEl = elements();
			SQLHistoryEntry info = null;
			while ( enumEl.hasMoreElements())
			{
				info = (SQLHistoryEntry) enumEl.nextElement() ;
				System.out.println("");
				System.out.println("--> query info");
				System.out.println("query : \t" +info.getQuery());
				System.out.println("lastexecution  state : \t" +info.getLastState() );
				System.out.println("lastexecution date : \t" +info.getDate() );
			}
			System.out.println("+--+ NO MORE QUERIES IN CACHE +--+");
	}
	
	/** Enregistre les connexions dans le fichier XML */
		public void writeXMLFile()
		{
				System.out.println("write");
				Element root = new Element("sqlhistory");
				Document doconnection = new Document(root);
				
				Enumeration enumEl = elements();
				SQLHistoryEntry info = null;
				while ( enumEl.hasMoreElements())
				{
						 
					System.out.println("boucle");
					info = (SQLHistoryEntry) enumEl.nextElement() ;
					Element element = new Element("query");
					element.setAttribute(new Attribute("value",info.getQuery()));
					element.setAttribute(new Attribute("lastexecutiondate",info.getDate()));
					if ( info.getLastState())
						element.setAttribute(new Attribute("lastexecutionstate","true"));
					else
						element.setAttribute(new Attribute("lastexecutionstate","false"));
				
					root.addContent(element);
				}
				

				XMLOutputter outputter = new XMLOutputter();
				try
				{
					outputter.output(doconnection, new FileOutputStream(Jxtray.HOME+"conf"+File.separator+"sqlhistory.xml"));
				}
				catch( Exception e )
				{
					Messages.error("SQLHistoryCache.writeXMLFile() :" + e);
				}
		}
		
		/** Memory Managnement */
		public void finalize() 
		{
			this.clear();
		}		
		

	
	
	

}
