EMMA Coverage Report (generated Tue Mar 05 16:36:55 GMT 2013)
[all classes][org.springframework.data.elasticsearch.repository.support]

COVERAGE SUMMARY FOR SOURCE FILE [MappingElasticsearchEntityInformation.java]

nameclass, %method, %block, %line, %
MappingElasticsearchEntityInformation.java100% (1/1)70%  (7/10)56%  (68/121)62%  (17.4/28)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MappingElasticsearchEntityInformation100% (1/1)70%  (7/10)56%  (68/121)62%  (17.4/28)
getIdAttribute (): String 0%   (0/1)0%   (0/23)0%   (0/2)
getIdType (): Class 0%   (0/1)0%   (0/2)0%   (0/1)
getVersionAttribute (): String 0%   (0/1)0%   (0/5)0%   (0/1)
getId (Object): Serializable 100% (1/1)58%  (14/24)43%  (3/7)
getVersion (Object): Long 100% (1/1)70%  (16/23)71%  (5/7)
getIndexName (): String 100% (1/1)70%  (7/10)70%  (0.7/1)
getType (): String 100% (1/1)70%  (7/10)70%  (0.7/1)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
MappingElasticsearchEntityInformation (ElasticsearchPersistentEntity): void 100% (1/1)100% (6/6)100% (2/2)
MappingElasticsearchEntityInformation (ElasticsearchPersistentEntity, String,... 100% (1/1)100% (14/14)100% (5/5)

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 */
16package org.springframework.data.elasticsearch.repository.support;
17 
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
21import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
22import org.springframework.data.mapping.model.BeanWrapper;
23import org.springframework.data.repository.core.support.AbstractEntityInformation;
24import org.springframework.util.Assert;
25 
26import 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 */
37public 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 

[all classes][org.springframework.data.elasticsearch.repository.support]
EMMA 2.0.5312 (C) Vladimir Roubtsov