1 | /* |
2 | * Copyright 2012 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.support; |
17 | |
18 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
19 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; |
20 | import org.springframework.data.elasticsearch.repository.query.ElasticsearchPartQuery; |
21 | import org.springframework.data.elasticsearch.repository.query.ElasticsearchQueryMethod; |
22 | import org.springframework.data.elasticsearch.repository.query.ElasticsearchStringQuery; |
23 | import org.springframework.data.querydsl.QueryDslPredicateExecutor; |
24 | import org.springframework.data.repository.core.NamedQueries; |
25 | import org.springframework.data.repository.core.RepositoryMetadata; |
26 | import org.springframework.data.repository.core.support.RepositoryFactorySupport; |
27 | import org.springframework.data.repository.query.QueryLookupStrategy; |
28 | import org.springframework.data.repository.query.RepositoryQuery; |
29 | import org.springframework.util.Assert; |
30 | |
31 | import java.io.Serializable; |
32 | import java.lang.reflect.Method; |
33 | |
34 | import static org.springframework.data.querydsl.QueryDslUtils.QUERY_DSL_PRESENT; |
35 | |
36 | /** |
37 | * Factory to create {@link ElasticsearchRepository} |
38 | * |
39 | */ |
40 | public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport { |
41 | |
42 | private final ElasticsearchOperations elasticsearchOperations; |
43 | private final ElasticsearchEntityInformationCreator entityInformationCreator; |
44 | |
45 | public ElasticsearchRepositoryFactory(ElasticsearchOperations elasticsearchOperations) { |
46 | Assert.notNull(elasticsearchOperations); |
47 | this.elasticsearchOperations = elasticsearchOperations; |
48 | this.entityInformationCreator = new ElasticsearchEntityInformationCreatorImpl(elasticsearchOperations.getElasticsearchConverter() |
49 | .getMappingContext()); |
50 | } |
51 | |
52 | @Override |
53 | public <T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) { |
54 | return entityInformationCreator.getEntityInformation(domainClass); |
55 | } |
56 | |
57 | @Override |
58 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
59 | protected Object getTargetRepository(RepositoryMetadata metadata) { |
60 | SimpleElasticsearchRepository repository = new SimpleElasticsearchRepository(getEntityInformation(metadata.getDomainType()), elasticsearchOperations); |
61 | repository.setEntityClass(metadata.getDomainType()); |
62 | return repository; |
63 | } |
64 | |
65 | @Override |
66 | protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { |
67 | if (isQueryDslRepository(metadata.getRepositoryInterface())) { |
68 | throw new IllegalArgumentException("QueryDsl Support has not been implemented yet."); |
69 | } |
70 | return SimpleElasticsearchRepository.class; |
71 | } |
72 | |
73 | private static boolean isQueryDslRepository(Class<?> repositoryInterface) { |
74 | return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface); |
75 | } |
76 | |
77 | @Override |
78 | protected QueryLookupStrategy getQueryLookupStrategy(QueryLookupStrategy.Key key) { |
79 | return new ElasticsearchQueryLookupStrategy(); |
80 | } |
81 | |
82 | private class ElasticsearchQueryLookupStrategy implements QueryLookupStrategy { |
83 | |
84 | @Override |
85 | public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, NamedQueries namedQueries) { |
86 | |
87 | ElasticsearchQueryMethod queryMethod = new ElasticsearchQueryMethod(method, metadata, entityInformationCreator); |
88 | String namedQueryName = queryMethod.getNamedQueryName(); |
89 | |
90 | if (namedQueries.hasQuery(namedQueryName)) { |
91 | String namedQuery = namedQueries.getQuery(namedQueryName); |
92 | return new ElasticsearchStringQuery(queryMethod, elasticsearchOperations, namedQuery); |
93 | } |
94 | else if (queryMethod.hasAnnotatedQuery()) { |
95 | return new ElasticsearchStringQuery(queryMethod, elasticsearchOperations, queryMethod.getAnnotatedQuery()); |
96 | } |
97 | return new ElasticsearchPartQuery(queryMethod, elasticsearchOperations); |
98 | } |
99 | } |
100 | |
101 | } |