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.mapping.ElasticsearchPersistentEntity; |
19 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; |
20 | import org.springframework.data.mapping.model.BeanWrapper; |
21 | import org.springframework.data.repository.core.support.AbstractEntityInformation; |
22 | import org.springframework.util.Assert; |
23 | |
24 | import java.io.Serializable; |
25 | |
26 | /** |
27 | * Elasticsearch specific implementation of {@link org.springframework.data.repository.core.support.AbstractEntityInformation} |
28 | * |
29 | * @param <T> |
30 | * @param <ID> |
31 | */ |
32 | public class MappingElasticsearchEntityInformation<T, ID extends Serializable> extends AbstractEntityInformation<T, ID> |
33 | implements ElasticsearchEntityInformation<T, ID> { |
34 | |
35 | private final ElasticsearchPersistentEntity<T> entityMetadata; |
36 | private final String indexName; |
37 | private final String type; |
38 | |
39 | public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity) { |
40 | this(entity, null, null); |
41 | } |
42 | |
43 | public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity, String indexName, String type) { |
44 | super(entity.getType()); |
45 | this.entityMetadata = entity; |
46 | this.indexName = indexName; |
47 | this.type = type; |
48 | } |
49 | |
50 | @SuppressWarnings("unchecked") |
51 | @Override |
52 | public ID getId(T entity) { |
53 | ElasticsearchPersistentProperty id = entityMetadata.getIdProperty(); |
54 | try { |
55 | return (ID) BeanWrapper.create(entity, null).getProperty(id); |
56 | } catch (Exception e) { |
57 | throw new IllegalStateException("ID could not be resolved", e); |
58 | } |
59 | } |
60 | |
61 | @SuppressWarnings("unchecked") |
62 | @Override |
63 | public Class<ID> getIdType() { |
64 | return (Class<ID>) String.class; |
65 | } |
66 | |
67 | @Override |
68 | public String getIdAttribute() { |
69 | 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' "); |
70 | return entityMetadata.getIdProperty().getFieldName(); |
71 | } |
72 | |
73 | @Override |
74 | public String getIndexName() { |
75 | return indexName != null? indexName : entityMetadata.getIndexName(); |
76 | } |
77 | |
78 | @Override |
79 | public String getType() { |
80 | return type != null? type : entityMetadata.getIndexType(); |
81 | } |
82 | } |