package org.mousephenotype.cda;

import org.hibernate.SessionFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by jmason on 29/06/2015.
 */
@Configuration
@EnableAutoConfiguration
@ComponentScan("org.mousephenotype.cda")
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "org.mousephenotype.cda.dao",
	entityManagerFactoryRef = "internalEntityManagerFactory",
	transactionManagerRef = "internalTransactionManager")
public class TestConfig {

	public static final String INTERNAL = "internal";

	@Bean(name = "komp2DataSource")
	@Primary
	public DataSource dataSource() {

		DataSource dataSource = new EmbeddedDatabaseBuilder()
			.setType(EmbeddedDatabaseType.H2)
			.setName("komp2")
			.build();
		DatabasePopulatorUtils.execute(createDatabasePopulator(), dataSource);

		return dataSource;
	}

	private DatabasePopulator createDatabasePopulator() {
		ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
		databasePopulator.setContinueOnError(true);
		databasePopulator.addScript(new ClassPathResource("sql/test-data.sql"));
		return databasePopulator;
	}

	@Bean(name = "internalEntityManagerFactory")
	@Primary
	public LocalContainerEntityManagerFactoryBean internalEntityManagerFactory(EntityManagerFactoryBuilder builder) {

		Map<String, Object> properties = new HashMap<>();
		properties.put("hibernate.hbm2ddl.auto", "create");

		return builder.dataSource(dataSource())
		              .packages("org.mousephenotype.cda.pojo", "org.mousephenotype.cda.dao")
		              .persistenceUnit(INTERNAL)
		              .properties(properties)
		              .build();
	}

	@Bean(name = "internalTransactionManager")
	@Primary
	public PlatformTransactionManager internalTransactionManager() {
		JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
		jpaTransactionManager.setDataSource(dataSource());
		jpaTransactionManager.setPersistenceUnitName(INTERNAL);
		return jpaTransactionManager;
	}

	@Bean(name = "admintoolsDataSource")
	public DataSource admintoolsDataSource() {
		return new EmbeddedDatabaseBuilder()
			.setType(EmbeddedDatabaseType.H2)
				.setName("admin_tools")
				//			.addScript("classpath:sql/schema.sql")
				//			.addScript("classpath:sql/test-data.sql")
			.build();
	}

	@Bean(name = "sessionFactory")
	public SessionFactory getSessionFactory(DataSource dataSource) {

		LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);

		sessionBuilder.scanPackages("org.mousephenotype.cda.dao");
		sessionBuilder.scanPackages("org.mousephenotype.cda.pojo");

		return sessionBuilder.buildSessionFactory();
	}
}

@@ -450,62 +450,8 @@
 ry;%0A
-import org.springframework.core.io.ClassPathResource;%0A
 impo
@@ -678,222 +678,8 @@
 pe;%0A
-import org.springframework.jdbc.datasource.init.DatabasePopulator;%0Aimport org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;%0Aimport org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;%0A
 impo
@@ -1723,16 +1723,18 @@
 uild();%0A
+//
 %09%09Databa
@@ -1824,16 +1824,18 @@
 ce;%0A%09%7D%0A%0A
+//
 %09private
@@ -1881,16 +1881,18 @@
 tor() %7B%0A
+//
 %09%09Resour
@@ -1964,16 +1964,18 @@
 ator();%0A
+//
 %09%09databa
@@ -2012,16 +2012,18 @@
 (true);%0A
+//
 %09%09databa
@@ -2089,16 +2089,18 @@
 sql%22));%0A
+//
 %09%09return
@@ -2119,16 +2119,18 @@
 ulator;%0A
+//
 %09%7D%0A%0A%09@Be
@@ -2402,16 +2402,89 @@
 reate%22);
+%0A%09%09properties.put(%22hibernate.hbm2ddl.import_files%22, %22sql/test-data.sql%22);
 %0A%0A%09%09retu

update test methods to populate database with some test data