package eu.eudat.elastic.entities;

import org.elasticsearch.common.xcontent.XContentBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by ikalyvas on 7/5/2018.
 */
public class Dataset implements ElasticEntity<Dataset> {
    private static final Logger logger = LoggerFactory.getLogger(Dataset.class);

    private String id;
    private List<Tag> tags = new LinkedList<>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Tag> getTags() {
        return tags;
    }

    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }

    @Override
    public XContentBuilder toElasticEntity(XContentBuilder builder) throws IOException {
        builder.startObject();
        builder.field("id", this.id);
        builder.startArray("tags");
        this.tags.forEach(x -> {
            try {
                x.toElasticEntity(builder);
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        });
        builder.endArray();
        builder.endObject();
        return builder;
    }

    @Override
    public Dataset fromElasticEntity(Map<String, Object> fields) {
        if (fields != null) {
            this.id = (String) fields.get("id");
            this.tags = ((List<Tag>) fields.get("tags"));
        }
        return this;
    }

}
