package devopsdistilled.operp.server.config;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.instrument.classloading.SimpleLoadTimeWeaver;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@PropertySource("server/jdbc.properties")
public class JpaConfig {

    @Value("${jdbc.driverClassName}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("devopsdistilled.operp.server.model")
    private String packagesToScan;

    @Bean
    public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
	return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public DataSource dataSource() {
	BasicDataSource dataSource = new BasicDataSource();
	dataSource.setDriverClassName(driverClassName);
	dataSource.setUrl(url);
	dataSource.setUsername(username);
	dataSource.setPassword(password);
	dataSource.setInitialSize(2);
	dataSource.setMaxActive(10);
	return dataSource;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
	EclipseLinkJpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
	jpaVendorAdapter.setDatabase(Database.MYSQL);
	jpaVendorAdapter.setShowSql(true);
	jpaVendorAdapter.setGenerateDdl(false);
	jpaVendorAdapter
		.setDatabasePlatform("org.eclipse.persistence.platform.database.MySQLPlatform");
	return jpaVendorAdapter;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
	LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
	entityManagerFactory.setDataSource(this.dataSource());
	entityManagerFactory.setJpaVendorAdapter(this.jpaVendorAdapter());
	entityManagerFactory.setPackagesToScan(packagesToScan);
	entityManagerFactory.setLoadTimeWeaver(new SimpleLoadTimeWeaver());
	return entityManagerFactory;
    }
/*
    @Bean
    public JpaDialect jpaDialect() {
	return new EclipseLinkJpaDialect();
    }

    @Bean
    public JpaTransactionManager transactionManager() {
	JpaTransactionManager transactionManager = new JpaTransactionManager();
	transactionManager.setEntityManagerFactory(entityManagerFactory()
		.getObject());
	transactionManager.setJpaDialect(jpaDialect());
	return transactionManager;
    }*/

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslationPostProcessor() {
	return new PersistenceExceptionTranslationPostProcessor();
    }

}
@@ -39,16 +39,46 @@
 onfig;%0A%0A
+import java.util.Properties;%0A%0A
 import j
@@ -581,50 +581,82 @@
 ork.
-instrument.classloading.SimpleLoadTimeWeav
+orm.jpa.JpaDialect;%0Aimport org.springframework.orm.jpa.JpaTransactionManag
 er;%0A
@@ -877,27 +877,88 @@
 .vendor.
-EclipseLink
+HibernateJpaDialect;%0Aimport org.springframework.orm.jpa.vendor.Hibernate
 JpaVendo
@@ -1980,32 +1980,34 @@
 aSource;%0A    %7D%0A%0A
+ %0A
     @Bean%0A    pu
@@ -2050,27 +2050,25 @@
 er() %7B%0A%09
-EclipseLink
+Hibernate
 JpaVendo
@@ -2099,27 +2099,25 @@
 r = new 
-EclipseLink
+Hibernate
 JpaVendo
@@ -2200,14 +2200,18 @@
 .set
-ShowSq
+GenerateDd
 l(tr
@@ -2240,24 +2240,19 @@
 .set
-GenerateDdl(fals
+ShowSql(tru
 e);%0A
@@ -2301,59 +2301,39 @@
 org.
-eclipse.persistence.platform.database.MySQLPlatform
+hibernate.dialect.MySQL5Dialect
 %22);%0A
@@ -2491,35 +2491,18 @@
 ryBean e
-ntityManagerFactory
+mf
  = new L
@@ -2544,35 +2544,18 @@
 an();%0A%09e
-ntityManagerFactory
+mf
 .setData
@@ -2583,35 +2583,18 @@
 e());%0A%09e
-ntityManagerFactory
+mf
 .setJpaV
@@ -2634,35 +2634,18 @@
 r());%0A%09e
-ntityManagerFactory
+mf
 .setPack
@@ -2678,111 +2678,76 @@
 ;%0A%09e
-ntityManagerFactory.setLoadTimeWeaver(new SimpleLoadTimeWeaver());%0A%09return entityManagerFactory
+mf.setJpaProperties(this.hibernateProperties());%0A%09return emf
 ;%0A    %7D%0A
 /*%0A 
@@ -2746,10 +2746,8 @@
   %7D%0A
-/*
 %0A   
@@ -2806,19 +2806,17 @@
 new 
-EclipseLink
+Hibernate
 JpaD
@@ -3141,10 +3141,205 @@
    %7D
-*/
+%0A%0A    @Bean%0A    public Properties hibernateProperties() %7B%0A%09Properties hibernateProps = new Properties();%0A%09hibernateProps.setProperty(%22hibernate.hbm2ddl.auto%22, %22true%22);%0A%09return hibernateProps;%0A    %7D
 %0A%0A  

Configure Hibernate as Persistence Provider for JPA 2