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.core.mapping; |
17 | |
18 | import org.springframework.beans.BeansException; |
19 | import org.springframework.context.ApplicationContext; |
20 | import org.springframework.context.ApplicationContextAware; |
21 | import org.springframework.context.expression.BeanFactoryAccessor; |
22 | import org.springframework.context.expression.BeanFactoryResolver; |
23 | import org.springframework.data.elasticsearch.annotations.Document; |
24 | import org.springframework.data.mapping.model.BasicPersistentEntity; |
25 | import org.springframework.data.util.TypeInformation; |
26 | import org.springframework.expression.spel.support.StandardEvaluationContext; |
27 | import org.springframework.util.Assert; |
28 | |
29 | import java.util.Locale; |
30 | |
31 | import static org.springframework.util.StringUtils.hasText; |
32 | |
33 | /** |
34 | * Elasticsearch specific {@link org.springframework.data.mapping.PersistentEntity} implementation holding |
35 | * |
36 | * @param <T> |
37 | */ |
38 | public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntity<T, ElasticsearchPersistentProperty> implements |
39 | ElasticsearchPersistentEntity<T>, ApplicationContextAware { |
40 | |
41 | private final StandardEvaluationContext context; |
42 | private String indexName; |
43 | private String indexType; |
44 | |
45 | public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) { |
46 | super(typeInformation); |
47 | this.context = new StandardEvaluationContext(); |
48 | Class<T> clazz = typeInformation.getType(); |
49 | Assert.isTrue(clazz.isAnnotationPresent(Document.class), |
50 | clazz.getSimpleName() + " is not a Document. Make sure the document class is annotated with @Document(indexName=\"foo\")"); |
51 | Document document = clazz.getAnnotation(Document.class); |
52 | Assert.hasText(document.indexName(), " Unknown indexName. Make sure the indexName is defined. e.g @Document(indexName=\"foo\")"); |
53 | this.indexName = typeInformation.getType().getAnnotation(Document.class).indexName(); |
54 | this.indexType = hasText(document.type())? document.type() : clazz.getSimpleName().toLowerCase(Locale.ENGLISH); |
55 | } |
56 | |
57 | @Override |
58 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
59 | context.addPropertyAccessor(new BeanFactoryAccessor()); |
60 | context.setBeanResolver(new BeanFactoryResolver(applicationContext)); |
61 | context.setRootObject(applicationContext); |
62 | } |
63 | |
64 | @Override |
65 | public String getIndexName() { |
66 | return indexName; |
67 | } |
68 | |
69 | @Override |
70 | public String getIndexType() { |
71 | return indexType; |
72 | } |
73 | } |