package no.valg.valgnatt.database;

import java.util.Properties;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;

@Profile("!test")
@Configuration
public class DatabaseProdHibernateSpringConfiguration {

    @Resource
    private Environment env;

    @Bean
    public Properties hibernateProperties(){
        Properties props = new Properties();
        leggTilFellesPropertiesForTestOgProd(props);
        props.put("hibernate.transaction.factory_class", "org.hibernate.transaction.JTATransactionFactory");
        props.put("hibernate.transaction.manager_lookup_class", "org.hibernate.transaction.JBossTransactionManagerLookup");
        props.put("jta.UserTransaction", "java:jboss/UserTransaction");
        return props;
    }

    protected static void leggTilFellesPropertiesForTestOgProd(Properties props) {
        props.put("hibernate.jdbc.batch_size", "100");
        props.put("hibernate.order_inserts", "true");
        props.put("hibernate.order_updates", "true");
        props.put("hibernate.jdbc.lob.non_contextual_creation", "true");
    }

    @Bean
    public DataSource dataSource() {
        return new JndiDataSourceLookup().getDataSource(env.getProperty("pm.jndiname"));
    }

}
