|
| 1 | +/* |
| 2 | + * Copyright 2021 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; |
| 17 | + |
| 18 | +import java.util.regex.Matcher; |
| 19 | +import java.util.regex.Pattern; |
| 20 | + |
| 21 | +import org.springframework.core.convert.support.GenericConversionService; |
| 22 | +import org.springframework.data.elasticsearch.core.convert.DateTimeConverters; |
| 23 | +import org.springframework.data.elasticsearch.repository.query.ElasticsearchStringQuery; |
| 24 | +import org.springframework.data.repository.query.ParameterAccessor; |
| 25 | +import org.springframework.util.ClassUtils; |
| 26 | +import org.springframework.util.NumberUtils; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Peter-Josef Meisch |
| 30 | + */ |
| 31 | +final public class StringQueryUtil { |
| 32 | + |
| 33 | + private static final Pattern PARAMETER_PLACEHOLDER = Pattern.compile("\\?(\\d+)"); |
| 34 | + private static final GenericConversionService conversionService = new GenericConversionService(); |
| 35 | + |
| 36 | + { |
| 37 | + if (!conversionService.canConvert(java.util.Date.class, String.class)) { |
| 38 | + conversionService.addConverter(DateTimeConverters.JavaDateConverter.INSTANCE); |
| 39 | + } |
| 40 | + if (ClassUtils.isPresent("org.joda.time.DateTimeZone", ElasticsearchStringQuery.class.getClassLoader())) { |
| 41 | + if (!conversionService.canConvert(org.joda.time.ReadableInstant.class, String.class)) { |
| 42 | + conversionService.addConverter(DateTimeConverters.JodaDateTimeConverter.INSTANCE); |
| 43 | + } |
| 44 | + if (!conversionService.canConvert(org.joda.time.LocalDateTime.class, String.class)) { |
| 45 | + conversionService.addConverter(DateTimeConverters.JodaLocalDateTimeConverter.INSTANCE); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private StringQueryUtil() {} |
| 51 | + |
| 52 | + public static String replacePlaceholders(String input, ParameterAccessor accessor) { |
| 53 | + |
| 54 | + Matcher matcher = PARAMETER_PLACEHOLDER.matcher(input); |
| 55 | + String result = input; |
| 56 | + while (matcher.find()) { |
| 57 | + |
| 58 | + String placeholder = Pattern.quote(matcher.group()) + "(?!\\d+)"; |
| 59 | + int index = NumberUtils.parseNumber(matcher.group(1), Integer.class); |
| 60 | + result = result.replaceAll(placeholder, Matcher.quoteReplacement(getParameterWithIndex(accessor, index))); |
| 61 | + } |
| 62 | + return result; |
| 63 | + } |
| 64 | + |
| 65 | + private static String getParameterWithIndex(ParameterAccessor accessor, int index) { |
| 66 | + |
| 67 | + Object parameter = accessor.getBindableValue(index); |
| 68 | + String parameterValue = "null"; |
| 69 | + |
| 70 | + // noinspection ConstantConditions |
| 71 | + if (parameter != null) { |
| 72 | + |
| 73 | + if (conversionService.canConvert(parameter.getClass(), String.class)) { |
| 74 | + String converted = conversionService.convert(parameter, String.class); |
| 75 | + |
| 76 | + if (converted != null) { |
| 77 | + parameterValue = converted; |
| 78 | + } |
| 79 | + } else { |
| 80 | + parameterValue = parameter.toString(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + parameterValue = parameterValue.replaceAll("\"", Matcher.quoteReplacement("\\\"")); |
| 85 | + return parameterValue; |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments