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

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

import org.jdom.output.XMLOutputter;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;

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


import jxtray.Messages;
import jxtray.Jxtray;


public class JxtConnectionManager extends Properties
{
	private Enumeration connectionName;
	private static boolean isInstanceAlive = false;

	public static JxtConnectionManager getInstance()
	{
		if ( isInstanceAlive )
			return null;
		else
		{	
			isInstanceAlive = true;
			return new JxtConnectionManager();
		}
	}
	
	
	
	public void closeAllConnections()
	{

		JxtConnection curCon = null;
		connectionName = keys();
		while ( connectionName.hasMoreElements())
		{	
			try
			{
				curCon = (JxtConnection) get(connectionName.nextElement());
				if ( curCon.isConnected() )
				{
					curCon.close();
					System.out.println("JxtConnectionManager.closeAllConnections() : "+ curCon.getName()+ " : CLOSED");
				}
			}
			catch ( Exception e )
			{
				System.err.println("JxtConnectionManager.closeAllConnection() : "+ e );
			}
		}	
	}
	

	public Object[] getAllConnectionName()
	{
		Vector vName = new Vector();
		connectionName = keys();
		while ( connectionName.hasMoreElements())
			vName.add(connectionName.nextElement());
		
		Object elmt[] = vName.toArray();
		Arrays.sort(elmt);
		return elmt;
	}	
	
	/** Renvoi le nombre de connexion présente dans le manager ( fermée ou non ) */
	public int count()
	{
			return this.size();
	}
	

	/** Enregistre les connexions dans le fichier XML */
	public void writeXMLFile()
	{
			Element root = new Element("connections");
			Document doconnection = new Document(root);
			
			connectionName = keys();
			while ( connectionName.hasMoreElements())
			{	
				JxtConnection curCon = (JxtConnection) get(connectionName.nextElement());
				Element element = new Element("connection");
				element.setAttribute(new Attribute("name",curCon.getJxtConnectionParam().getName()));
				element.setAttribute(new Attribute("host",curCon.getJxtConnectionParam().getHost()));
				element.setAttribute(new Attribute("passwordprompt",""+ curCon.getJxtConnectionParam().isPasswordPrompt()));

				Element subelement = new Element("driver");
				subelement.setAttribute(new Attribute("name",curCon.getDriverName()));
				subelement.setAttribute(new Attribute("jdbcurl",curCon.getJxtConnectionParam().getJdbcurl()));
				
				element.addContent(subelement);
				
				subelement = new Element("user");
				subelement.setAttribute(new Attribute("login",curCon.getJxtConnectionParam().getLogin()));
				subelement.setAttribute(new Attribute("password",curCon.getJxtConnectionParam().getPassword()));
				element.addContent(subelement);
				
				root.addContent(element);
			}
				
			XMLOutputter outputter = new XMLOutputter();
			try
			{
				outputter.output(doconnection, new FileOutputStream(Jxtray.HOME+"conf"+File.separator+"connections.xml"));
			}
			catch( Exception e )
			{
				Messages.error("JxtConnexionManager.writeXMLFile() :" + e);
				
			}
	}
	
	public void printState() 
	{
		
		System.out.println("JxtConnectionManager : "+ size() + " in cache");
		connectionName = keys();
		while ( connectionName.hasMoreElements())
		{	
			JxtConnection curCon = (JxtConnection) get(connectionName.nextElement());
			System.out.println("--> new connection");
			System.out.println("\tname : "+curCon.getJxtConnectionParam().getName());
			System.out.println("\tdriver classe name :"+curCon.getDriverName());			
			System.out.println("\thost : "+curCon.getJxtConnectionParam().getHost());
			System.out.println("\tjdbcurl : "+curCon.getJxtConnectionParam().getJdbcurl());			
			System.out.println("\tlogin : "+curCon.getJxtConnectionParam().getLogin());
			System.out.println("\tpassword :"+curCon.getJxtConnectionParam().getPassword());
			System.out.println(" -- °° --");
			System.out.println(" ");
		}
	}
	
	public void updateDriverName( String oldName, String newName)
	{
		System.out.println("updating drivername...");
		connectionName = keys();
		JxtConnection con = null;
		while ( connectionName.hasMoreElements())
		{
			con = (JxtConnection) get(connectionName.nextElement());
			System.out.println(con.getName()+" : compare : ["+ con.getDriverName()+"] with ["+oldName+"]");
			if ( con.getDriverName().equals(oldName))
			{
				System.out.println("--> CHANGE IT !");
				con.setDriverName(newName);
				//put(con.getName(),con);
			}
		}		
	
	}

}
