use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-mongodb by spring-projects.
the class MongoTemplate method doSaveVersioned.
private <T> T doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity, String collectionName) {
ConvertingPropertyAccessor convertingAccessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(objectToSave), mongoConverter.getConversionService());
MongoPersistentProperty property = entity.getRequiredVersionProperty();
Number number = convertingAccessor.getProperty(property, Number.class);
if (number != null) {
// Bump version number
convertingAccessor.setProperty(property, number.longValue() + 1);
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
assertUpdateableIdIfNotSet(objectToSave);
Document document = new Document();
this.mongoConverter.write(objectToSave, document);
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, document, collectionName));
Update update = Update.fromDocument(document, ID_FIELD);
// Create query for entity with the id and old version
MongoPersistentProperty idProperty = entity.getRequiredIdProperty();
Object id = entity.getIdentifierAccessor(objectToSave).getRequiredIdentifier();
Query query = new Query(Criteria.where(idProperty.getName()).is(id).and(property.getName()).is(number));
UpdateResult result = doUpdate(collectionName, query, update, objectToSave.getClass(), false, false);
if (result.getModifiedCount() == 0) {
throw new OptimisticLockingFailureException(String.format("Cannot save entity %s with version %s to collection %s. Has it been modified meanwhile?", id, number, collectionName));
}
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, document, collectionName));
return objectToSave;
}
doInsert(collectionName, objectToSave, this.mongoConverter);
return objectToSave;
}
use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-mongodb by spring-projects.
the class MappingMongoConverter method read.
@Nullable
private <S extends Object> S read(final MongoPersistentEntity<S> entity, final Document bson, final ObjectPath path) {
DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(bson, spELContext);
ParameterValueProvider<MongoPersistentProperty> provider = getParameterProvider(entity, bson, evaluator, path);
EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
S instance = instantiator.createInstance(entity, provider);
PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(instance), conversionService);
MongoPersistentProperty idProperty = entity.getIdProperty();
DocumentAccessor documentAccessor = new DocumentAccessor(bson);
// make sure id property is set before all other properties
Object idValue = null;
if (idProperty != null && documentAccessor.hasValue(idProperty)) {
idValue = readIdValue(path, evaluator, idProperty, documentAccessor);
accessor.setProperty(idProperty, idValue);
}
ObjectPath currentPath = path.push(instance, entity, idValue != null ? bson.get(idProperty.getFieldName()) : null);
MongoDbPropertyValueProvider valueProvider = new MongoDbPropertyValueProvider(documentAccessor, evaluator, currentPath);
DbRefResolverCallback callback = new DefaultDbRefResolverCallback(bson, currentPath, evaluator, MappingMongoConverter.this);
readProperties(entity, accessor, idProperty, documentAccessor, valueProvider, callback);
return instance;
}
use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-jdbc by spring-projects.
the class EntityRowMapper method readEntityFrom.
private <S> S readEntityFrom(ResultSet rs, PersistentProperty<?> property) {
String prefix = property.getName() + "_";
@SuppressWarnings("unchecked") JdbcPersistentEntity<S> entity = (JdbcPersistentEntity<S>) context.getRequiredPersistentEntity(property.getActualType());
if (readFrom(rs, entity.getRequiredIdProperty(), prefix) == null) {
return null;
}
S instance = instantiator.createInstance(entity, new ResultSetParameterValueProvider(rs, entity, conversions, prefix));
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(instance);
ConvertingPropertyAccessor propertyAccessor = new ConvertingPropertyAccessor(accessor, conversions);
for (JdbcPersistentProperty p : entity) {
propertyAccessor.setProperty(p, readFrom(rs, p, prefix));
}
return instance;
}
use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-jdbc by spring-projects.
the class EntityRowMapper method mapRow.
/*
* (non-Javadoc)
* @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
*/
@Override
public T mapRow(ResultSet resultSet, int rowNumber) throws SQLException {
T result = createInstance(resultSet);
ConvertingPropertyAccessor propertyAccessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(result), conversions);
Object id = idProperty == null ? null : readFrom(resultSet, idProperty, "");
for (JdbcPersistentProperty property : entity) {
if (property.isCollectionLike()) {
propertyAccessor.setProperty(property, accessStrategy.findAllByProperty(id, property));
} else if (property.isMap()) {
Iterable<Object> allByProperty = accessStrategy.findAllByProperty(id, property);
propertyAccessor.setProperty(property, ITERABLE_OF_ENTRY_TO_MAP_CONVERTER.convert(allByProperty));
} else {
propertyAccessor.setProperty(property, readFrom(resultSet, property, ""));
}
}
return result;
}
use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-mongodb by spring-projects.
the class MongoTemplate method populateIdIfNecessary.
/**
* Populates the id property of the saved object, if it's not set already.
*
* @param savedObject
* @param id
*/
protected void populateIdIfNecessary(Object savedObject, Object id) {
if (id == null) {
return;
}
if (savedObject instanceof Document) {
Document document = (Document) savedObject;
document.put(ID_FIELD, id);
return;
}
MongoPersistentProperty idProperty = getIdPropertyFor(savedObject.getClass());
if (idProperty != null) {
ConversionService conversionService = mongoConverter.getConversionService();
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(savedObject.getClass());
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(savedObject);
Object value = accessor.getProperty(idProperty);
if (value == null) {
new ConvertingPropertyAccessor(accessor, conversionService).setProperty(idProperty, id);
}
}
}
Aggregations