/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.hibernate.console;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.eclipse.datatools.connectivity.ConnectionProfileConstants;
import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.ProfileManager;
import org.eclipse.datatools.connectivity.drivers.DriverInstance;
import org.eclipse.datatools.connectivity.drivers.jdbc.IJDBCDriverDefinitionConstants;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.resolver.DialectFactory;

/**
 * @author Vitali Yemialyanchyk
 */
public class ConnectionProfileUtil {

	public static String getDriverDefinitionId(IConnectionProfile profile) {
		if (profile == null) {
			return null;
		}
		return profile.getBaseProperties().getProperty(
				ConnectionProfileConstants.PROP_DRIVER_DEFINITION_ID);
	}

	public static DriverInstance getDriverDefinition(String connectionProfile) {
		if (connectionProfile == null) {
			return null;
		}
		IConnectionProfile profile = ProfileManager.getInstance().getProfileByName(
				connectionProfile);
		if (profile == null) {
			return null;
		}
		String driverID = getDriverDefinitionId(profile);
		return org.eclipse.datatools.connectivity.drivers.DriverManager.getInstance()
				.getDriverInstanceByID(driverID);
	}

	/*
	 * try get a path to the sql driver jar file from DTP connection profile
	 */
	public static String[] getConnectionProfileDriverURL(String connectionProfile) {
		DriverInstance di = getDriverDefinition(connectionProfile);
		if (di == null) {
			return null;
		}
		return di.getJarListAsArray();
	}

	public static String getDriverClass(String connectionProfile) {
		DriverInstance di = getDriverDefinition(connectionProfile);
		String driverClass = di != null ? 
			di.getProperty(IJDBCDriverDefinitionConstants.DRIVER_CLASS_PROP_ID) : ""; //$NON-NLS-1$
		return driverClass;
	}
	
	/**
	 * This method extracts connection properties from connection profile and convert them to
	 * hiberante properties (uses other "keys" for them)
	 * @param profile
	 * @return
	 */
	public static Properties getHibernateConnectionProperties(IConnectionProfile profile){
		Properties props = new Properties();
		if (profile != null) {
			final Properties cpProperties = profile.getProperties(profile.getProviderId());
			String driverClass = ConnectionProfileUtil.getDriverClass(profile.getName());
			props.setProperty(Environment.DRIVER, driverClass);
			String url = cpProperties.getProperty(IJDBCDriverDefinitionConstants.URL_PROP_ID);
			props.setProperty(Environment.URL, url);
			String user = cpProperties.getProperty(IJDBCDriverDefinitionConstants.USERNAME_PROP_ID);
			if (null != user && user.length() > 0) {
				props.setProperty(Environment.USER, user);
			}
			String pass = cpProperties.getProperty(IJDBCDriverDefinitionConstants.PASSWORD_PROP_ID);
			if (null != pass && pass.length() > 0) {
				props.setProperty(Environment.PASS, pass);
			}
		}
		return props;
	}
	
	public static String autoDetectDialect(Properties properties) {
		if (properties.getProperty(Environment.DIALECT) == null) {
			String url = properties.getProperty(Environment.URL);
			String user = properties.getProperty(Environment.USER);
			String pass = properties.getProperty(Environment.PASS);
			Connection connection = null;
			try {
				connection = DriverManager.getConnection(url, user, pass);
				// SQL Dialect:
				//note this code potentially could throw class cast exception
				//see https://issues.jboss.org/browse/JBIDE-8192
				//probably when not Hiberante3.5 is used
				Dialect dialect = DialectFactory.buildDialect(properties, connection);
				return dialect.toString();
			} catch (SQLException e) {
				// can't determine dialect
			} finally {
				if (connection != null) {
					try {
						connection.close();
					} catch (SQLException e) {
						// ignore
					}
				}
			}
			return null;
		} else {
			return properties.getProperty(Environment.DIALECT);
		}
	}
}
