Search in sources :

Example 6 with ConvertingPropertyAccessor

use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-mongodb by spring-projects.

the class MongoTemplate method initializeVersionProperty.

private void initializeVersionProperty(Object entity) {
    MongoPersistentEntity<?> persistentEntity = getPersistentEntity(entity.getClass());
    if (persistentEntity != null && persistentEntity.hasVersionProperty()) {
        MongoPersistentProperty versionProperty = persistentEntity.getRequiredVersionProperty();
        ConvertingPropertyAccessor accessor = new ConvertingPropertyAccessor(persistentEntity.getPropertyAccessor(entity), mongoConverter.getConversionService());
        accessor.setProperty(versionProperty, 0);
    }
}
Also used : ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)

Example 7 with ConvertingPropertyAccessor

use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method initializeVersionProperty.

private void initializeVersionProperty(Object entity) {
    MongoPersistentEntity<?> mongoPersistentEntity = getPersistentEntity(entity.getClass());
    if (mongoPersistentEntity != null && mongoPersistentEntity.hasVersionProperty()) {
        ConvertingPropertyAccessor accessor = new ConvertingPropertyAccessor(mongoPersistentEntity.getPropertyAccessor(entity), mongoConverter.getConversionService());
        accessor.setProperty(mongoPersistentEntity.getRequiredVersionProperty(), 0);
    }
}
Also used : ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor)

Example 8 with ConvertingPropertyAccessor

use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method doSaveVersioned.

private <T> Mono<T> doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity, String collectionName) {
    return createMono(collectionName, collection -> {
        ConvertingPropertyAccessor convertingAccessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(objectToSave), mongoConverter.getConversionService());
        MongoPersistentProperty idProperty = entity.getRequiredIdProperty();
        MongoPersistentProperty versionProperty = entity.getRequiredVersionProperty();
        Object version = convertingAccessor.getProperty(versionProperty);
        Number versionNumber = convertingAccessor.getProperty(versionProperty, Number.class);
        // Fresh instance -> initialize version property
        if (version == null) {
            return doInsert(collectionName, objectToSave, mongoConverter);
        }
        assertUpdateableIdIfNotSet(objectToSave);
        // Create query for entity with the id and old version
        Object id = convertingAccessor.getProperty(idProperty);
        Query query = new Query(Criteria.where(idProperty.getName()).is(id).and(versionProperty.getName()).is(version));
        if (versionNumber == null) {
            versionNumber = 0;
        }
        // Bump version number
        convertingAccessor.setProperty(versionProperty, versionNumber.longValue() + 1);
        ReactiveMongoTemplate.this.maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
        Document document = ReactiveMongoTemplate.this.toDbObject(objectToSave, mongoConverter);
        ReactiveMongoTemplate.this.maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, document, collectionName));
        Update update = Update.fromDocument(document, ID_FIELD);
        return doUpdate(collectionName, query, update, objectToSave.getClass(), false, false).map(updateResult -> {
            maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, document, collectionName));
            return objectToSave;
        });
    });
}
Also used : ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) Update(org.springframework.data.mongodb.core.query.Update)

Example 9 with ConvertingPropertyAccessor

use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method populateIdIfNecessary.

/**
 * Populates the id property of the saved object, if it's not set already.
 *
 * @param savedObject
 * @param id
 */
private void populateIdIfNecessary(Object savedObject, @Nullable Object id) {
    if (id == null) {
        return;
    }
    if (savedObject instanceof Document) {
        Document Document = (Document) savedObject;
        Document.put(ID_FIELD, id);
        return;
    }
    MongoPersistentProperty idProp = getIdPropertyFor(savedObject.getClass());
    if (idProp == null) {
        return;
    }
    ConversionService conversionService = mongoConverter.getConversionService();
    MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(savedObject.getClass());
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(savedObject);
    if (accessor.getProperty(idProp) != null) {
        return;
    }
    new ConvertingPropertyAccessor(accessor, conversionService).setProperty(idProp, id);
}
Also used : ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ConversionService(org.springframework.core.convert.ConversionService) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument)

Aggregations

ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)9 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)6 Document (org.bson.Document)4 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)4 ReturnDocument (com.mongodb.client.model.ReturnDocument)2 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 ConversionService (org.springframework.core.convert.ConversionService)2 JdbcPersistentProperty (org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty)2 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)2 Query (org.springframework.data.mongodb.core.query.Query)2 Update (org.springframework.data.mongodb.core.query.Update)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 UpdateResult (com.mongodb.client.result.UpdateResult)1 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)1 EntityInstantiator (org.springframework.data.convert.EntityInstantiator)1 JdbcPersistentEntity (org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity)1 DefaultSpELExpressionEvaluator (org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator)1 Nullable (org.springframework.lang.Nullable)1