package eu.dnetlib.dhp.provision.scholix; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import eu.dnetlib.dhp.provision.RelatedItemInfo; import eu.dnetlib.dhp.schema.oaf.Author; import eu.dnetlib.dhp.schema.oaf.StructuredProperty; import eu.dnetlib.dhp.schema.scholexplorer.DLIDataset; import eu.dnetlib.dhp.schema.scholexplorer.DLIPublication; import eu.dnetlib.dhp.schema.scholexplorer.DLIUnknown; import java.io.Serializable; import java.util.List; import java.util.stream.Collectors; public class ScholixSummary implements Serializable { private String id; private List localIdentifier; private Typology typology; private List title; private List author; private List date; private String description; private List subject; private List publisher; private int relatedPublications; private int relatedDatasets; private int relatedUnknown; private List datasources; public String getId() { return id; } public void setId(String id) { this.id = id; } public List getLocalIdentifier() { return localIdentifier; } public void setLocalIdentifier(List localIdentifier) { this.localIdentifier = localIdentifier; } public Typology getTypology() { return typology; } public void setTypology(Typology typology) { this.typology = typology; } public List getTitle() { return title; } public void setTitle(List title) { this.title = title; } public List getAuthor() { return author; } public void setAuthor(List author) { this.author = author; } public List getDate() { return date; } public void setDate(List date) { this.date = date; } @JsonProperty("abstract") public String getDescription() { return description; } @JsonProperty("abstract") public void setDescription(String description) { this.description = description; } public List getSubject() { return subject; } public void setSubject(List subject) { this.subject = subject; } public List getPublisher() { return publisher; } public void setPublisher(List publisher) { this.publisher = publisher; } public int getRelatedPublications() { return relatedPublications; } public void setRelatedPublications(int relatedPublications) { this.relatedPublications = relatedPublications; } public int getRelatedDatasets() { return relatedDatasets; } public void setRelatedDatasets(int relatedDatasets) { this.relatedDatasets = relatedDatasets; } public int getRelatedUnknown() { return relatedUnknown; } public void setRelatedUnknown(int relatedUnknown) { this.relatedUnknown = relatedUnknown; } public List getDatasources() { return datasources; } public void setDatasources(List datasources) { this.datasources = datasources; } public static String fromJsonOAF(final Typology oafType, final String oafJson, final String relEntityJson) { try { final ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); RelatedItemInfo relatedItemInfo = mapper.readValue(relEntityJson, RelatedItemInfo.class); switch (oafType) { case dataset: return mapper.writeValueAsString(summaryFromDataset(mapper.readValue(oafJson, DLIDataset.class), relatedItemInfo)); case publication: return mapper.writeValueAsString(summaryFromPublication(mapper.readValue(oafJson, DLIPublication.class), relatedItemInfo)); case unknown: return mapper.writeValueAsString(summaryFromUnknown(mapper.readValue(oafJson, DLIUnknown.class), relatedItemInfo)); } } catch (Throwable e) { throw new RuntimeException(e); } return null; } private static ScholixSummary summaryFromDataset(final DLIDataset item, final RelatedItemInfo relatedItemInfo) { ScholixSummary summary = new ScholixSummary(); summary.setId(item.getId()); if (item.getPid() != null) summary.setLocalIdentifier(item.getPid().stream() .map(p -> new TypedIdentifier(p.getValue(), p.getQualifier().getClassid())) .collect(Collectors.toList()) ); summary.setTypology(Typology.dataset); if (item.getTitle() != null) summary.setTitle(item.getTitle().stream().map(StructuredProperty::getValue).collect(Collectors.toList())); if (item.getAuthor() != null) { summary.setAuthor(item.getAuthor().stream().map(Author::getFullname).collect(Collectors.toList())); } if (item.getRelevantdate() != null) summary.setDate( item.getRelevantdate().stream() .filter(d -> "date".equalsIgnoreCase(d.getQualifier().getClassname())) .map(StructuredProperty::getValue) .collect(Collectors.toList()) ); if (item.getDescription() != null && item.getDescription().size() > 0) summary.setDescription(item.getDescription().get(0).getValue()); if (item.getSubject() != null) { summary.setSubject(item.getSubject().stream() .map(s -> new SchemeValue(s.getQualifier().getClassid(), s.getValue())) .collect(Collectors.toList()) ); } summary.setRelatedDatasets(relatedItemInfo.getRelatedDataset()); summary.setRelatedPublications(relatedItemInfo.getRelatedPublication()); summary.setRelatedUnknown(relatedItemInfo.getRelatedUnknown()); if (item.getDlicollectedfrom() != null) summary.setDatasources(item.getDlicollectedfrom().stream() .map( c -> new CollectedFromType(c.getName(), c.getId(), c.getCompletionStatus()) ).collect(Collectors.toList())); return summary; } private static ScholixSummary summaryFromPublication(final DLIPublication item, final RelatedItemInfo relatedItemInfo) { ScholixSummary summary = new ScholixSummary(); summary.setId(item.getId()); if (item.getPid() != null) summary.setLocalIdentifier(item.getPid().stream() .map(p -> new TypedIdentifier(p.getValue(), p.getQualifier().getClassid())) .collect(Collectors.toList()) ); summary.setTypology(Typology.dataset); if (item.getTitle() != null) summary.setTitle(item.getTitle().stream().map(StructuredProperty::getValue).collect(Collectors.toList())); if (item.getAuthor() != null) { summary.setAuthor(item.getAuthor().stream().map(Author::getFullname).collect(Collectors.toList())); } if (item.getRelevantdate() != null) summary.setDate( item.getRelevantdate().stream() .filter(d -> "date".equalsIgnoreCase(d.getQualifier().getClassname())) .map(StructuredProperty::getValue) .collect(Collectors.toList()) ); if (item.getDescription() != null && item.getDescription().size() > 0) summary.setDescription(item.getDescription().get(0).getValue()); if (item.getSubject() != null) { summary.setSubject(item.getSubject().stream() .map(s -> new SchemeValue(s.getQualifier().getClassid(), s.getValue())) .collect(Collectors.toList()) ); } summary.setRelatedDatasets(relatedItemInfo.getRelatedDataset()); summary.setRelatedPublications(relatedItemInfo.getRelatedPublication()); summary.setRelatedUnknown(relatedItemInfo.getRelatedUnknown()); if (item.getDlicollectedfrom() != null) summary.setDatasources(item.getDlicollectedfrom().stream() .map( c -> new CollectedFromType(c.getName(), c.getId(), c.getCompletionStatus()) ).collect(Collectors.toList())); return summary; } private static ScholixSummary summaryFromUnknown(final DLIUnknown item, final RelatedItemInfo relatedItemInfo) { ScholixSummary summary = new ScholixSummary(); summary.setId(item.getId()); if (item.getPid() != null) summary.setLocalIdentifier(item.getPid().stream() .map(p -> new TypedIdentifier(p.getValue(), p.getQualifier().getClassid())) .collect(Collectors.toList()) ); summary.setRelatedDatasets(relatedItemInfo.getRelatedDataset()); summary.setRelatedPublications(relatedItemInfo.getRelatedPublication()); summary.setRelatedUnknown(relatedItemInfo.getRelatedUnknown()); if (item.getDlicollectedfrom() != null) summary.setDatasources(item.getDlicollectedfrom().stream() .map( c -> new CollectedFromType(c.getName(), c.getId(), c.getCompletionStatus()) ).collect(Collectors.toList())); return summary; } }