package com.excilys.ebi.spring.dbunit;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.jdbc.datasource.init.DatabasePopulator;

import com.excilys.ebi.spring.dbunit.config.DataSetConfiguration;
import com.excilys.ebi.spring.dbunit.config.DatabaseConnectionConfigurer;
import com.excilys.ebi.spring.dbunit.config.Phase;

public class DbUnitDatabasePopulator implements DatabasePopulator {

	private DataSetConfiguration dataSetConfiguration;

	private Phase phase;

	public void populate(Connection connection) throws SQLException {
		DatabaseOperation operation = phase.getOperation(dataSetConfiguration);
		try {
			IDataSet dataSet = dataSetConfiguration.getDataSet();
			DatabaseConnection databaseConnection = getDatabaseConnection(connection, dataSetConfiguration);
			operation.execute(databaseConnection, dataSet);

		} catch (DatabaseUnitException e) {
			throw new DbUnitException(e);
		} catch (IOException e) {
			throw new DbUnitException(e);
		}
	}

	private DatabaseConnection getDatabaseConnection(Connection connection, DatabaseConnectionConfigurer databaseConnectionConfigurer) throws DatabaseUnitException {

		DatabaseConnection databaseConnection = new DatabaseConnection(connection);
		DatabaseConfig databaseConfig = databaseConnection.getConfig();
		databaseConnectionConfigurer.configure(databaseConfig);

		return databaseConnection;
	}

	public DataSetConfiguration getDataSetConfiguration() {
		return dataSetConfiguration;
	}

	public Phase getPhase() {
		return phase;
	}

	@Required
	public void setDataSetConfiguration(DataSetConfiguration dataSetConfiguration) {
		this.dataSetConfiguration = dataSetConfiguration;
	}

	@Required
	public void setPhase(Phase phase) {
		this.phase = phase;
	}

}

@@ -57,24 +57,62 @@
 OException;%0A
+import java.sql.BatchUpdateException;%0A
 import java.
@@ -123,24 +123,24 @@
 Connection;%0A
-
 import java.
@@ -368,24 +368,81 @@
 eOperation;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.s
@@ -557,16 +557,59 @@
 pulator;
+%0Aimport org.springframework.util.StopWatch;
 %0A%0Aimport
@@ -863,16 +863,111 @@
 ator %7B%0A%0A
+%09private static final Logger LOGGER = LoggerFactory.getLogger(DbUnitDatabasePopulator.class);%0A%0A
 %09private
@@ -1101,16 +1101,108 @@
 ption %7B%0A
+%0A%09%09LOGGER.debug(%22populating%22);%0A%0A%09%09StopWatch sw = new StopWatch(%22DbUnitDatabasePopulator%22);%0A%0A
 %09%09Databa
@@ -1432,16 +1432,43 @@
 ation);%0A
+%09%09%09sw.start(%22populating%22);%0A
 %09%09%09opera
@@ -1509,16 +1509,236 @@
 ataSet);
+%0A%09%09%09sw.stop();%0A%09%09%09LOGGER.debug(sw.prettyPrint());%0A%0A%09%09%7D catch (BatchUpdateException e) %7B%0A%09%09%09LOGGER.error(%22BatchUpdateException while loading dataset%22, e);%0A%09%09%09LOGGER.error(%22Caused by : %22, e.getNextException());%0A%09%09%09throw e;
 %0A%0A%09%09%7D ca

add logs