| 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.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.mapping.model.MappingException; |
| 26 | import org.springframework.data.util.TypeInformation; |
| 27 | import org.springframework.expression.spel.support.StandardEvaluationContext; |
| 28 | import org.springframework.util.Assert; |
| 29 | |
| 30 | import java.util.Locale; |
| 31 | |
| 32 | import static org.springframework.util.StringUtils.hasText; |
| 33 | |
| 34 | /** |
| 35 | * Elasticsearch specific {@link org.springframework.data.mapping.PersistentEntity} implementation holding |
| 36 | * |
| 37 | * @param <T> |
| 38 | * |
| 39 | * @author Rizwan Idrees |
| 40 | * @author Mohsin Husen |
| 41 | */ |
| 42 | public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntity<T, ElasticsearchPersistentProperty> implements |
| 43 | ElasticsearchPersistentEntity<T>, ApplicationContextAware { |
| 44 | |
| 45 | private final StandardEvaluationContext context; |
| 46 | private String indexName; |
| 47 | private String indexType; |
| 48 | private ElasticsearchPersistentProperty versionProperty; |
| 49 | |
| 50 | public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) { |
| 51 | super(typeInformation); |
| 52 | this.context = new StandardEvaluationContext(); |
| 53 | Class<T> clazz = typeInformation.getType(); |
| 54 | if(clazz.isAnnotationPresent(Document.class)){ |
| 55 | Document document = clazz.getAnnotation(Document.class); |
| 56 | Assert.hasText(document.indexName(), " Unknown indexName. Make sure the indexName is defined. e.g @Document(indexName=\"foo\")"); |
| 57 | this.indexName = typeInformation.getType().getAnnotation(Document.class).indexName(); |
| 58 | this.indexType = hasText(document.type())? document.type() : clazz.getSimpleName().toLowerCase(Locale.ENGLISH); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 64 | context.addPropertyAccessor(new BeanFactoryAccessor()); |
| 65 | context.setBeanResolver(new BeanFactoryResolver(applicationContext)); |
| 66 | context.setRootObject(applicationContext); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String getIndexName() { |
| 71 | return indexName; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public String getIndexType() { |
| 76 | return indexType; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public ElasticsearchPersistentProperty getVersionProperty() { |
| 81 | return this.versionProperty; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void addPersistentProperty(ElasticsearchPersistentProperty property) { |
| 86 | super.addPersistentProperty(property); |
| 87 | if(property.isVersionProperty()){ |
| 88 | if (this.versionProperty != null) { |
| 89 | throw new MappingException(String.format( |
| 90 | "Attempt to add version property %s but already have property %s registered " |
| 91 | + "as version. Check your mapping configuration!", property.getField(), versionProperty.getField())); |
| 92 | } |
| 93 | Assert.isTrue(property.getType() == Long.class, "Version property should be Long"); |
| 94 | this.versionProperty = property; |
| 95 | } |
| 96 | } |
| 97 | } |