Search in sources :

Example 1 with DefaultSpELExpressionEvaluator

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

the class DefaultDbRefProxyHandler method populateId.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.convert.DbRefProxyHandler#populateId(com.mongodb.DBRef, java.lang.Object)
	 */
@Override
public Object populateId(MongoPersistentProperty property, @Nullable DBRef source, Object proxy) {
    if (source == null) {
        return proxy;
    }
    MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(property);
    MongoPersistentProperty idProperty = entity.getRequiredIdProperty();
    if (idProperty.usePropertyAccess()) {
        return proxy;
    }
    SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(proxy, spELContext);
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(proxy);
    Document object = new Document(idProperty.getFieldName(), source.getId());
    ObjectPath objectPath = ObjectPath.ROOT.push(proxy, entity, null);
    accessor.setProperty(idProperty, resolver.getValueInternal(idProperty, object, evaluator, objectPath));
    return proxy;
}
Also used : DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) SpELExpressionEvaluator(org.springframework.data.mapping.model.SpELExpressionEvaluator) DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) Document(org.bson.Document)

Example 2 with DefaultSpELExpressionEvaluator

use of org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator 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;
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) EntityInstantiator(org.springframework.data.convert.EntityInstantiator) DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Nullable(org.springframework.lang.Nullable)

Aggregations

PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)2 DefaultSpELExpressionEvaluator (org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator)2 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 Document (org.bson.Document)1 EntityInstantiator (org.springframework.data.convert.EntityInstantiator)1 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)1 SpELExpressionEvaluator (org.springframework.data.mapping.model.SpELExpressionEvaluator)1 Nullable (org.springframework.lang.Nullable)1