package eu.dnetlib.dummy;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * First implementation of DummyDAOInterface. It stores a DummyObject in two postgres db as following: db: lexis table:
 * table1 columns: theInt integer primary key --> DummyObject.getTheInt() db: lexis2 table: table2 columns: id integer
 * primary key --> DummyObject.getTheInt(), theString varchar --> DummyObject.getTheString()
 * 
 * @author lexis
 */
public class DummyDAO implements DummyDAOInterface {

	/**
	 * logger.
	 */
	private static final Log log = LogFactory.getLog(DummyDAO.class);
	/** JDBCTemplate for database lexis. */
	private JdbcTemplate jdbTemplate1;
	/** JDBCTEmplate for database lexis2. */
	private JdbcTemplate jdbTemplate2;

	public JdbcTemplate getJdbTemplate1() {
		return this.jdbTemplate1;
	}

	public void setJdbTemplate1(final JdbcTemplate jdbTemplate1) {
		this.jdbTemplate1 = jdbTemplate1;
	}

	public JdbcTemplate getJdbTemplate2() {
		return this.jdbTemplate2;
	}

	public void setJdbTemplate2(final JdbcTemplate jdbTemplate2) {
		this.jdbTemplate2 = jdbTemplate2;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see eu.dnetlib.dummy.DummyDAOInterface#save(eu.dnetlib.dummy.DummyObject, boolean)
	 */
	public void save(final DummyObject obj, final boolean fail) {
		this.jdbTemplate1.execute("INSERT INTO table1 VALUES (" + obj.getTheInt() + ")");
		if (fail)
			this.jdbTemplate2.execute("INSERT INTO table2(id, thestring) VALUES (" + obj.getTheInt() + ", '" + obj.getTheString() + "')");
		//this will causa a SQLException (wrapped in a spring exception) for a Duplicate key value entry
		this.jdbTemplate2.execute("INSERT INTO table2(id, thestring) VALUES (" + obj.getTheInt() + ", '" + obj.getTheString() + "')");
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see eu.dnetlib.dummy.DummyDAOInterface#saveUsingConnection(eu.dnetlib.dummy.DummyObject, boolean)
	 */
	public void saveUsingConnection(final DummyObject obj, final boolean fail) {
		log.debug(this);
		Connection con1 = null, con2 = null;
		try {
			con1 = this.jdbTemplate1.getDataSource().getConnection();
			log.debug("Connection1 autocommit? " + con1.getAutoCommit());
			Statement st1 = con1.createStatement();
			st1.execute("INSERT INTO table1 VALUES (" + obj.getTheInt() + ")");
			con2 = this.jdbTemplate2.getDataSource().getConnection();
			log.debug("Connection2 autocommit? " + con2.getAutoCommit());
			if (fail) {
				Statement st2 = con2.createStatement();
				st2.execute("INSERT INTO table2(id, thestring) VALUES (" + obj.getTheInt() + ", '" + obj.getTheString() + "')");
			}
			Statement st3 = con2.createStatement();
			st3.execute("INSERT INTO table2(id, thestring) VALUES (" + obj.getTheInt() + ", '" + obj.getTheString() + "')");
		} catch (SQLException e) {
			try {
				if (con1 != null)
					con1.close();
				if (con2 != null)
					con2.close();
			} catch (Exception ex) {
				throw new RuntimeException(ex);
			}
			throw new RuntimeException(e);
			//		} finally {
			//			try {
			//				if (con1 != null)
			//					con1.close();
			//				if (con2 != null)
			//					con2.close();
			//			} catch (Exception e) {
			//				throw new RuntimeException(e);
			//			}
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see eu.dnetlib.dummy.DummyDAOInterface#deleteAllContents()
	 */
	public void deleteAllContents() {
		this.jdbTemplate1.execute("DELETE FROM table1");
		this.jdbTemplate2.execute("DELETE FROM table2");
	}
}
