package <%=packageName%>.config;
import com.mongodb.Mongo;<% if (authenticationType == 'token') { %>
import com.mycompany.myapp.config.oauth2.OAuth2AuthenticationReadConverter;<% } %>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
@Configuration
@Profile("cloud")
@EnableMongoRepositories("<%=packageName%>.repository")
public class CloudMongoDbConfiguration extends AbstractMongoConfiguration {
private final Logger log = LoggerFactory.getLogger(CloudDatabaseConfiguration.class);
@Inject
private MongoDbFactory mongoDbFactory;
@Bean
public ValidatingMongoEventListener validatingMongoEventListener() {
return new ValidatingMongoEventListener(validator());
}
@Bean
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
}<% if (authenticationType == 'token') { %>
@Bean
public CustomConversions customConversions() {
List<Converter<?, ?>> converterList = new ArrayList<>();
OAuth2AuthenticationReadConverter converter = new OAuth2AuthenticationReadConverter();
converterList.add(converter);
return new CustomConversions(converterList);
}<% } %>
@Override
protected String getDatabaseName() {
return mongoDbFactory.getDb().getName();
}
@Override
public Mongo mongo() throws Exception {
return mongoDbFactory().getDb().getMongo();
}
}