Subversion Repositories SmartDukaan

Rev

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

package com.smartdukaan.cron.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
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.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.spice.profitmandi.dao.convertor.LocalDateJsonConverter;
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
import com.spice.profitmandi.dao.repository.dtr.Mongo;
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.*;
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 javax.sql.DataSource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Properties;

@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);
                LocalDateSerializer serializer1 = new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE);
                LocalDateDeserializer deserializer1 = new LocalDateDeserializer(DateTimeFormatter.ISO_LOCAL_DATE);
                JavaTimeModule jtm = new JavaTimeModule();
                jtm.addSerializer(LocalDateTime.class, serializer);
                jtm.addSerializer(LocalDate.class, serializer1);
                jtm.addDeserializer(LocalDateTime.class, deserializer);
                jtm.addDeserializer(LocalDate.class, deserializer1);
                ObjectMapper mapper = new ObjectMapper()
                                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                                .registerModule(new ParameterNamesModule())
                                .registerModule(new Jdk8Module()).registerModule(jtm); // new module, NOT JSR310Module
                LOGGER.info("ObjectMapper returned {}", mapper);
                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.*");
                LOGGER.info("Session Factory returned {}", "sessionFactory");
                return sessionBuilder.buildSessionFactory();
        }

        @Primary
        @Autowired
        @Bean(name = "transactionManager")
        public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
                HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
                LOGGER.info("Transaction Manager returned {}", transactionManager);
                return transactionManager;
        }

        @Bean
        public Mongo mongoClient(SessionFactory sessionFactory) {
                return new Mongo(mongoHost, contentMongoHost);
        }

        @Bean(name = "gson")
        public Gson gson() {
                Gson gson = new GsonBuilder().serializeNulls()
                                .registerTypeAdapter(LocalDate.class, new LocalDateJsonConverter())
                                .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
                LOGGER.info("Gson returned {}", gson);
                return gson;

        }
}