Search in sources :

Example 1 with SpELExpressionEvaluator

use of org.springframework.data.mapping.model.SpELExpressionEvaluator 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 SpELExpressionEvaluator

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

the class MappingMongoConverter method doReadProjection.

@SuppressWarnings("unchecked")
private <R> R doReadProjection(ConversionContext context, Bson bson, EntityProjection<R, ?> projection) {
    MongoPersistentEntity<?> entity = getMappingContext().getRequiredPersistentEntity(projection.getActualDomainType());
    TypeInformation<?> mappedType = projection.getActualMappedType();
    MongoPersistentEntity<R> mappedEntity = (MongoPersistentEntity<R>) getMappingContext().getPersistentEntity(mappedType);
    SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(bson, spELContext);
    boolean isInterfaceProjection = mappedType.getType().isInterface();
    if (isInterfaceProjection) {
        PersistentPropertyTranslator propertyTranslator = PersistentPropertyTranslator.create(mappedEntity);
        DocumentAccessor documentAccessor = new DocumentAccessor(bson);
        PersistentPropertyAccessor<?> accessor = new MapPersistentPropertyAccessor();
        PersistentPropertyAccessor<?> convertingAccessor = PropertyTranslatingPropertyAccessor.create(new ConvertingPropertyAccessor<>(accessor, conversionService), propertyTranslator);
        MongoDbPropertyValueProvider valueProvider = new MongoDbPropertyValueProvider(context, documentAccessor, evaluator);
        readProperties(context, entity, convertingAccessor, documentAccessor, valueProvider, evaluator, Predicates.isTrue());
        return (R) projectionFactory.createProjection(mappedType.getType(), accessor.getBean());
    }
    // DTO projection
    if (mappedEntity == null) {
        throw new MappingException(String.format("No mapping metadata found for %s", mappedType.getType().getName()));
    }
    // create target instance, merge metadata from underlying DTO type
    PersistentPropertyTranslator propertyTranslator = PersistentPropertyTranslator.create(entity, Predicates.negate(MongoPersistentProperty::hasExplicitFieldName));
    DocumentAccessor documentAccessor = new DocumentAccessor(bson) {

        @Override
        String getFieldName(MongoPersistentProperty prop) {
            return propertyTranslator.translate(prop).getFieldName();
        }
    };
    PreferredConstructor<?, MongoPersistentProperty> persistenceConstructor = mappedEntity.getPersistenceConstructor();
    ParameterValueProvider<MongoPersistentProperty> provider = persistenceConstructor != null && persistenceConstructor.hasParameters() ? getParameterProvider(context, mappedEntity, documentAccessor, evaluator) : NoOpParameterValueProvider.INSTANCE;
    EntityInstantiator instantiator = instantiators.getInstantiatorFor(mappedEntity);
    R instance = instantiator.createInstance(mappedEntity, provider);
    PersistentPropertyAccessor<R> accessor = mappedEntity.getPropertyAccessor(instance);
    populateProperties(context, mappedEntity, documentAccessor, evaluator, instance);
    PersistentPropertyAccessor<?> convertingAccessor = new ConvertingPropertyAccessor<>(accessor, conversionService);
    MongoDbPropertyValueProvider valueProvider = new MongoDbPropertyValueProvider(context, documentAccessor, evaluator);
    readProperties(context, mappedEntity, convertingAccessor, documentAccessor, valueProvider, evaluator, Predicates.isTrue());
    return accessor.getBean();
}
Also used : SpELExpressionEvaluator(org.springframework.data.mapping.model.SpELExpressionEvaluator) DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) BasicMongoPersistentProperty(org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty) EntityInstantiator(org.springframework.data.mapping.model.EntityInstantiator) DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) PersistentPropertyTranslator(org.springframework.data.mongodb.core.mapping.PersistentPropertyTranslator)

Example 3 with SpELExpressionEvaluator

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

the class AbstractMongoQuery method prepareBindingContext.

/**
 * Create the {@link ParameterBindingContext binding context} used for SpEL evaluation.
 *
 * @param source the JSON source.
 * @param accessor value provider for parameter binding.
 * @return never {@literal null}.
 * @since 3.4
 */
protected ParameterBindingContext prepareBindingContext(String source, ConvertingParameterAccessor accessor) {
    ExpressionDependencies dependencies = getParameterBindingCodec().captureExpressionDependencies(source, accessor::getBindableValue, expressionParser);
    SpELExpressionEvaluator evaluator = getSpELExpressionEvaluatorFor(dependencies, accessor);
    return new ParameterBindingContext(accessor::getBindableValue, evaluator);
}
Also used : SpELExpressionEvaluator(org.springframework.data.mapping.model.SpELExpressionEvaluator) ExpressionDependencies(org.springframework.data.spel.ExpressionDependencies) ParameterBindingContext(org.springframework.data.mongodb.util.json.ParameterBindingContext)

Example 4 with SpELExpressionEvaluator

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

the class MappingMongoConverter method read.

private <S> S read(ConversionContext context, MongoPersistentEntity<S> entity, Document bson) {
    SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(bson, spELContext);
    DocumentAccessor documentAccessor = new DocumentAccessor(bson);
    if (hasIdentifier(bson)) {
        S existing = findContextualEntity(context, entity, bson);
        if (existing != null) {
            return existing;
        }
    }
    PreferredConstructor<S, MongoPersistentProperty> persistenceConstructor = entity.getPersistenceConstructor();
    ParameterValueProvider<MongoPersistentProperty> provider = persistenceConstructor != null && persistenceConstructor.hasParameters() ? getParameterProvider(context, entity, documentAccessor, evaluator) : NoOpParameterValueProvider.INSTANCE;
    EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
    S instance = instantiator.createInstance(entity, provider);
    if (entity.requiresPropertyPopulation()) {
        return populateProperties(context, entity, documentAccessor, evaluator, instance);
    }
    return instance;
}
Also used : DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) SpELExpressionEvaluator(org.springframework.data.mapping.model.SpELExpressionEvaluator) DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) BasicMongoPersistentProperty(org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty) EntityInstantiator(org.springframework.data.mapping.model.EntityInstantiator)

Aggregations

SpELExpressionEvaluator (org.springframework.data.mapping.model.SpELExpressionEvaluator)4 DefaultSpELExpressionEvaluator (org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator)3 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)3 EntityInstantiator (org.springframework.data.mapping.model.EntityInstantiator)2 BasicMongoPersistentProperty (org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty)2 Document (org.bson.Document)1 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)1 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)1 MongoPersistentEntity (org.springframework.data.mongodb.core.mapping.MongoPersistentEntity)1 PersistentPropertyTranslator (org.springframework.data.mongodb.core.mapping.PersistentPropertyTranslator)1 ParameterBindingContext (org.springframework.data.mongodb.util.json.ParameterBindingContext)1 ExpressionDependencies (org.springframework.data.spel.ExpressionDependencies)1