| 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.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
| 20 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; |
| 21 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; |
| 22 | import org.springframework.data.mapping.model.BeanWrapper; |
| 23 | import org.springframework.data.repository.core.support.AbstractEntityInformation; |
| 24 | import org.springframework.util.Assert; |
| 25 | |
| 26 | import java.io.Serializable; |
| 27 | |
| 28 | /** |
| 29 | * Elasticsearch specific implementation of {@link org.springframework.data.repository.core.support.AbstractEntityInformation} |
| 30 | * |
| 31 | * @param <T> |
| 32 | * @param <ID> |
| 33 | * |
| 34 | * @author Rizwan Idrees |
| 35 | * @author Mohsin Husen |
| 36 | */ |
| 37 | public class MappingElasticsearchEntityInformation<T, ID extends Serializable> extends AbstractEntityInformation<T, ID> |
| 38 | implements ElasticsearchEntityInformation<T, ID> { |
| 39 | |
| 40 | private static final Logger logger = LoggerFactory.getLogger(MappingElasticsearchEntityInformation.class); |
| 41 | private final ElasticsearchPersistentEntity<T> entityMetadata; |
| 42 | private final String indexName; |
| 43 | private final String type; |
| 44 | |
| 45 | public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity) { |
| 46 | this(entity, null, null); |
| 47 | } |
| 48 | |
| 49 | public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity, String indexName, String type) { |
| 50 | super(entity.getType()); |
| 51 | this.entityMetadata = entity; |
| 52 | this.indexName = indexName; |
| 53 | this.type = type; |
| 54 | } |
| 55 | |
| 56 | @SuppressWarnings("unchecked") |
| 57 | @Override |
| 58 | public ID getId(T entity) { |
| 59 | ElasticsearchPersistentProperty id = entityMetadata.getIdProperty(); |
| 60 | try { |
| 61 | if(id != null){ |
| 62 | return (ID) BeanWrapper.create(entity, null).getProperty(id); |
| 63 | } |
| 64 | } catch (Exception e) { |
| 65 | throw new IllegalStateException("ID could not be resolved", e); |
| 66 | } |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | @SuppressWarnings("unchecked") |
| 71 | @Override |
| 72 | public Class<ID> getIdType() { |
| 73 | return (Class<ID>) String.class; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String getIdAttribute() { |
| 78 | Assert.notNull(entityMetadata.getIdProperty(),"Unable to identify 'id' property in class " + entityMetadata.getType().getSimpleName() +". Make sure the 'id' property is annotated with @Id or named as 'id' or 'documentId' "); |
| 79 | return entityMetadata.getIdProperty().getFieldName(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public String getIndexName() { |
| 84 | return indexName != null? indexName : entityMetadata.getIndexName(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public String getType() { |
| 89 | return type != null? type : entityMetadata.getIndexType(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public String getVersionAttribute() { |
| 94 | return entityMetadata.getVersionProperty().getFieldName(); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Long getVersion(T entity) { |
| 99 | ElasticsearchPersistentProperty versionProperty = entityMetadata.getVersionProperty(); |
| 100 | try { |
| 101 | if(versionProperty != null){ |
| 102 | return (Long) BeanWrapper.create(entity, null).getProperty(versionProperty); |
| 103 | } |
| 104 | } catch (Exception e) { |
| 105 | throw new IllegalStateException("failed to load version field", e); |
| 106 | } |
| 107 | return null; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |