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; import java.util.UUID; import java.util.stream.Collectors; /** * Created by ikalyvas on 7/5/2018. */ public class Dataset implements ElasticEntity { private static final Logger logger = LoggerFactory.getLogger(Dataset.class); public enum Status { SAVED((short) 0), FINALISED((short) 1), CANCELED((short) 2), DELETED((short) 99),; private short value; private Status(short value) { this.value = value; } public short getValue() { return value; } public static Status fromInteger(int value) { switch (value) { case 0: return SAVED; case 1: return FINALISED; case 2: return CANCELED; case 99: return DELETED; default: throw new RuntimeException("Unsupported Dataset Status"); } } } private String id; private List tags = new LinkedList<>(); private String label; private UUID template; private Short status; private UUID dmp; private UUID group; private UUID grant; private List collaborators; private Boolean lastVersion; private List organizations; public String getId() { return id; } public void setId(String id) { this.id = id; } public List getTags() { return tags; } public void setTags(List tags) { this.tags = tags; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public UUID getTemplate() { return template; } public void setTemplate(UUID template) { this.template = template; } public Short getStatus() { return status; } public void setStatus(Short status) { this.status = status; } public UUID getDmp() { return dmp; } public void setDmp(UUID dmp) { this.dmp = dmp; } public UUID getGroup() { return group; } public void setGroup(UUID group) { this.group = group; } public UUID getGrant() { return grant; } public void setGrant(UUID grant) { this.grant = grant; } public List getCollaborators() { return collaborators; } public void setCollaborators(List collaborators) { this.collaborators = collaborators; } public Boolean getLastVersion() { return lastVersion; } public void setLastVersion(Boolean lastVersion) { this.lastVersion = lastVersion; } public List getOrganizations() { return organizations; } public void setOrganizations(List organizations) { this.organizations = organizations; } @Override public XContentBuilder toElasticEntity(XContentBuilder builder) throws IOException { builder.startObject(); builder.field("id", this.id); builder.field("label", this.label); builder.field("template", this.template.toString()); builder.field("status", this.status.toString()); builder.field("dmp", this.dmp.toString()); builder.field("group", this.group.toString()); builder.field("grant", this.grant.toString()); if (collaborators != null) { builder.startArray("collaborators"); this.collaborators.forEach(x -> { try { x.toElasticEntity(builder); } catch (IOException e) { logger.error(e.getMessage(), e); } }); builder.endArray(); } builder.field("lastVersion", this.lastVersion.toString()); if (organizations != null) { builder.startArray("organizations"); this.organizations.forEach(x -> { try { x.toElasticEntity(builder); } catch (IOException e) { logger.error(e.getMessage(), e); } }); builder.endArray(); } if (this.tags != null) { 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 fields) { if (fields != null) { this.id = (String) fields.get("id"); this.tags = ((List) fields.get("tags")); this.label = (String) fields.get("label"); this.template = UUID.fromString((String) fields.get("template")); this.status = Short.valueOf((String) fields.get("status")); this.dmp = UUID.fromString((String) fields.get("dmp")); this.group = UUID.fromString((String) fields.get("goup")); this.grant = UUID.fromString((String) fields.get("grant")); this.collaborators = ((List) fields.get("collaborators")); this.lastVersion = Boolean.parseBoolean((String) fields.get("lastVersion")); this.organizations = (List) fields.get("organiztions"); } return this; } }