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.data.elasticsearch.core.ElasticsearchOperations; |
20 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; |
21 | import org.springframework.data.elasticsearch.core.query.CriteriaQuery; |
22 | import org.springframework.data.elasticsearch.repository.query.parser.ElasticsearchQueryCreator; |
23 | import org.springframework.data.mapping.context.MappingContext; |
24 | import org.springframework.data.repository.query.ParametersParameterAccessor; |
25 | import org.springframework.data.repository.query.parser.PartTree; |
26 | |
27 | /** |
28 | * ElasticsearchPartQuery |
29 | * |
30 | * @author Rizwan Idrees |
31 | * @author Mohsin Husen |
32 | */ |
33 | public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery{ |
34 | |
35 | private final PartTree tree; |
36 | private final MappingContext<?, ElasticsearchPersistentProperty> mappingContext; |
37 | |
38 | |
39 | public ElasticsearchPartQuery(ElasticsearchQueryMethod method, ElasticsearchOperations elasticsearchOperations) { |
40 | super(method, elasticsearchOperations); |
41 | this.tree = new PartTree(method.getName(), method.getEntityInformation().getJavaType()); |
42 | this.mappingContext = elasticsearchOperations.getElasticsearchConverter().getMappingContext(); |
43 | } |
44 | |
45 | @Override |
46 | public Object execute(Object[] parameters) { |
47 | ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters); |
48 | CriteriaQuery query = createQuery(accessor); |
49 | if(queryMethod.isPageQuery()){ |
50 | return elasticsearchOperations.queryForPage(query, queryMethod.getEntityInformation().getJavaType()); |
51 | } |
52 | return elasticsearchOperations.queryForObject(query, queryMethod.getEntityInformation().getJavaType()); |
53 | } |
54 | |
55 | public CriteriaQuery createQuery(ParametersParameterAccessor accessor) { |
56 | return new ElasticsearchQueryCreator(tree, accessor, mappingContext).createQuery(); |
57 | } |
58 | } |