Search in sources :

Example 1 with ExampleMatcherAccessor

use of org.springframework.data.support.ExampleMatcherAccessor in project spring-data-mongodb by spring-projects.

the class MongoExampleMapper method getMappedExample.

/**
 * Returns the given {@link Example} as {@link Document} holding matching values extracted from
 * {@link Example#getProbe()}.
 *
 * @param example must not be {@literal null}.
 * @param entity must not be {@literal null}.
 * @return
 */
public Document getMappedExample(Example<?> example, MongoPersistentEntity<?> entity) {
    Assert.notNull(example, "Example must not be null!");
    Assert.notNull(entity, "MongoPersistentEntity must not be null!");
    Document reference = (Document) converter.convertToMongoType(example.getProbe());
    if (entity.getIdProperty() != null && ClassUtils.isAssignable(entity.getType(), example.getProbeType())) {
        Object identifier = entity.getIdentifierAccessor(example.getProbe()).getIdentifier();
        if (identifier == null) {
            reference.remove(entity.getIdProperty().getFieldName());
        }
    }
    ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher());
    applyPropertySpecs("", reference, example.getProbeType(), matcherAccessor);
    Document flattened = ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, matcherAccessor.getNullHandler()) ? reference : new Document(SerializationUtils.flattenMap(reference));
    Document result = example.getMatcher().isAllMatching() ? flattened : orConcatenate(flattened);
    return updateTypeRestrictions(result, example);
}
Also used : ExampleMatcherAccessor(org.springframework.data.support.ExampleMatcherAccessor) Document(org.bson.Document)

Aggregations

Document (org.bson.Document)1 ExampleMatcherAccessor (org.springframework.data.support.ExampleMatcherAccessor)1