package fi.hsl.parkandride.config;

import java.sql.Connection;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.PlatformTransactionManager;

import com.mysema.query.sql.SQLTemplates;
import com.mysema.query.sql.postgres.PostgresQueryFactory;
import com.mysema.query.sql.spatial.PostGISTemplates;
import com.mysema.query.sql.types.EnumByNameType;

import fi.hsl.parkandride.core.domain.CapacityType;

@Configuration
@Import({
        PropertyPlaceholderAutoConfiguration.class,
        JdbcConfiguration.H2.class
    })
public class JdbcConfiguration {

    @Configuration
    @Profile({"!psql"})
    public static class H2 {

        public H2() {
            System.out.println("USING H2");
        }

        @Bean
        public SQLTemplates sqlTemplates() {
            return new H2GISTemplates();
        }

    }

    @Configuration
    @Profile("psql")
    public static class Postgresql {

        public Postgresql() {
            System.out.println("USING POSTGRESQL");
        }

        @Bean
        public SQLTemplates sqlTemplates() {
            return new PostGISTemplates();
        }
    }

    @Inject SQLTemplates sqlTemplates;
    @Inject DataSource dataSource;

    @Bean
    public PostgresQueryFactory queryFactory() {
        return new PostgresQueryFactory(querydslConfiguration(), connectionProvider());
    }

    @Bean
    public Provider<Connection> connectionProvider() {
        return () -> {
            Connection conn = DataSourceUtils.getConnection(dataSource);
            if (!DataSourceUtils.isConnectionTransactional(conn, dataSource)) {
                throw new RuntimeException("Connection should be transactional");
            }
            return conn;
        };
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public com.mysema.query.sql.Configuration querydslConfiguration() {
        com.mysema.query.sql.Configuration conf = new com.mysema.query.sql.Configuration(sqlTemplates);
        conf.register("CAPACITY", "CAPACITY_TYPE", new EnumByNameType<>(CapacityType.class));
//        conf.register("FACILITY", "BORDER", H2PolygonType.DEFAULT);
        return conf;
    }
}

@@ -982,112 +982,8 @@
 ion%0A
-@Import(%7B%0A        PropertyPlaceholderAutoConfiguration.class,%0A        JdbcConfiguration.H2.class%0A    %7D)%0A
 publ

remove unnecessary configuration imports