package eu.eudat.models.rda.mapper;

import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.elastic.entities.Tag;
import eu.eudat.logic.utilities.json.JavaToJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

public class KeywordRDAMapper {
	private static final Logger logger = LoggerFactory.getLogger(KeywordRDAMapper.class);

	public static List<String> toRDA(String value) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			value = JavaToJson.objectStringToJson(value);
			List<Tag> tags = Arrays.asList(mapper.readValue(value, Tag[].class));
			List<String> keywordNames = tags.stream().map(Tag::getName).collect(Collectors.toList());
			return keywordNames;
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}

		return new ArrayList<>();
	}
}
