Search in sources :

Example 1 with PersistentPropertyTranslator

use of org.springframework.data.mongodb.core.mapping.PersistentPropertyTranslator in project spring-data-mongodb by spring-projects.

the class PropertyOperations method computeMappedFieldsForProjection.

/**
 * For cases where {@code fields} is {@link Document#isEmpty() empty} include only fields that are required for
 * creating the projection (target) type if the {@code EntityProjection} is a {@literal DTO projection} or a
 * {@literal closed interface projection}.
 *
 * @param projection must not be {@literal null}.
 * @param fields must not be {@literal null}.
 * @return {@link Document} with fields to be included.
 */
Document computeMappedFieldsForProjection(EntityProjection<?, ?> projection, Document fields) {
    if (!projection.isClosedProjection()) {
        return fields;
    }
    Document projectedFields = new Document();
    if (projection.getMappedType().getType().isInterface()) {
        projection.forEach(it -> {
            projectedFields.put(it.getPropertyPath().getSegment(), 1);
        });
    } else {
        // DTO projections use merged metadata between domain type and result type
        PersistentPropertyTranslator translator = PersistentPropertyTranslator.create(mappingContext.getRequiredPersistentEntity(projection.getDomainType()), Predicates.negate(MongoPersistentProperty::hasExplicitFieldName));
        MongoPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(projection.getMappedType());
        for (MongoPersistentProperty property : persistentEntity) {
            projectedFields.put(translator.translate(property).getFieldName(), 1);
        }
    }
    return projectedFields;
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Document(org.bson.Document) PersistentPropertyTranslator(org.springframework.data.mongodb.core.mapping.PersistentPropertyTranslator)

Example 2 with PersistentPropertyTranslator

use of org.springframework.data.mongodb.core.mapping.PersistentPropertyTranslator 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)

Aggregations

MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)2 PersistentPropertyTranslator (org.springframework.data.mongodb.core.mapping.PersistentPropertyTranslator)2 Document (org.bson.Document)1 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)1 DefaultSpELExpressionEvaluator (org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator)1 EntityInstantiator (org.springframework.data.mapping.model.EntityInstantiator)1 SpELExpressionEvaluator (org.springframework.data.mapping.model.SpELExpressionEvaluator)1 BasicMongoPersistentProperty (org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty)1 MongoPersistentEntity (org.springframework.data.mongodb.core.mapping.MongoPersistentEntity)1