<commit_before>/*******************************************************************************
 * Copyright © 2016 EMBL - European Bioinformatics Institute
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the
 * License.
 ******************************************************************************/

package org.mousephenotype.cda.loads.common.config;

import org.hibernate.SessionFactory;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;

import javax.sql.DataSource;

@Configuration
@PropertySource(value="file:${user.home}/configfiles/${profile}/datarelease.properties")
@EnableAutoConfiguration(exclude = {
        JndiConnectionFactoryAutoConfiguration.class,
        DataSourceAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class,
        JpaRepositoriesAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class,
        Neo4jDataAutoConfiguration.class
        })
/**
 * This configuration class holds configuration information shared by the data load create process.
 *
 * Created by mrelac on 18/08/2016.
 */
public class DataSourcesConfigApp {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    final private Integer POOL_THREADS = 1;

    @Value("${datasource.cda.url}")
    String cdaUrl;

    @Value("${datasource.cda.username}")
    String cdaUsername;

    @Value("${datasource.cda.password}")
    String cdaPassword;

    @Value("${datasource.dccEurophenomeFinal.url}")
    String dccEurophenomeFinalUrl;

    @Value("${datasource.dccEurophenomeFinal.username}")
    String dccEurophenomeFinalUsername;

    @Value("${datasource.dccEurophenomeFinal.password}")
    String dccEurophenomeFinalPassword;

    @Value("${datasource.cdabase.url}")
    String cdabaseUrl;

    @Value("${datasource.cdabase.username}")
    String cdabaseUsername;

    @Value("${datasource.cdabase.password}")
    String cdabasePassword;

    @Value("${datasource.dcc.url}")
    String dccUrl;

    @Value("${datasource.dcc.username}")
    String dccUsername;

    @Value("${datasource.dcc.password}")
    String dccPassword;



    private DataSource getConfiguredDatasource(String url, String username, String password) {
        org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setInitialSize(POOL_THREADS);
        ds.setMaxActive(50);
        ds.setMinIdle(POOL_THREADS);
        ds.setMaxIdle(POOL_THREADS);
        ds.setTestOnBorrow(true);
        ds.setValidationQuery("SELECT 1");
        ds.setTimeBetweenEvictionRunsMillis(15000);
        ds.setMinEvictableIdleTimeMillis(5000);
        ds.setValidationInterval(30000);
        ds.setRemoveAbandoned(true);
        ds.setRemoveAbandonedTimeout(10000); // 10 seconds before abandoning a query

        try {
            logger.info("Using cdasource database {} with initial pool size {}", ds.getConnection().getCatalog(), ds.getInitialSize());

        } catch (Exception e) { }

        return ds;
    }


    @Bean(name = "cdabaseDataSource")
    public DataSource cdabaseDataSource() {

//        DataSource ds = DataSourceBuilder
//                .create()
//                .url(cdabaseUrl)
//                .username(cdabaseUsername)
//                .password(<PASSWORD>)
//                .type(BasicDataSource.class)
//                .driverClassName("com.mysql.jdbc.Driver").build();
//        ((BasicDataSource) ds).setInitialSize(4);
//
//
//        ((BasicDataSource) ds).setLogAbandoned(false);
//        ((BasicDataSource) ds).setRemoveAbandoned(false);
//
//        try {<commit_msg>Specify additional DB connection parameters<commit_after>/*******************************************************************************
 * Copyright © 2016 EMBL - European Bioinformatics Institute
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the
 * License.
 ******************************************************************************/

package org.mousephenotype.cda.loads.common.config;

import org.hibernate.SessionFactory;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;

import javax.sql.DataSource;

@Configuration
@PropertySource(value="file:${user.home}/configfiles/${profile}/datarelease.properties")
@EnableAutoConfiguration(exclude = {
        JndiConnectionFactoryAutoConfiguration.class,
        DataSourceAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class,
        JpaRepositoriesAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class,
        Neo4jDataAutoConfiguration.class
        })
/**
 * This configuration class holds configuration information shared by the data load create process.
 *
 * Created by mrelac on 18/08/2016.
 */
public class DataSourcesConfigApp {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    final private Integer INITIAL_POOL_CONNECTIONS = 1;

    @Value("${datasource.cda.url}")
    String cdaUrl;

    @Value("${datasource.cda.username}")
    String cdaUsername;

    @Value("${datasource.cda.password}")
    String cdaPassword;

    @Value("${datasource.dccEurophenomeFinal.url}")
    String dccEurophenomeFinalUrl;

    @Value("${datasource.dccEurophenomeFinal.username}")
    String dccEurophenomeFinalUsername;

    @Value("${datasource.dccEurophenomeFinal.password}")
    String dccEurophenomeFinalPassword;

    @Value("${datasource.cdabase.url}")
    String cdabaseUrl;

    @Value("${datasource.cdabase.username}")
    String cdabaseUsername;

    @Value("${datasource.cdabase.password}")
    String cdabasePassword;

    @Value("${datasource.dcc.url}")
    String dccUrl;

    @Value("${datasource.dcc.username}")
    String dccUsername;

    @Value("${datasource.dcc.password}")
    String dccPassword;



    private DataSource getConfiguredDatasource(String url, String username, String password) {
        org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setInitialSize(INITIAL_POOL_CONNECTIONS);
        ds.setMaxActive(50);
        ds.setMinIdle(INITIAL_POOL_CONNECTIONS);
        ds.setMaxIdle(INITIAL_POOL_CONNECTIONS);
        ds.setTestOnBorrow(true);
        ds.setValidationQuery("SELECT 1");
        ds.setValidationInterval(5000);
        ds.setMaxAge(30000);
        ds.setMaxWait(35000);
        ds.setTestWhileIdle(true);
        ds.setTimeBetweenEvictionRunsMillis(5000);
        ds.setMinEvictableIdleTimeMillis(5000);
        ds.setValidationInterval(30000);
        ds.setRemoveAbandoned(true);
        ds.setRemoveAbandonedTimeout(10000); // 10 seconds before abandoning a query

        try {
            logger.info("Using cdasource database {} with initial pool size {}", ds.getConnection().getCatalog(), ds.getInitialSize());

        } catch (Exception e) { }

        return ds;
    }


    @Bean(name = "cdabaseDataSource")
    public DataSource cdabaseDataSource() {

//        DataSource ds = DataSourceBuilder
//                .create()
//                .url(cdabaseUrl)
//                .username(cdabaseUsername)
//                .password(<PASSWORD>)
//                .type(BasicDataSource.class)
//                .driverClassName("com.mysql.jdbc.Driver").build();
//        ((BasicDataSource) ds).setInitialSize(4);
//
//
//        ((BasicDataSource) ds).setLogAbandoned(false);
//        ((BasicDataSource) ds).setRemoveAbandoned(false);
//
//        try {
{'fraction_non_alphanumeric': 0.13312202852614896, 'fraction_numerical': 0.01016127528665983, 'mean_word_length': 4.006066262249184, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3527718', 'n_tokens_mistral': 3046, 'n_tokens_neox': 2871, 'n_words': 548}