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.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 | * @author Rizwan Idrees |
40 | * @author Mohsin Husen |
41 | */ |
42 | public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport { |
43 | |
44 | private final ElasticsearchOperations elasticsearchOperations; |
45 | private final ElasticsearchEntityInformationCreator entityInformationCreator; |
46 | |
47 | public ElasticsearchRepositoryFactory(ElasticsearchOperations elasticsearchOperations) { |
48 | Assert.notNull(elasticsearchOperations); |
49 | this.elasticsearchOperations = elasticsearchOperations; |
50 | this.entityInformationCreator = new ElasticsearchEntityInformationCreatorImpl(elasticsearchOperations.getElasticsearchConverter() |
51 | .getMappingContext()); |
52 | } |
53 | |
54 | @Override |
55 | public <T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) { |
56 | return entityInformationCreator.getEntityInformation(domainClass); |
57 | } |
58 | |
59 | @Override |
60 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
61 | protected Object getTargetRepository(RepositoryMetadata metadata) { |
62 | SimpleElasticsearchRepository repository = new SimpleElasticsearchRepository(getEntityInformation(metadata.getDomainType()), elasticsearchOperations); |
63 | repository.setEntityClass(metadata.getDomainType()); |
64 | return repository; |
65 | } |
66 | |
67 | @Override |
68 | protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { |
69 | if (isQueryDslRepository(metadata.getRepositoryInterface())) { |
70 | throw new IllegalArgumentException("QueryDsl Support has not been implemented yet."); |
71 | } |
72 | return SimpleElasticsearchRepository.class; |
73 | } |
74 | |
75 | private static boolean isQueryDslRepository(Class<?> repositoryInterface) { |
76 | return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface); |
77 | } |
78 | |
79 | @Override |
80 | protected QueryLookupStrategy getQueryLookupStrategy(QueryLookupStrategy.Key key) { |
81 | return new ElasticsearchQueryLookupStrategy(); |
82 | } |
83 | |
84 | private class ElasticsearchQueryLookupStrategy implements QueryLookupStrategy { |
85 | |
86 | @Override |
87 | public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, NamedQueries namedQueries) { |
88 | |
89 | ElasticsearchQueryMethod queryMethod = new ElasticsearchQueryMethod(method, metadata, entityInformationCreator); |
90 | String namedQueryName = queryMethod.getNamedQueryName(); |
91 | |
92 | if (namedQueries.hasQuery(namedQueryName)) { |
93 | String namedQuery = namedQueries.getQuery(namedQueryName); |
94 | return new ElasticsearchStringQuery(queryMethod, elasticsearchOperations, namedQuery); |
95 | } |
96 | else if (queryMethod.hasAnnotatedQuery()) { |
97 | return new ElasticsearchStringQuery(queryMethod, elasticsearchOperations, queryMethod.getAnnotatedQuery()); |
98 | } |
99 | return new ElasticsearchPartQuery(queryMethod, elasticsearchOperations); |
100 | } |
101 | } |
102 | |
103 | } |