<%#
 Copyright 2013-2023 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 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

      https://www.apache.org/licenses/LICENSE-2.0

 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.
-%>

<%_
    let uuidConversionNeeded = (reactive && !prodDatabaseTypePostgresql);
_%>
package <%= packageName %>.config;

<%_ if (devDatabaseTypeH2Any) { _%>
import tech.jhipster.config.JHipsterConstants;
import tech.jhipster.config.h2.H2ConfigurationHelper;
<%_ } _%>
<%_ if (databaseTypeSql && reactive) { _%>
import io.r2dbc.spi.ConnectionFactory;
<%_ } _%>
<%_ if (devDatabaseTypeH2Any) { _%>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
<%_ } _%>
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
<%_ if (devDatabaseTypeH2Any) { _%>
import org.springframework.context.annotation.Profile;
<%_ } _%>
<%_ if (reactive) { _%>
import org.springframework.core.convert.converter.Converter;
<%_ } _%>
<%_ if (searchEngineElasticsearch) { _%>
import org.springframework.data.elasticsearch.repository.config.Enable<% if (reactive) { %>Reactive<% } %>ElasticsearchRepositories;
<%_ } _%>
<%_ if (devDatabaseTypeH2Any) { _%>
import org.springframework.core.env.Environment;
<%_ } _%>
<%_ if (reactive) { _%>
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.r2dbc.convert.MappingR2dbcConverter;
import org.springframework.data.r2dbc.convert.R2dbcCustomConversions;
import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.dialect.DialectResolver;
import org.springframework.data.r2dbc.dialect.R2dbcDialect;
import org.springframework.data.r2dbc.query.UpdateMapper;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
import org.springframework.data.relational.core.dialect.RenderContextFactory;
import org.springframework.data.relational.core.sql.render.SqlRenderer;
<%_ } else { _%>
  <%_ if (ormTool === 'mybatis') { _%>
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
    <%_ if (useMultiTenant) { _%>
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
    <%_ } _%>
    <%_ if (softDelete) { _%>
import com.diboot.core.methods.RecycleBinSqlInjector;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
    <%_ } _%>
import org.mybatis.spring.annotation.MapperScan;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
      <%_ if (['oracle', 'postgresql'].includes(prodDatabaseType)) { _%>
import com.baomidou.mybatisplus.extension.incrementer.*;
      <%_ } _%>
  <%_ } else { _%>
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
      <%_ if (softDelete) { _%>
import com.huibozhixin.softdelete.repository.EnableJpaSoftDeleteRepositories;
      <%_ } else { _%>
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.cosium.spring.data.jpa.entity.graph.repository.support.EntityGraphJpaRepositoryFactoryBean;
      <%_ } _%>
  <%_ } _%>
<%_ } _%>
import org.springframework.transaction.annotation.EnableTransactionManagement;

<%_ if (devDatabaseTypeH2Any) { _%>
import java.sql.SQLException;
<%_ } _%>
<%_ if (reactive) { _%>
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
import java.util.List;
    <%_ if (uuidConversionNeeded) { _%>
import java.util.UUID;
    <%_ } _%>
<%_ } _%>

@Configuration
<%_ if (reactive) { _%>
@EnableR2dbcRepositories({<%- domains.map(domain => `"${domain}.repository"`).join(', ') %>})
<%_ } else { _%>
    <%_ if (ormTool === 'hibernate') { _%>
        <%_ if (softDelete) { _%>
@EnableJpaSoftDeleteRepositories({<%- domains.map(domain => `"${domain}.**.repository"`).join(', ') %>})
        <%_ } else { _%>
@EnableJpaRepositories(value = {<%- domains.map(domain => `"${domain}.**.repository"`).join(', ') %>}, repositoryFactoryBeanClass = EntityGraphJpaRepositoryFactoryBean.class)
        <%_ } _%>
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
    <%_ } else { _%>
@MapperScan(basePackages = {"com.diboot.core", <%- domains.map(domain => `"${domain}.**.repository"`).join(', ') %>, "<%= packageName %>.**.repository"}, annotationClass = Mapper.class)
@ComponentScan(
    basePackages = { "com.diboot.core"},
    excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = { Repository.class }) }
)
    <%_ } _%>
<%_ } _%>
@EnableTransactionManagement
<%_ if (searchEngineElasticsearch) { _%>
@Enable<% if (reactive) { %>Reactive<% } %>ElasticsearchRepositories({"<%= packageName %>.repository.search", "<%= packageName %>.**.repository.search"})
<%_ } _%>
public class DatabaseConfiguration {

<%_ if (devDatabaseTypeH2Any) { _%>
    private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class);

    private final Environment env;

    public DatabaseConfiguration(Environment env) {
        this.env = env;
    }

    /**
     * Open the TCP port for the H2 database, so it is available remotely.
     *
     * @return the H2 database TCP server.
     * @throws SQLException if the server failed to start.
     */
    @Bean(initMethod = "start", destroyMethod = "stop")
    @Profile(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)
    public Object h2TCPServer() throws SQLException {
        String port = getValidPortForH2();
        log.debug("H2 database is available on port {}", port);
        return H2ConfigurationHelper.createServer(port);
    }

    private String getValidPortForH2() {
        int port = Integer.parseInt(env.getProperty("server.port"));
        if (port < 10000) {
            port = 10000 + port;
        } else {
            if (port < 63536) {
                port = port + 2000;
            } else {
                port = port - 2000;
            }
        }
        return String.valueOf(port);
    }
<%_ } _%>
<%_ if (ormTool === 'mybatis') { _%>

  <%_ if (prodDatabaseType === 'postgresql') { _%>
    @Bean
    public PostgreKeyGenerator postgreKeyGenerator() {
        return new PostgreKeyGenerator();
    }
  <%_ } _%>
  <%_ if (prodDatabaseType === 'oracle') { _%>
    @Bean
    public OracleKeyGenerator oracleKeyGenerator(){
        return new OracleKeyGenerator();
    }
  <%_ } _%>

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  <%_ if (useMultiTenant) { _%>
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantHandler()));
  <%_ } _%>
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
  <%_ if (softDelete) { _%>

    @Bean
    public ISqlInjector iSqlInjector() {
        return new RecycleBinSqlInjector();
    }
  <%_ } _%>
<%_ } _%>
<% if (uuidConversionNeeded) { %>
   /**
    * Simple singleton to convert {@link UUID}s to their {@link String} representation.
    */
    @WritingConverter
    public enum UUIDToStringConverter implements Converter<UUID, String> {
        INSTANCE;

        @Override
        public String convert(UUID source) {
            return source == null ? null : source.toString();
        }
    }

    /**
    * Simple singleton to convert from {@link String} {@link UUID} representation.
    */
    @ReadingConverter
    public enum StringToUUIDConverter implements Converter<String, UUID> {
        INSTANCE;

        @Override
        public UUID convert(String source) {
            return source == null ? null : UUID.fromString(source);
        }

    }

<%_ } _%>
<%_ if (reactive) { _%>
    // LocalDateTime seems to be the only type that is supported across all drivers atm
    // See https://github.com/r2dbc/r2dbc-h2/pull/139 https://github.com/mirromutth/r2dbc-mysql/issues/105
    @Bean
    public R2dbcCustomConversions r2dbcCustomConversions(R2dbcDialect dialect) {
        List<Object> converters = new ArrayList<>();
        converters.add(InstantWriteConverter.INSTANCE);
        converters.add(InstantReadConverter.INSTANCE);
        converters.add(BitSetReadConverter.INSTANCE);
        converters.add(DurationWriteConverter.INSTANCE);
        converters.add(DurationReadConverter.INSTANCE);
        converters.add(ZonedDateTimeReadConverter.INSTANCE);
        converters.add(ZonedDateTimeWriteConverter.INSTANCE);
  <%_ if (uuidConversionNeeded) { _%>
        converters.add(StringToUUIDConverter.INSTANCE);
        converters.add(UUIDToStringConverter.INSTANCE);
  <%_ } _%>
        return R2dbcCustomConversions.of(dialect, converters);
    }

    @Bean
    public R2dbcDialect dialect(ConnectionFactory connectionFactory) {
        return DialectResolver.getDialect(connectionFactory);
    }

    @Bean
    public UpdateMapper updateMapper(R2dbcDialect dialect, MappingR2dbcConverter mappingR2dbcConverter) {
        return new UpdateMapper(dialect, mappingR2dbcConverter);
    }

    @Bean
    public SqlRenderer sqlRenderer(R2dbcDialect dialect) {
        RenderContextFactory factory = new RenderContextFactory(dialect);
        return SqlRenderer.create(factory.createRenderContext());
    }

    @WritingConverter
    public enum InstantWriteConverter implements Converter<Instant, LocalDateTime> {
        INSTANCE;

        public LocalDateTime convert(Instant source) {
            return LocalDateTime.ofInstant(source, ZoneOffset.UTC);
        }
    }

    @ReadingConverter
    public enum InstantReadConverter implements Converter<LocalDateTime, Instant> {
        INSTANCE;

        @Override
        public Instant convert(LocalDateTime localDateTime) {
            return localDateTime.toInstant(ZoneOffset.UTC);
        }
    }

    @ReadingConverter
    public enum BitSetReadConverter implements Converter<BitSet, Boolean> {
        INSTANCE;

        @Override
        public Boolean convert(BitSet bitSet) {
            return bitSet.get(0);
        }
    }

    @ReadingConverter
    public enum ZonedDateTimeReadConverter implements Converter<LocalDateTime, ZonedDateTime> {
        INSTANCE;

        @Override
        public ZonedDateTime convert(LocalDateTime localDateTime) {
            // Be aware - we are using the UTC timezone
            return ZonedDateTime.of(localDateTime, ZoneOffset.UTC);
        }
    }

    @WritingConverter
    public enum ZonedDateTimeWriteConverter implements Converter<ZonedDateTime, LocalDateTime> {
        INSTANCE;

        @Override
        public LocalDateTime convert(ZonedDateTime zonedDateTime) {
            return zonedDateTime.toLocalDateTime();
        }
    }

    @WritingConverter
    public enum DurationWriteConverter implements Converter<Duration, Long> {
        INSTANCE;

        @Override
        public Long convert(Duration source) {
            return source != null ? source.toMillis() : null;
        }
    }

    @ReadingConverter
    public enum DurationReadConverter implements Converter<Long, Duration> {
        INSTANCE;

        @Override
        public Duration convert(Long source) {
            return source != null ? Duration.ofMillis(source) : null;
        }
    }

<%_ } _%>
}
