package com.rizki.mufrizal.spring.oauth2.custom.configuration;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import javax.sql.DataSource;
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.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

/**
 *
 * @Author Rizki Mufrizal <mufrizalrizki@gmail.com>
 * @Web <https://RizkiMufrizal.github.io>
 * @Since Jan 30, 2017
 * @Time 12:49:36 PM
 * @Encoding UTF-8
 * @Project Spring-OAuth2-Custom
 * @Package com.rizki.mufrizal.spring.oauth2.custom.configuration
 *
 */
@Configuration
@PropertySource("classpath:application.yml")
public class DataSourceConfiguration {

    @Autowired
    private Environment environment;

    @Value("classpath:schema.sql")
    private Resource schemaScript;

    @Value("classpath:data.sql")
    private Resource dataScript;

    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
        HikariConfig dataSourceConfig = new HikariConfig();
        dataSourceConfig.setDriverClassName(environment.getRequiredProperty("spring.datasource.driver-class-name"));
        dataSourceConfig.setJdbcUrl(environment.getRequiredProperty("spring.datasource.url"));
        dataSourceConfig.setUsername(environment.getRequiredProperty("spring.datasource.username"));
        dataSourceConfig.setPassword(environment.getRequiredProperty("spring.datasource.password"));
        dataSourceConfig.setMaximumPoolSize(Integer.parseInt(environment.getRequiredProperty("spring.datasource.maximumPoolSize")));
        dataSourceConfig.setMinimumIdle(Integer.parseInt(environment.getRequiredProperty("spring.datasource.minimumIdle")));
        dataSourceConfig.setMaxLifetime(Integer.parseInt(environment.getRequiredProperty("spring.datasource.maxLifetime")));
        dataSourceConfig.setConnectionTimeout(Long.parseLong(environment.getRequiredProperty("spring.datasource.connectionTimeout")));
        dataSourceConfig.setIdleTimeout(Long.parseLong(environment.getRequiredProperty("spring.datasource.idleTimeout")));
        dataSourceConfig.addDataSourceProperty("poolName", environment.getRequiredProperty("spring.datasource.poolName"));
        dataSourceConfig.addDataSourceProperty("cachePrepStmts", "true");
        dataSourceConfig.addDataSourceProperty("prepStmtCacheSize", "250");
        dataSourceConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        return new HikariDataSource(dataSourceConfig);
    }

    private DatabasePopulator databasePopulator() {
        final ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
        resourceDatabasePopulator.addScript(schemaScript);
        resourceDatabasePopulator.addScript(dataScript);
        return resourceDatabasePopulator;
    }

    @Bean
    public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
        final DataSourceInitializer initializer = new DataSourceInitializer();
        initializer.setDataSource(dataSource);
        initializer.setDatabasePopulator(databasePopulator());
        return initializer;
    }
}

@@ -143,75 +143,8 @@
 ce;%0A
-import org.springframework.jdbc.datasource.init.DatabasePopulator;%0A
 impo
@@ -160,32 +160,32 @@
 sql.DataSource;%0A
+
 import org.sprin
@@ -235,67 +235,8 @@
 ed;%0A
-import org.springframework.beans.factory.annotation.Value;%0A
 impo
@@ -458,199 +458,8 @@
 ent;
-%0Aimport org.springframework.core.io.Resource;%0Aimport org.springframework.jdbc.datasource.init.DataSourceInitializer;%0Aimport org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
 %0A%0A/*
@@ -883,146 +883,8 @@
 t;%0A%0A
-    @Value(%22classpath:schema.sql%22)%0A    private Resource schemaScript;%0A%0A    @Value(%22classpath:data.sql%22)%0A    private Resource dataScript;%0A%0A
     
@@ -2416,24 +2416,24 @@
 %22, %222048%22);%0A
+
         retu
@@ -2484,645 +2484,7 @@
    %7D
-%0A%0A    private DatabasePopulator databasePopulator() %7B%0A        final ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();%0A        resourceDatabasePopulator.addScript(schemaScript);%0A        resourceDatabasePopulator.addScript(dataScript);%0A        return resourceDatabasePopulator;%0A    %7D%0A%0A    @Bean%0A    public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) %7B%0A        final DataSourceInitializer initializer = new DataSourceInitializer();%0A        initializer.setDataSource(dataSource);%0A        initializer.setDatabasePopulator(databasePopulator());%0A        return initializer;%0A    %7D
 %0A%7D%0A

update datasource configuration