All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.mongock.driver.mongodb.springdata.v4.config.SpringDataMongoV4ContextBase Maven / Gradle / Ivy

The newest version!
package io.mongock.driver.mongodb.springdata.v4.config;

import com.mongodb.*;
import io.mongock.api.config.*;
import io.mongock.driver.api.driver.*;
import io.mongock.driver.mongodb.springdata.v4.*;
import org.springframework.context.annotation.*;
import org.springframework.data.mongodb.core.*;
import org.springframework.transaction.*;

import java.util.*;

public abstract class SpringDataMongoV4ContextBase {

  @Bean
  public ConnectionDriver connectionDriver(MongoTemplate mongoTemplate,
                                           CONFIG config,
                                           MongoDBConfiguration mongoDbConfig,
                                           Optional txManagerOpt) {
    DRIVER driver = buildDriver(mongoTemplate, config, mongoDbConfig);
    setGenericDriverConfig(config, txManagerOpt, driver);
    setMongoDBConfig(mongoDbConfig, driver);
    driver.initialize();
    return driver;
  }

  protected abstract DRIVER buildDriver(MongoTemplate mongoTemplate,
                                        CONFIG config,
                                        MongoDBConfiguration mongoDbConfig);

  private void setGenericDriverConfig(CONFIG config,
                                      Optional txManagerOpt,
                                      DRIVER driver) {
    if (config.getTransactional().isPresent()) {
      if (config.getTransactional().get()) {
        driver.enableTransaction();
      }
    }
    else {
      txManagerOpt.filter(tx -> config.getTransactionEnabled().orElse(true)).ifPresent(tx -> driver.enableTransaction());
    }
    driver.setMigrationRepositoryName(config.getMigrationRepositoryName());
    driver.setLockRepositoryName(config.getLockRepositoryName());
    driver.setIndexCreation(config.isIndexCreation());
  }

  private void setMongoDBConfig(MongoDBConfiguration mongoDbConfig, DRIVER driver) {
    driver.setWriteConcern(mongoDbConfig.getBuiltMongoDBWriteConcern());
    driver.setReadConcern(new ReadConcern(mongoDbConfig.getReadConcern()));
    driver.setReadPreference(mongoDbConfig.getReadPreference().getValue());
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy