Search in sources :

Example 1 with MongoCustomConversions

use of org.springframework.data.mongodb.core.convert.MongoCustomConversions in project spring-integration by spring-projects.

the class AbstractConfigurableMongoDbMessageStore method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    if (this.mongoTemplate == null) {
        if (this.mappingMongoConverter == null) {
            this.mappingMongoConverter = new MappingMongoConverter(new DefaultDbRefResolver(this.mongoDbFactory), new MongoMappingContext());
            this.mappingMongoConverter.setApplicationContext(this.applicationContext);
            List<Object> customConverters = new ArrayList<Object>();
            customConverters.add(new MessageToBinaryConverter());
            customConverters.add(new BinaryToMessageConverter());
            this.mappingMongoConverter.setCustomConversions(new MongoCustomConversions(customConverters));
            this.mappingMongoConverter.afterPropertiesSet();
        }
        this.mongoTemplate = new MongoTemplate(this.mongoDbFactory, this.mappingMongoConverter);
    }
    this.messageBuilderFactory = IntegrationUtils.getMessageBuilderFactory(this.applicationContext);
    IndexOperations indexOperations = this.mongoTemplate.indexOps(this.collectionName);
    indexOperations.ensureIndex(new Index(MessageDocumentFields.MESSAGE_ID, Sort.Direction.ASC));
    indexOperations.ensureIndex(new Index(MessageDocumentFields.GROUP_ID, Sort.Direction.ASC).on(MessageDocumentFields.MESSAGE_ID, Sort.Direction.ASC).unique());
    indexOperations.ensureIndex(new Index(MessageDocumentFields.GROUP_ID, Sort.Direction.ASC).on(MessageDocumentFields.LAST_MODIFIED_TIME, Sort.Direction.DESC).on(MessageDocumentFields.SEQUENCE, Sort.Direction.DESC));
}
Also used : MessageToBinaryConverter(org.springframework.integration.mongodb.support.MessageToBinaryConverter) IndexOperations(org.springframework.data.mongodb.core.index.IndexOperations) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) ArrayList(java.util.ArrayList) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) Index(org.springframework.data.mongodb.core.index.Index) BinaryToMessageConverter(org.springframework.integration.mongodb.support.BinaryToMessageConverter) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate)

Example 2 with MongoCustomConversions

use of org.springframework.data.mongodb.core.convert.MongoCustomConversions in project spring-data-mongodb by spring-projects.

the class MongoTemplate method getDefaultMongoConverter.

private static MongoConverter getDefaultMongoConverter(MongoDatabaseFactory factory) {
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
    MongoCustomConversions conversions = new MongoCustomConversions(Collections.emptyList());
    MongoMappingContext mappingContext = new MongoMappingContext();
    mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
    mappingContext.afterPropertiesSet();
    MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mappingContext);
    converter.setCustomConversions(conversions);
    converter.setCodecRegistryProvider(factory);
    converter.afterPropertiesSet();
    return converter;
}
Also used : DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) DbRefResolver(org.springframework.data.mongodb.core.convert.DbRefResolver) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver)

Example 3 with MongoCustomConversions

use of org.springframework.data.mongodb.core.convert.MongoCustomConversions in project spring-data-mongodb by spring-projects.

the class MappingMongoJsonSchemaCreatorUnitTests method converterRegistered.

// DATAMONGO-1849
@Test
public void converterRegistered() {
    MappingMongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext);
    MongoCustomConversions mcc = new MongoCustomConversions(Collections.singletonList(SimpleToDocumentConverter.INSTANCE));
    converter.setCustomConversions(mcc);
    converter.afterPropertiesSet();
    schemaCreator = new MappingMongoJsonSchemaCreator(converter);
    MongoJsonSchema schema = schemaCreator.createSchemaFor(WithNestedDomainType.class);
    assertThat(schema.toDocument().get("$jsonSchema", Document.class)).isEqualTo("{ 'type' : 'object', 'properties' : { '_id' : { 'type' : 'object' }, 'nested' : { 'type' : 'object' } } }");
}
Also used : MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) MongoJsonSchema(org.springframework.data.mongodb.core.schema.MongoJsonSchema) Test(org.junit.jupiter.api.Test)

Example 4 with MongoCustomConversions

use of org.springframework.data.mongodb.core.convert.MongoCustomConversions in project spring-data-mongodb by spring-projects.

the class SessionBoundMongoTemplateTests method getDefaultMongoConverter.

private MongoConverter getDefaultMongoConverter(MongoDatabaseFactory factory) {
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
    MongoCustomConversions conversions = new MongoCustomConversions(Collections.emptyList());
    MongoMappingContext mappingContext = new MongoMappingContext();
    mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
    mappingContext.afterPropertiesSet();
    MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mappingContext);
    converter.setCustomConversions(conversions);
    converter.setCodecRegistryProvider(factory);
    converter.afterPropertiesSet();
    return converter;
}
Also used : DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) DbRefResolver(org.springframework.data.mongodb.core.convert.DbRefResolver) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver)

Example 5 with MongoCustomConversions

use of org.springframework.data.mongodb.core.convert.MongoCustomConversions in project spring-data-mongodb by spring-projects.

the class SpringDataMongodbSerializerUnitTests method takesCustomConversionForEnumsIntoAccount.

// DATAMONGO-1485
@Test
public void takesCustomConversionForEnumsIntoAccount() {
    MongoMappingContext context = new MongoMappingContext();
    MappingMongoConverter converter = new MappingMongoConverter(dbFactory, context);
    converter.setCustomConversions(new MongoCustomConversions(Collections.singletonList(new SexTypeWriteConverter())));
    converter.afterPropertiesSet();
    this.converter = converter;
    this.serializer = new SpringDataMongodbSerializer(this.converter);
    Object mappedPredicate = serializer.handle(QPerson.person.sex.eq(Sex.FEMALE));
    assertThat(mappedPredicate).isInstanceOf(Document.class);
    assertThat(((Document) mappedPredicate).get("sex")).isEqualTo("f");
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Aggregations

MongoCustomConversions (org.springframework.data.mongodb.core.convert.MongoCustomConversions)12 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)10 MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)9 DefaultDbRefResolver (org.springframework.data.mongodb.core.convert.DefaultDbRefResolver)6 Document (org.bson.Document)4 DbRefResolver (org.springframework.data.mongodb.core.convert.DbRefResolver)4 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 CountOptions (com.mongodb.client.model.CountOptions)2 FindOneAndReplaceOptions (com.mongodb.client.model.FindOneAndReplaceOptions)2 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)2 ReplaceOptions (com.mongodb.client.model.ReplaceOptions)2 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 Bson (org.bson.conversions.Bson)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 CustomConversions (org.springframework.data.convert.CustomConversions)2 MongoNamespace (com.mongodb.MongoNamespace)1 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)1 FindOneAndDeleteOptions (com.mongodb.client.model.FindOneAndDeleteOptions)1 DateToOffsetDateTimeConverter (de.spring.example.persistence.converters.DateToOffsetDateTimeConverter)1