1 | /* |
2 | * Copyright 2013 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 | * http://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.query; |
17 | |
18 | |
19 | import org.springframework.core.convert.support.GenericConversionService; |
20 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
21 | import org.springframework.data.elasticsearch.core.convert.DateTimeConverters; |
22 | import org.springframework.data.elasticsearch.core.query.StringQuery; |
23 | import org.springframework.data.repository.query.ParametersParameterAccessor; |
24 | import org.springframework.util.Assert; |
25 | |
26 | import java.util.regex.Matcher; |
27 | import java.util.regex.Pattern; |
28 | |
29 | /** |
30 | * ElasticsearchStringQuery |
31 | * |
32 | * @author Rizwan Idrees |
33 | * @author Mohsin Husen |
34 | */ |
35 | public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQuery{ |
36 | |
37 | private static final Pattern PARAMETER_PLACEHOLDER = Pattern.compile("\\?(\\d+)"); |
38 | private String query; |
39 | |
40 | private final GenericConversionService conversionService = new GenericConversionService(); |
41 | |
42 | { |
43 | if (!conversionService.canConvert(java.util.Date.class, String.class)) { |
44 | conversionService.addConverter(DateTimeConverters.JavaDateConverter.INSTANCE); |
45 | } |
46 | if (!conversionService.canConvert(org.joda.time.ReadableInstant.class, String.class)) { |
47 | conversionService.addConverter(DateTimeConverters.JodaDateTimeConverter.INSTANCE); |
48 | } |
49 | if (!conversionService.canConvert(org.joda.time.LocalDateTime.class, String.class)) { |
50 | conversionService.addConverter(DateTimeConverters.JodaLocalDateTimeConverter.INSTANCE); |
51 | } |
52 | |
53 | } |
54 | |
55 | public ElasticsearchStringQuery(ElasticsearchQueryMethod queryMethod, ElasticsearchOperations elasticsearchOperations, String query) { |
56 | super(queryMethod, elasticsearchOperations); |
57 | Assert.notNull(query, "Query cannot be empty"); |
58 | this.query = query; |
59 | } |
60 | |
61 | @Override |
62 | public Object execute(Object[] parameters) { |
63 | ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters); |
64 | StringQuery stringQuery = createQuery(accessor); |
65 | if(queryMethod.isPageQuery()){ |
66 | return elasticsearchOperations.queryForPage(stringQuery, queryMethod.getEntityInformation().getJavaType()); |
67 | } |
68 | return elasticsearchOperations.queryForObject(stringQuery, queryMethod.getEntityInformation().getJavaType()); |
69 | } |
70 | |
71 | |
72 | protected StringQuery createQuery(ParametersParameterAccessor parameterAccessor) { |
73 | String queryString = replacePlaceholders(this.query, parameterAccessor); |
74 | return new StringQuery(queryString); |
75 | } |
76 | |
77 | private String replacePlaceholders(String input, ParametersParameterAccessor accessor) { |
78 | Matcher matcher = PARAMETER_PLACEHOLDER.matcher(input); |
79 | String result = input; |
80 | while (matcher.find()) { |
81 | String group = matcher.group(); |
82 | int index = Integer.parseInt(matcher.group(1)); |
83 | result = result.replace(group, getParameterWithIndex(accessor, index)); |
84 | } |
85 | return result; |
86 | } |
87 | |
88 | private String getParameterWithIndex(ParametersParameterAccessor accessor, int index) { |
89 | Object parameter = accessor.getBindableValue(index); |
90 | if (parameter == null) { |
91 | return "null"; |
92 | } |
93 | if (conversionService.canConvert(parameter.getClass(), String.class)) { |
94 | return conversionService.convert(parameter, String.class); |
95 | } |
96 | return parameter.toString(); |
97 | } |
98 | } |