From 411ab9b27f5adeb0583476638c1ea619238f487d Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Fri, 6 Dec 2024 09:41:19 +0100 Subject: [PATCH] partial implementation of xml serialization --- .../directindex/solr/XMLSolrSerializer.java | 102 ++++++++++++------ 1 file changed, 67 insertions(+), 35 deletions(-) diff --git a/src/main/java/eu/dnetlib/app/directindex/solr/XMLSolrSerializer.java b/src/main/java/eu/dnetlib/app/directindex/solr/XMLSolrSerializer.java index 7f9b498..0b2e64f 100644 --- a/src/main/java/eu/dnetlib/app/directindex/solr/XMLSolrSerializer.java +++ b/src/main/java/eu/dnetlib/app/directindex/solr/XMLSolrSerializer.java @@ -6,8 +6,12 @@ import org.dom4j.Element; import org.dom4j.Namespace; import org.dom4j.QName; +import eu.dnetlib.dhp.schema.common.ModelConstants; import eu.dnetlib.dhp.schema.solr.BestAccessRight; +import eu.dnetlib.dhp.schema.solr.Instance; import eu.dnetlib.dhp.schema.solr.Language; +import eu.dnetlib.dhp.schema.solr.RecordType; +import eu.dnetlib.dhp.schema.solr.RelatedRecord; import eu.dnetlib.dhp.schema.solr.SolrRecord; // @formatter:off @@ -32,7 +36,7 @@ public class XMLSolrSerializer { header.addAttribute(new QName("objIdentifier", DRI_NS), sr.getHeader().getId()); header.addAttribute(new QName("dateOfCollection", DRI_NS), ""); header.addAttribute(new QName("dateOfTransformation", DRI_NS), ""); - header.addAttribute(new QName("status", DRI_NS), "under curation"); + header.addAttribute(new QName("status", DRI_NS), sr.getHeader().getStatus().name()); } private static void poulateMetadata(final SolrRecord sr, final Element metadata) { @@ -43,6 +47,7 @@ public class XMLSolrSerializer { } } + @SuppressWarnings("deprecation") private static void populateResultFields(final SolrRecord sr, final Element fields) { if (sr.getCollectedfrom() != null) { @@ -57,11 +62,11 @@ public class XMLSolrSerializer { sr.getHeader().getOriginalId().forEach(id -> fields.addElement("originalId").addText(id)); } - addStructuredField(fields, "title", "main title", "dnet:dataCite_title", sr.getResult().getMaintitle()); + addStructuredField(fields, "title", "main title", ModelConstants.DNET_DATACITE_TITLE, sr.getResult().getMaintitle()); if (sr.getResult().getBestaccessright() != null) { final BestAccessRight r = sr.getResult().getBestaccessright(); - addStructuredField(fields, "bestaccessright", r.getCode() , r.getLabel(), "dnet:access_modes", null); + addStructuredField(fields, "bestaccessright", r.getCode() , r.getLabel(), ModelConstants.DNET_ACCESS_MODES, null); } if (sr.getResult().getAuthor() != null) { @@ -78,11 +83,11 @@ public class XMLSolrSerializer { if (sr.getResult().getLanguage() != null) { final Language l = sr.getResult().getLanguage(); - addStructuredField(fields, "language", l.getCode(), l.getLabel(), "dnet:languages", null); + addStructuredField(fields, "language", l.getCode(), l.getLabel(), ModelConstants.DNET_LANGUAGES, null); } if (sr.getResult().getResulttype() != null) { - addStructuredField(fields, "resulttype", sr.getResult().getResulttype(), "dnet:result_typologies", null); + addStructuredField(fields, "resulttype", sr.getResult().getResulttype(), ModelConstants.DNET_RESULT_TYPOLOGIES, null); } if (sr.getContext() != null) { @@ -113,41 +118,25 @@ public class XMLSolrSerializer { datainfo.addElement("deletedbyinference").setText("false"); datainfo.addElement("trust").setText("0.9"); datainfo.addElement("inferenceprovenance").setText(""); - addStructuredField(datainfo, "provenanceaction", "user:insert", "dnet:provenanceActions", null); + addStructuredField(datainfo, "provenanceaction", "user:insert", ModelConstants.DNET_PROVENANCE_ACTIONS, null); - //TODO add project rels + final Element relsNode = fields.addElement("rels"); + if (sr.getLinks() != null) { + sr.getLinks().forEach(p -> addRelProject(relsNode, p)); + } + + final Element childrenNode = fields.addElement("children"); + if (sr.getResult().getInstance() != null) { + sr.getResult().getInstance().forEach(p -> addResultInstance(childrenNode, p)); + } + } + + private static void addResultInstance(final Element childrenNode, final Instance p) { + // TODO Auto-generated method stub /* - - #foreach($link in $!pub.linksToProjects) - - #set( $info = $!util.calculateProjectInfo($!link) ) - - $!esc.evaluate($!info.id) - $!esc.evaluate($!info.code) - $!esc.evaluate($!info.acronym) - $!esc.evaluate($!info.title) - - - - - #if($!info.fundingId) - $!esc.evaluate($!info.fundingId) - #end - - - - #end - - - */ - //TODO add instances - /* @@ -164,8 +153,51 @@ public class XMLSolrSerializer { */ + } + + private static void addRelProject(final Element relsNode, final RelatedRecord p) { + + if (p.getHeader().getRelatedRecordType() != RecordType.project) { + return; + } + + final Element rel = relsNode.addElement("rel"); + + rel.addAttribute("inferred", "false"); + rel.addAttribute("trust", "0.9"); + rel.addAttribute("inferenceprovenance", ""); + rel.addAttribute("provenanceaction", ModelConstants.USER_CLAIM); + + final Element to = rel.addElement("to"); + to.addAttribute("class", ModelConstants.IS_PRODUCED_BY); + to.addAttribute("scheme", "dnet:result_project_relations"); + to.addAttribute("type", RecordType.project.name()); + to.addText(p.getHeader().getRelatedIdentifier()); + + rel.addElement("code").addText(p.getCode()); + rel.addElement("acronym").addText(p.getAcronym()); + rel.addElement("title").addText(p.getProjectTitle()); + + if (p.getFunding() != null) { + final Element funding = rel.addElement("funding"); + + if (p.getFunding().getFunder() != null) { + final Element funder = funding.addElement("funder"); + funder.addAttribute("id", p.getFunding().getFunder().getId()); + funder.addAttribute("name", p.getFunding().getFunder().getName()); + funder.addAttribute("shortname", p.getFunding().getFunder().getShortname()); + if (p.getFunding().getFunder().getJurisdiction() != null) { + funder.addAttribute("jurisdiction", p.getFunding().getFunder().getJurisdiction().getCode()); + } + } + if (p.getFunding().getLevel0() != null) { + final Element level0 = funding.addElement("funding_level_0"); + level0.addAttribute("name", p.getFunding().getLevel0().getName()); + level0.addText(p.getFunding().getLevel0().getId()); + } + } } private static void addStructuredField(final Element parent, final String fieldName, final String classid, final String scheme, final String value) {