Subversion Repositories SmartDukaan

Rev

Rev 23898 | Rev 25721 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.smartdukaan.cron.config;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import com.spice.profitmandi.dao.repository.dtr.Mongo;

@Configuration
@EnableTransactionManagement
@ComponentScan("com.spice.profitmandi.*")
@PropertySource("classpath:META-INF/env.properties")
public class DBConfig {

        private static final Logger LOGGER=LogManager.getLogger(DBConfig.class);

        
        private static final String HIBERNATE_DIALECT = "hibernate.dialect";

        private static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql";

        private static final String HIBERNATE_FORMAT_SQL = "hibernate.format_sql";

        private static final String HIBERNATE_JDBC_BATCH_SIZE = "hibernate.jdbc.batch_size";

        private static final String HIBERNATE_C3P0_MIN_SIZE = "hibernate.c3p0.min_size";

        private static final String HIBERNATE_C3P0_MAX_SIZE = "hibernate.c3p0.max_size";

        private static final String HIBERNATE_C3P0_TIMEOUT = "hibernate.c3p0.timeout";

        private static final String HIBERNATE_C3P0_MAX_STATEMENTS = "hibernate.c3p0.max_statements";

        private static final String HIBERNATE_C3P0_IDLE_TEST_PERIOD = "hibernate.c3p0.idle_test_period";

        @Value("${hibernate.driver.class}")
        private String hibernateDriverClass;

        @Value("${hibernate.url}")
        private String hibernateUrl;

        @Value("${hibernate.user.name}")
        private String hibernateUserName;

        @Value("${hibernate.password}")
        private String hibernatePassword;

        @Value("${hibernate.dialect}")
        private String hibernateDialect;

        @Value("${hibernate.show_sql}")
        private String hibernateShowSql;

        @Value("${hibernate.format_sql}")
        private String hibernateFormatSql;

        @Value("${hibernate.jdbc.batch_size}")
        private String hibernateBatchSize;

        @Value("${hibernate.c3p0.min_size}")
        private String hibernateMinSize;

        @Value("${hibernate.c3p0.max_size}")
        private String hibernateMaxSize;

        @Value("${hibernate.c3p0.timeout}")
        private String hibernateTimeout;

        @Value("${hibernate.c3p0.max_statements}")
        private String hibernateMaxStatements;

        @Value("${hibernate.c3p0.idle_test_period}")
        private String hibernateIdleTestPeriod;
        
        @Value("${mongo.host}")
        private String mongoHost;

        @Value("${content.mongo.host}")
        private String contentMongoHost;
        
        @Primary
        @Bean(name = "dataSource")
        public DataSource dataSource() {
                DriverManagerDataSource dataSource = new DriverManagerDataSource();
                dataSource.setDriverClassName(hibernateDriverClass);
                dataSource.setUrl(hibernateUrl);
                dataSource.setUsername(hibernateUserName);
                dataSource.setPassword(hibernatePassword);
                LOGGER.info("DataSource returned {}", dataSource);
                return dataSource;
        }
        
        @Bean
        public ObjectMapper objectMapper() {
                DateTimeFormatter df = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .append(DateTimeFormatter.ISO_LOCAL_DATE)
                .optionalStart()
                .appendLiteral('T')
                .optionalEnd()
                .appendLiteral(' ')
                .append(DateTimeFormatter.ISO_LOCAL_TIME)
                .toFormatter();
                DateTimeFormatter sf = new DateTimeFormatterBuilder()
                                .parseCaseInsensitive()
                                .append(DateTimeFormatter.ISO_LOCAL_DATE)
                                .appendLiteral('T')
                                .append(DateTimeFormatter.ISO_LOCAL_TIME)
                                .toFormatter();
                LocalDateTimeSerializer serializer = new LocalDateTimeSerializer(sf);
                LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(df);
                JavaTimeModule jtm = new JavaTimeModule();
                jtm.addSerializer(LocalDateTime.class, serializer);
                jtm.addDeserializer(LocalDateTime.class, deserializer);
                ObjectMapper mapper = new ObjectMapper()
                                   .registerModule(new ParameterNamesModule())
                                   .registerModule(new Jdk8Module())
                                   .registerModule(jtm); // new module, NOT JSR310Module
                return mapper;
        }

        @Bean
        public Properties getHibernateProperties() {
                Properties dbProperties = new Properties();
                dbProperties.put(HIBERNATE_DIALECT, hibernateDialect);
                dbProperties.put(HIBERNATE_SHOW_SQL, hibernateShowSql);
                dbProperties.put(HIBERNATE_FORMAT_SQL, hibernateFormatSql);
                dbProperties.put(HIBERNATE_JDBC_BATCH_SIZE, hibernateBatchSize);
                dbProperties.put(HIBERNATE_C3P0_MIN_SIZE, hibernateMinSize);
                dbProperties.put(HIBERNATE_C3P0_MAX_SIZE, hibernateMaxSize);
                dbProperties.put(HIBERNATE_C3P0_TIMEOUT, hibernateTimeout);
                dbProperties.put(HIBERNATE_C3P0_MAX_STATEMENTS, hibernateMaxStatements);
                dbProperties.put(HIBERNATE_C3P0_IDLE_TEST_PERIOD, hibernateIdleTestPeriod);
                return dbProperties;
        }
        
        @Primary
        @Autowired
        @Bean(name = "sessionFactory")
        public SessionFactory getSessionFactory(DataSource dataSource) {
                LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
                sessionBuilder.addProperties(getHibernateProperties());
                sessionBuilder.scanPackages("com.spice.profitmandi.dao.*");
                return sessionBuilder.buildSessionFactory();
        }
        
        @Primary
        @Autowired
        @Bean(name = "transactionManager")
        public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
                HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
                return transactionManager;
        }
        
        @Bean
        public Mongo mongoClient(SessionFactory sessionFactory) {
                return new Mongo(mongoHost, contentMongoHost);
        }
}