|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.elasticsearch.repository.support.querybyexample; |
| 17 | + |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +import org.springframework.dao.InvalidDataAccessApiUsageException; |
| 22 | +import org.springframework.data.domain.Example; |
| 23 | +import org.springframework.data.domain.ExampleMatcher; |
| 24 | +import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; |
| 25 | +import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; |
| 26 | +import org.springframework.data.elasticsearch.core.query.Criteria; |
| 27 | +import org.springframework.data.mapping.PersistentPropertyAccessor; |
| 28 | +import org.springframework.data.mapping.context.MappingContext; |
| 29 | +import org.springframework.data.support.ExampleMatcherAccessor; |
| 30 | +import org.springframework.lang.Nullable; |
| 31 | +import org.springframework.util.StringUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Maps a {@link Example} to a {@link org.springframework.data.elasticsearch.core.query.Criteria} |
| 35 | + * |
| 36 | + * @author Ezequiel Antúnez Camacho |
| 37 | + * @since 5.1 |
| 38 | + */ |
| 39 | +class ExampleCriteriaMapper { |
| 40 | + |
| 41 | + private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext; |
| 42 | + |
| 43 | + /** |
| 44 | + * Builds a {@link ExampleCriteriaMapper} |
| 45 | + * |
| 46 | + * @param mappingContext mappingContext to use |
| 47 | + */ |
| 48 | + ExampleCriteriaMapper( |
| 49 | + MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) { |
| 50 | + this.mappingContext = mappingContext; |
| 51 | + } |
| 52 | + |
| 53 | + <S> Criteria criteria(Example<S> example) { |
| 54 | + return buildCriteria(example); |
| 55 | + } |
| 56 | + |
| 57 | + private <S> Criteria buildCriteria(Example<S> example) { |
| 58 | + final ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); |
| 59 | + |
| 60 | + return applyPropertySpecs(new Criteria(), "", example.getProbe(), |
| 61 | + mappingContext.getRequiredPersistentEntity(example.getProbeType()), matcherAccessor, |
| 62 | + example.getMatcher().getMatchMode()); |
| 63 | + } |
| 64 | + |
| 65 | + private Criteria applyPropertySpecs(Criteria criteria, String path, @Nullable Object probe, |
| 66 | + ElasticsearchPersistentEntity<?> persistentEntity, ExampleMatcherAccessor exampleSpecAccessor, |
| 67 | + ExampleMatcher.MatchMode matchMode) { |
| 68 | + |
| 69 | + if (probe == null) { |
| 70 | + return criteria; |
| 71 | + } |
| 72 | + |
| 73 | + PersistentPropertyAccessor<?> propertyAccessor = persistentEntity.getPropertyAccessor(probe); |
| 74 | + |
| 75 | + for (ElasticsearchPersistentProperty property : persistentEntity) { |
| 76 | + final String propertyName = property.getName(); |
| 77 | + String propertyPath = StringUtils.hasText(path) ? (path + "." + propertyName) : propertyName; |
| 78 | + if (exampleSpecAccessor.isIgnoredPath(propertyPath) || property.isCollectionLike() |
| 79 | + || property.isVersionProperty()) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + Object propertyValue = propertyAccessor.getProperty(property); |
| 84 | + if (property.isMap() && propertyValue != null) { |
| 85 | + for (Map.Entry<String, Object> entry : ((Map<String, Object>) propertyValue).entrySet()) { |
| 86 | + String key = entry.getKey(); |
| 87 | + Object value = entry.getValue(); |
| 88 | + criteria = applyPropertySpec(propertyPath + "." + key, value, exampleSpecAccessor, property, matchMode, |
| 89 | + criteria); |
| 90 | + } |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + criteria = applyPropertySpec(propertyPath, propertyValue, exampleSpecAccessor, property, matchMode, criteria); |
| 95 | + } |
| 96 | + return criteria; |
| 97 | + } |
| 98 | + |
| 99 | + private Criteria applyPropertySpec(String path, Object propertyValue, ExampleMatcherAccessor exampleSpecAccessor, |
| 100 | + ElasticsearchPersistentProperty property, ExampleMatcher.MatchMode matchMode, Criteria criteria) { |
| 101 | + |
| 102 | + if (exampleSpecAccessor.isIgnoreCaseForPath(path)) { |
| 103 | + throw new InvalidDataAccessApiUsageException( |
| 104 | + "Current implementation of Query-by-Example supports only case-sensitive matching."); |
| 105 | + } |
| 106 | + |
| 107 | + final Object transformedValue = exampleSpecAccessor.getValueTransformerForPath(path) |
| 108 | + .apply(Optional.ofNullable(propertyValue)).orElse(null); |
| 109 | + |
| 110 | + if (transformedValue == null) { |
| 111 | + criteria = tryToAppendMustNotSentence(criteria, path, exampleSpecAccessor); |
| 112 | + } else { |
| 113 | + if (property.isEntity()) { |
| 114 | + return applyPropertySpecs(criteria, path, transformedValue, |
| 115 | + mappingContext.getRequiredPersistentEntity(property), exampleSpecAccessor, matchMode); |
| 116 | + } else { |
| 117 | + return applyStringMatcher(applyMatchMode(criteria, path, matchMode), transformedValue, |
| 118 | + exampleSpecAccessor.getStringMatcherForPath(path)); |
| 119 | + } |
| 120 | + } |
| 121 | + return criteria; |
| 122 | + } |
| 123 | + |
| 124 | + private Criteria tryToAppendMustNotSentence(Criteria criteria, String path, |
| 125 | + ExampleMatcherAccessor exampleSpecAccessor) { |
| 126 | + if (ExampleMatcher.NullHandler.INCLUDE.equals(exampleSpecAccessor.getNullHandler()) |
| 127 | + || exampleSpecAccessor.hasPropertySpecifier(path)) { |
| 128 | + return criteria.and(path).not().exists(); |
| 129 | + } |
| 130 | + return criteria; |
| 131 | + } |
| 132 | + |
| 133 | + private Criteria applyMatchMode(Criteria criteria, String path, ExampleMatcher.MatchMode matchMode) { |
| 134 | + if (matchMode == ExampleMatcher.MatchMode.ALL) { |
| 135 | + return criteria.and(path); |
| 136 | + } else { |
| 137 | + return criteria.or(path); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private Criteria applyStringMatcher(Criteria criteria, Object value, ExampleMatcher.StringMatcher stringMatcher) { |
| 142 | + return switch (stringMatcher) { |
| 143 | + case DEFAULT -> criteria.is(value); |
| 144 | + case EXACT -> criteria.matchesAll(value); |
| 145 | + case STARTING -> criteria.startsWith(validateString(value)); |
| 146 | + case ENDING -> criteria.endsWith(validateString(value)); |
| 147 | + case CONTAINING -> criteria.contains(validateString(value)); |
| 148 | + case REGEX -> criteria.regexp(validateString(value)); |
| 149 | + }; |
| 150 | + } |
| 151 | + |
| 152 | + private String validateString(Object value) { |
| 153 | + if (value instanceof String) { |
| 154 | + return value.toString(); |
| 155 | + } |
| 156 | + throw new IllegalArgumentException("This operation requires a String but got " + value.getClass()); |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments