package eu.dnetlib.dlms.jdbc;

import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Driver implementation.
 * 
 * @author lexis
 */
public class DorotyDriver implements Driver {

	/** Logger. */
	private static final Log log = LogFactory.getLog(DorotyDriver.class);

	static {
		try {
			DriverManager.registerDriver(new DorotyDriver());
		} catch (SQLException e) {
			log.debug("Exception registering driver in static initializer (DorotyDriver class) " + e);
		}
	}

	/**
	 * Protocol that this driver understands. Calling acceptsURL(String) will return true if the given string is
	 * equal(ignore case) to this property.
	 */
	private String knownProtocol;

	/**
	 * driver's major version number.
	 */
	private int majorVersion;
	/**
	 * driver's minor version number.
	 */
	private int minorVersion;

	/**
	 * Constructor.
	 */
	public DorotyDriver() {
		this.majorVersion = 1;
		this.minorVersion = 0;
	}

	public String getKnownProtocol() {
		return this.knownProtocol;
	}

	public void setKnownProtocol(final String knownProtocol) {
		this.knownProtocol = knownProtocol;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.Driver#acceptsURL(java.lang.String)
	 */
	public boolean acceptsURL(final String url) throws SQLException {
		try {
			URL theUrl = new URL(url);
			return theUrl.getProtocol().equalsIgnoreCase(this.knownProtocol);
		} catch (MalformedURLException e) {
			return false;
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.Driver#connect(java.lang.String, java.util.Properties)
	 */
	public Connection connect(final String url, final Properties info) throws SQLException {
		/*
		 * Attempts to make a database connection to the given URL. The driver should return "null" if it realizes it is
		 * the wrong kind of driver to connect to the given URL. This will be common, as when the JDBC driver manager is
		 * asked to connect to a given URL it passes the URL to each loaded driver in turn. The driver should throw an
		 * SQLException if it is the right driver to connect to the given URL but has trouble connecting to the
		 * database. The java.util.Properties argument can be used to pass arbitrary string tag/value pairs as
		 * connection arguments. Normally at least "user" and "password" properties should be included in the Properties
		 * object.
		 */
		// TODO implement me
		return null;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.Driver#getMajorVersion()
	 */
	public int getMajorVersion() {
		return this.majorVersion;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.Driver#getMinorVersion()
	 */
	public int getMinorVersion() {
		return this.minorVersion;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.Driver#getPropertyInfo(java.lang.String, java.util.Properties)
	 */
	public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) throws SQLException {
		/*
		 * TODO maybe provide some info inside the returned array. For now just return an empty one (that means no
		 * propertiy is required.
		 */
		return new DriverPropertyInfo[0];
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.sql.Driver#jdbcCompliant()
	 */
	public boolean jdbcCompliant() {
		return false;
	}

}
