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.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; public class Dmp implements ElasticEntity { private static final Logger logger = LoggerFactory.getLogger(Dmp.class); public enum DMPStatus { ACTIVE((short) 0), FINALISED((short) 1),DELETED((short) 99); private short value; private DMPStatus(short value) { this.value = value; } public short getValue() { return value; } public static DMPStatus fromInteger(short value) { switch (value) { case 0: return ACTIVE; case 1: return FINALISED; case 99: return DELETED; default: throw new RuntimeException("Unsupported DMP Status"); } } } private UUID id; private String label; private String description; private UUID groupId; private Short status; private List templates; private List collaborators; private List organizations; private Boolean lastVersion; private Boolean lastPublicVersion; private Boolean isPublic; private List datasets; private UUID grant; private Short grantStatus; public UUID getId() { return id; } public void setId(UUID id) { this.id = id; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public UUID getGroupId() { return groupId; } public void setGroupId(UUID groupId) { this.groupId = groupId; } public Short getStatus() { return status; } public void setStatus(Short status) { this.status = status; } public List getTemplates() { return templates; } public void setTemplates(List templates) { this.templates = templates; } public List getCollaborators() { return collaborators; } public void setCollaborators(List collaborators) { this.collaborators = collaborators; } public List getOrganizations() { return organizations; } public void setOrganizations(List organizations) { this.organizations = organizations; } public Boolean getLastVersion() { return lastVersion; } public void setLastVersion(Boolean lastVersion) { this.lastVersion = lastVersion; } public Boolean getLastPublicVersion() { return lastPublicVersion; } public void setLastPublicVersion(Boolean lastPublicVersion) { this.lastPublicVersion = lastPublicVersion; } public Boolean getPublic() { return isPublic; } public void setPublic(Boolean aPublic) { isPublic = aPublic; } public List getDatasets() { return datasets; } public void setDatasets(List datasets) { this.datasets = datasets; } public UUID getGrant() { return grant; } public void setGrant(UUID grant) { this.grant = grant; } public Short getGrantStatus() { return grantStatus; } public void setGrantStatus(Short grantStatus) { this.grantStatus = grantStatus; } @Override public XContentBuilder toElasticEntity(XContentBuilder builder) throws IOException { builder.startObject(); if (this.id != null) { builder.field(MapKey.ID.getName(), this.id.toString()); } builder.field(MapKey.LABEL.getName(), this.label); builder.field(MapKey.DESCRIPTION.getName(), this.description); if (this.groupId != null) { builder.field(MapKey.GROUPID.getName(), this.groupId.toString()); } builder.field(MapKey.STATUS.getName(), this.status); if (this.templates != null && !this.templates.isEmpty()) { builder.startArray(MapKey.TEMPLATES.getName()); this.templates.forEach(template -> { try { template.toElasticEntity(builder); } catch (IOException e) { logger.error(e.getMessage(), e); } }); builder.endArray(); } if (this.collaborators != null && !this.collaborators.isEmpty()) { builder.startArray(MapKey.COLLABORATORS.getName()); this.collaborators.forEach(collaborator -> { try { collaborator.toElasticEntity(builder); } catch (IOException e) { logger.error(e.getMessage(), e); } }); builder.endArray(); } if (this.organizations != null && !this.organizations.isEmpty()) { builder.startArray(MapKey.ORGANIZATIONS.getName()); this.organizations.forEach(organization -> { try { organization.toElasticEntity(builder); } catch (IOException e) { logger.error(e.getMessage(), e); } }); builder.endArray(); } builder.field(MapKey.LASTVERSION.getName(), this.lastVersion); builder.field(MapKey.LASTPUBLICVERSION.getName(), this.lastPublicVersion); builder.field(MapKey.ISPUBLIC.getName(), this.isPublic); if (datasets != null && !this.datasets.isEmpty()) { builder.startArray(MapKey.DATASETS.getName()); this.datasets.forEach(dataset -> { try { if (dataset != null) { dataset.toElasticEntity(builder); } } catch (Exception e) { logger.error(e.getMessage(), e); } }); builder.endArray(); } if (this.grant != null) { builder.field(MapKey.GRANT.getName(), this.grant.toString()); } builder.field(MapKey.GRANTSTATUS.getName(), this.grantStatus); builder.endObject(); return builder; } @Override public Dmp fromElasticEntity(Map fields) { this.id = UUID.fromString((String) fields.get(MapKey.ID.getName())); this.label = (String) fields.get(MapKey.LABEL.getName()); this.description = (String) fields.get(MapKey.DESCRIPTION.getName()); if (fields.get(MapKey.GROUPID.getName()) != null) { this.groupId = UUID.fromString((String) fields.get(MapKey.GROUPID.getName())); } this.status = Short.valueOf(fields.get(MapKey.STATUS.getName()).toString()); if (fields.get(MapKey.TEMPLATES.getName()) != null) { this.templates = ((List>) fields.get(MapKey.TEMPLATES.getName())).stream().map(hashMap -> new DatasetTempalate().fromElasticEntity(hashMap)).collect(Collectors.toList()); } if (fields.get(MapKey.COLLABORATORS.getName()) != null) { this.collaborators = ((List>) fields.get(MapKey.COLLABORATORS.getName())).stream().map(map -> new Collaborator().fromElasticEntity(map)).collect(Collectors.toList()); } if (fields.get(MapKey.ORGANIZATIONS.getName()) != null) { this.organizations = ((List>) fields.get(MapKey.ORGANIZATIONS.getName())).stream().map(map -> new Organization().fromElasticEntity(map)).collect(Collectors.toList()); } this.lastVersion = (Boolean) fields.get(MapKey.LASTVERSION.getName()); this.lastPublicVersion = (Boolean) fields.get(MapKey.LASTPUBLICVERSION.getName()); this.isPublic = (Boolean) fields.get(MapKey.ISPUBLIC.getName()); if (fields.get(MapKey.DATASETS.getName()) != null) { this.datasets = ((List>) fields.get(MapKey.DATASETS.getName())).stream().map(map -> new Dataset().fromElasticEntity(map)).collect(Collectors.toList()); } this.grant = UUID.fromString((String) fields.get(MapKey.GRANT.getName())); if (fields.get(MapKey.GRANTSTATUS.getName()) != null) { this.grantStatus = Short.valueOf(fields.get(MapKey.GRANTSTATUS.getName()).toString()); } return this; } public enum MapKey { ID ("id"), LABEL ("label"), DESCRIPTION ("description"), GROUPID ("groupId"), STATUS ("status"), TEMPLATES ("templates"), COLLABORATORS ("collaborators"), ORGANIZATIONS ("organizations"), LASTVERSION ("lastVersion"), LASTPUBLICVERSION ("lastPublicVersion"), ISPUBLIC ("isPublic"), DATASETS ("datasets"), GRANT ("grant"), GRANTSTATUS ("grantStatus"); private final String name; private MapKey(String name) { this.name = name; } public String getName() { return name; } } }