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);
}
}
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);
}
}
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;
});
});
}
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);
}
Aggregations