package org.mousephenotype.cda.db;

import org.hibernate.SessionFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

/**
 * Komp2DatasourceConfig holds the configuration for the komp2 datasource
 */

@Configuration
@EnableAutoConfiguration
@ComponentScan("org.mousephenotype.cda.db")
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "org.mousephenotype.cda.db", entityManagerFactoryRef = "emf")
public class Komp2DatasourceConfig {

	@Bean
	@Primary
	@ConfigurationProperties(prefix = "datasource.komp2")
	public DataSource komp2DataSource() {
		return DataSourceBuilder.create().build();
	}

	@Bean
	@Primary
	public LocalContainerEntityManagerFactoryBean emf(EntityManagerFactoryBuilder builder){
		return builder
			.dataSource(komp2DataSource())
			.packages("org.mousephenotype.cda.db")
			.persistenceUnit("komp2")
			.build();
	}

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

		LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);

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

		return sessionBuilder.buildSessionFactory();
	}

	@Bean(name = "komp2JpaSessionFactory")
	@Primary
	public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
		HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
		factory.setEntityManagerFactory(emf);
		return factory;
	}

}

@@ -33,45 +33,8 @@
 b;%0A%0A
-import org.hibernate.SessionFactory;%0A
 impo
@@ -92,32 +92,32 @@
 oConfiguration;%0A
+
 import org.sprin
@@ -639,78 +639,8 @@
 es;%0A
-import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;%0A
 impo
@@ -1672,32 +1672,32 @@
 %09%09.build();%0A%09%7D%0A%0A
+
 %09@Bean(name = %22s
@@ -1716,387 +1716,8 @@
 y%22)%0A
-%09public SessionFactory getSessionFactory(DataSource dataSource) %7B%0A%0A%09%09LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);%0A%0A%09%09sessionBuilder.scanPackages(%22org.mousephenotype.cda.db.dao%22);%0A%09%09sessionBuilder.scanPackages(%22org.mousephenotype.cda.db.pojo%22);%0A%0A%09%09return sessionBuilder.buildSessionFactory();%0A%09%7D%0A%0A%09@Bean(name = %22komp2JpaSessionFactory%22)%0A
 %09@Pr

replace session factor implementation with a cleaner configuration