package com.aranga.config;

import com.aranga.hbm.AppConfig;
import com.aranga.hbm.DataSourceDriver;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import java.util.Properties;


/**
 * Created by Aranga on 03/02/2016
 */
@Configuration
@EnableTransactionManagement
public class JPAConfig
{
    private static final Logger LOG = Logger.getLogger(JPAConfig.class);

    @Autowired
    private DataSourceDriver driver;

    @Autowired
    private AppConfig dbconfig;

    public JPAConfig()
    {
        LOG.info("JPA Configuration loaded!");
    }

    @Bean(name = "coreEntityManager")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory()
    {
        driver.create();
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(driver.create());
        em.setPackagesToScan(new String[]{"com"});
        JpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setShowSql(false);
        vendorAdapter.setGenerateDdl(false);

        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());
        return em;
    }

    @Autowired
    @Bean(name = "coreTxnManager")
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

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

    @Bean
    public Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "none");
        properties.setProperty("hibernate.dialect", driver.getDialect());
        properties.setProperty("hibernate.show_sql",dbconfig.isShowSql());
        properties.setProperty("hibernate.format_sql","true");

        return properties;
    }


}

@@ -1062,32 +1062,50 @@
 %0A%0A    @Autowired
+(required = false)
 %0A    private Dat
@@ -1367,32 +1367,180 @@
 Factory()%0A    %7B%0A
+        if (driver == null)%7B%0A            throw new IllegalStateException(%22cannot find db.type property with valid value ( myql,ms-sql)%22);%0A        %7D%0A
         driver.c

clean up config.properties