Search in sources :

Example 1 with PropertyValueTransformer

use of org.springframework.data.domain.ExampleMatcher.PropertyValueTransformer in project spring-data-mongodb by spring-projects.

the class MongoExampleMapper method applyPropertySpecs.

private void applyPropertySpecs(String path, Document source, Class<?> probeType, ExampleMatcherAccessor exampleSpecAccessor) {
    if (source == null) {
        return;
    }
    Iterator<Map.Entry<String, Object>> iter = source.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry<String, Object> entry = iter.next();
        String propertyPath = StringUtils.hasText(path) ? path + "." + entry.getKey() : entry.getKey();
        String mappedPropertyPath = getMappedPropertyPath(propertyPath, probeType);
        if (isEmptyIdProperty(entry)) {
            iter.remove();
            continue;
        }
        if (exampleSpecAccessor.isIgnoredPath(propertyPath) || exampleSpecAccessor.isIgnoredPath(mappedPropertyPath)) {
            iter.remove();
            continue;
        }
        StringMatcher stringMatcher = exampleSpecAccessor.getDefaultStringMatcher();
        Object value = entry.getValue();
        boolean ignoreCase = exampleSpecAccessor.isIgnoreCaseEnabled();
        if (exampleSpecAccessor.hasPropertySpecifiers()) {
            mappedPropertyPath = exampleSpecAccessor.hasPropertySpecifier(propertyPath) ? propertyPath : getMappedPropertyPath(propertyPath, probeType);
            stringMatcher = exampleSpecAccessor.getStringMatcherForPath(mappedPropertyPath);
            ignoreCase = exampleSpecAccessor.isIgnoreCaseForPath(mappedPropertyPath);
        }
        // TODO: should a PropertySpecifier outrule the later on string matching?
        if (exampleSpecAccessor.hasPropertySpecifier(mappedPropertyPath)) {
            PropertyValueTransformer valueTransformer = exampleSpecAccessor.getValueTransformerForPath(mappedPropertyPath);
            value = valueTransformer.convert(value);
            if (value == null) {
                iter.remove();
                continue;
            }
            entry.setValue(value);
        }
        if (entry.getValue() instanceof String) {
            applyStringMatcher(entry, stringMatcher, ignoreCase);
        } else if (entry.getValue() instanceof Document) {
            applyPropertySpecs(propertyPath, (Document) entry.getValue(), probeType, exampleSpecAccessor);
        }
    }
}
Also used : Entry(java.util.Map.Entry) PropertyValueTransformer(org.springframework.data.domain.ExampleMatcher.PropertyValueTransformer) StringMatcher(org.springframework.data.domain.ExampleMatcher.StringMatcher) Document(org.bson.Document) Map(java.util.Map)

Example 2 with PropertyValueTransformer

use of org.springframework.data.domain.ExampleMatcher.PropertyValueTransformer in project spring-data-commons by spring-projects.

the class ExampleSpecificationAccessorUnitTests method getValueTransformerForPathReturnsConfigurtedTransformerForPath.

// DATACMNS-810
@Test
public void getValueTransformerForPathReturnsConfigurtedTransformerForPath() {
    PropertyValueTransformer transformer = source -> source.map(Object::toString);
    specification = // 
    ExampleMatcher.matching().withTransformer("firstname", transformer);
    exampleSpecificationAccessor = new ExampleMatcherAccessor(specification);
    assertThat(exampleSpecificationAccessor.getValueTransformerForPath("firstname")).isEqualTo(transformer);
}
Also used : NullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler) StringMatcher(org.springframework.data.domain.ExampleMatcher.StringMatcher) NoOpPropertyValueTransformer(org.springframework.data.domain.ExampleMatcher.NoOpPropertyValueTransformer) PropertyValueTransformer(org.springframework.data.domain.ExampleMatcher.PropertyValueTransformer) GenericPropertyMatchers(org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers) GenericPropertyMatcher(org.springframework.data.domain.ExampleMatcher.GenericPropertyMatcher) Assertions(org.assertj.core.api.Assertions) Test(org.junit.Test) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Before(org.junit.Before) ExampleMatcherAccessor(org.springframework.data.support.ExampleMatcherAccessor) NoOpPropertyValueTransformer(org.springframework.data.domain.ExampleMatcher.NoOpPropertyValueTransformer) PropertyValueTransformer(org.springframework.data.domain.ExampleMatcher.PropertyValueTransformer) ExampleMatcherAccessor(org.springframework.data.support.ExampleMatcherAccessor) Test(org.junit.Test)

Aggregations

PropertyValueTransformer (org.springframework.data.domain.ExampleMatcher.PropertyValueTransformer)2 StringMatcher (org.springframework.data.domain.ExampleMatcher.StringMatcher)2 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Assertions (org.assertj.core.api.Assertions)1 Document (org.bson.Document)1 Before (org.junit.Before)1 Test (org.junit.Test)1 ExampleMatcher (org.springframework.data.domain.ExampleMatcher)1 GenericPropertyMatcher (org.springframework.data.domain.ExampleMatcher.GenericPropertyMatcher)1 GenericPropertyMatchers (org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers)1 NoOpPropertyValueTransformer (org.springframework.data.domain.ExampleMatcher.NoOpPropertyValueTransformer)1 NullHandler (org.springframework.data.domain.ExampleMatcher.NullHandler)1 ExampleMatcherAccessor (org.springframework.data.support.ExampleMatcherAccessor)1