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

COVERAGE SUMMARY FOR SOURCE FILE [SimpleElasticsearchPersistentEntity.java]

nameclass, %method, %block, %line, %
SimpleElasticsearchPersistentEntity.java100% (1/1)83%  (5/6)64%  (73/114)78%  (17.9/23)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SimpleElasticsearchPersistentEntity100% (1/1)83%  (5/6)64%  (73/114)78%  (17.9/23)
setApplicationContext (ApplicationContext): void 0%   (0/1)0%   (0/18)0%   (0/4)
addPersistentProperty (ElasticsearchPersistentProperty): void 100% (1/1)54%  (22/41)86%  (6/7)
SimpleElasticsearchPersistentEntity (TypeInformation): void 100% (1/1)91%  (42/46)98%  (8.9/9)
getIndexName (): String 100% (1/1)100% (3/3)100% (1/1)
getIndexType (): String 100% (1/1)100% (3/3)100% (1/1)
getVersionProperty (): ElasticsearchPersistentProperty 100% (1/1)100% (3/3)100% (1/1)

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.core.mapping;
17 
18import org.springframework.beans.BeansException;
19import org.springframework.context.ApplicationContext;
20import org.springframework.context.ApplicationContextAware;
21import org.springframework.context.expression.BeanFactoryAccessor;
22import org.springframework.context.expression.BeanFactoryResolver;
23import org.springframework.data.elasticsearch.annotations.Document;
24import org.springframework.data.mapping.model.BasicPersistentEntity;
25import org.springframework.data.mapping.model.MappingException;
26import org.springframework.data.util.TypeInformation;
27import org.springframework.expression.spel.support.StandardEvaluationContext;
28import org.springframework.util.Assert;
29 
30import java.util.Locale;
31 
32import 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 */
42public 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}

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