com.github.ydespreaux.spring.data.elasticsearch.core.converter.MappingElasticsearchConverter Maven / Gradle / Ivy
/*
* Copyright (C) 2018 Yoann Despréaux
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING . If not, write to the
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Please send bugreports with examples or suggestions to [email protected]
*/
package com.github.ydespreaux.spring.data.elasticsearch.core.converter;
import com.github.ydespreaux.spring.data.elasticsearch.core.EntityMapper;
import com.github.ydespreaux.spring.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import com.github.ydespreaux.spring.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.util.Assert;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Yoann Despréaux
* @since 1.0.0
*/
@Slf4j
public class MappingElasticsearchConverter implements ElasticsearchConverter, ApplicationContextAware {
private final MappingContext mappingContext;
private final Map, ElasticsearchPersistentEntity> context = new ConcurrentHashMap<>();
private final EntityMapper mapper;
public MappingElasticsearchConverter(MappingContext mappingContext, final EntityMapper mapper) {
Assert.notNull(mappingContext, "MappingContext must not be null!");
Assert.notNull(mapper, "Mapper must not be null!");
this.mappingContext = mappingContext;
this.mapper = mapper;
}
@Override
public ElasticsearchPersistentEntity getRequiredPersistentEntity(Class clazz) {
if (context.containsKey(clazz)) {
return (ElasticsearchPersistentEntity) context.get(clazz);
}
ElasticsearchPersistentEntity persistentEntity = (ElasticsearchPersistentEntity) this.mappingContext.getRequiredPersistentEntity(clazz);
this.mapper.register(persistentEntity);
this.context.put(clazz, persistentEntity);
return persistentEntity;
}
/**
* Returns the underlying {@link MappingContext} used by the converter.
*
* @return never {@literal null}
*/
@Override
public MappingContext getMappingContext() {
return this.mappingContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
if (mappingContext instanceof ApplicationContextAware) {
((ApplicationContextAware) mappingContext).setApplicationContext(applicationContext);
}
}
}