argos/dmp-backend/elastic/src/main/java/eu/eudat/elastic/repository/ElasticRepository.java

53 lines
1.8 KiB
Java

package eu.eudat.elastic.repository;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.elastic.criteria.Criteria;
import eu.eudat.elastic.entities.ElasticEntity;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import java.io.IOException;
/**
* Created by ikalyvas on 7/5/2018.
*/
public abstract class ElasticRepository<T extends ElasticEntity,C extends Criteria> implements Repository<T,C> {
private static final Logger logger = LoggerFactory.getLogger(ElasticRepository.class);
private ElasticsearchClient client;
public ElasticsearchClient getClient() {
return client;
}
public ElasticRepository(ElasticsearchClient client, Environment environment) {
try {
if (!Boolean.TRUE.equals(environment.getProperty("elastic.enabled", boolean.class))){
logger.warn("Unable to connect to Elastic Services");
this.client = null;
return;
}
if (client.ping().value()) {
this.client = client;
}
} catch (IOException e) {
logger.warn("Unable to connect to Elastic Services");
logger.error(e.getMessage(), e);
this.client = null;
}
}
public <T> T transformFromString(String value, Class<T> tClass) {
ObjectMapper mapper = new ObjectMapper();
T item = null;
try {
item = mapper.readValue(value, tClass);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return item;
}
}