| 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 | import org.springframework.core.annotation.AnnotationUtils; |
| 19 | import org.springframework.data.elasticsearch.annotations.Query; |
| 20 | import org.springframework.data.elasticsearch.repository.support.ElasticsearchEntityInformation; |
| 21 | import org.springframework.data.elasticsearch.repository.support.ElasticsearchEntityInformationCreator; |
| 22 | import org.springframework.data.repository.core.RepositoryMetadata; |
| 23 | import org.springframework.data.repository.query.QueryMethod; |
| 24 | import org.springframework.util.StringUtils; |
| 25 | |
| 26 | import java.lang.reflect.Method; |
| 27 | |
| 28 | /** |
| 29 | * ElasticsearchQueryMethod |
| 30 | * |
| 31 | * @author Rizwan Idrees |
| 32 | * @author Mohsin Husen |
| 33 | */ |
| 34 | public class ElasticsearchQueryMethod extends QueryMethod { |
| 35 | |
| 36 | private final ElasticsearchEntityInformation<?, ?> entityInformation; |
| 37 | private Method method; |
| 38 | |
| 39 | public ElasticsearchQueryMethod(Method method, RepositoryMetadata metadata, ElasticsearchEntityInformationCreator elasticsearchEntityInformationCreator) { |
| 40 | super(method, metadata); |
| 41 | this.entityInformation = elasticsearchEntityInformationCreator.getEntityInformation(metadata.getReturnedDomainClass(method)); |
| 42 | this.method = method; |
| 43 | } |
| 44 | |
| 45 | public boolean hasAnnotatedQuery() { |
| 46 | return getQueryAnnotation() != null; |
| 47 | } |
| 48 | |
| 49 | public String getAnnotatedQuery() { |
| 50 | String query = (String) AnnotationUtils.getValue(getQueryAnnotation(), "value"); |
| 51 | return StringUtils.hasText(query) ? query : null; |
| 52 | } |
| 53 | |
| 54 | private Query getQueryAnnotation() { |
| 55 | return this.method.getAnnotation(Query.class); |
| 56 | } |
| 57 | |
| 58 | } |