partial implementation of xml serialization

This commit is contained in:
Michele Artini 2024-12-06 09:41:19 +01:00
parent 6540ad2f4e
commit 411ab9b27f
1 changed files with 67 additions and 35 deletions

View File

@ -6,8 +6,12 @@ import org.dom4j.Element;
import org.dom4j.Namespace; import org.dom4j.Namespace;
import org.dom4j.QName; import org.dom4j.QName;
import eu.dnetlib.dhp.schema.common.ModelConstants;
import eu.dnetlib.dhp.schema.solr.BestAccessRight; 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.Language;
import eu.dnetlib.dhp.schema.solr.RecordType;
import eu.dnetlib.dhp.schema.solr.RelatedRecord;
import eu.dnetlib.dhp.schema.solr.SolrRecord; import eu.dnetlib.dhp.schema.solr.SolrRecord;
// @formatter:off // @formatter:off
@ -32,7 +36,7 @@ public class XMLSolrSerializer {
header.addAttribute(new QName("objIdentifier", DRI_NS), sr.getHeader().getId()); header.addAttribute(new QName("objIdentifier", DRI_NS), sr.getHeader().getId());
header.addAttribute(new QName("dateOfCollection", DRI_NS), ""); header.addAttribute(new QName("dateOfCollection", DRI_NS), "");
header.addAttribute(new QName("dateOfTransformation", 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) { 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) { private static void populateResultFields(final SolrRecord sr, final Element fields) {
if (sr.getCollectedfrom() != null) { if (sr.getCollectedfrom() != null) {
@ -57,11 +62,11 @@ public class XMLSolrSerializer {
sr.getHeader().getOriginalId().forEach(id -> fields.addElement("originalId").addText(id)); 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) { if (sr.getResult().getBestaccessright() != null) {
final BestAccessRight r = sr.getResult().getBestaccessright(); 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) { if (sr.getResult().getAuthor() != null) {
@ -78,11 +83,11 @@ public class XMLSolrSerializer {
if (sr.getResult().getLanguage() != null) { if (sr.getResult().getLanguage() != null) {
final Language l = sr.getResult().getLanguage(); 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) { 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) { if (sr.getContext() != null) {
@ -113,41 +118,25 @@ public class XMLSolrSerializer {
datainfo.addElement("deletedbyinference").setText("false"); datainfo.addElement("deletedbyinference").setText("false");
datainfo.addElement("trust").setText("0.9"); datainfo.addElement("trust").setText("0.9");
datainfo.addElement("inferenceprovenance").setText(""); 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
/* /*
<rels>
#foreach($link in $!pub.linksToProjects)
#set( $info = $!util.calculateProjectInfo($!link) )
<rel inferred="false" trust="0.9" inferenceprovenance="" provenanceaction="user:claim">
<to class="isProducedBy" scheme="dnet:result_project_relations" type="project">$!esc.evaluate($!info.id)</to>
<code>$!esc.evaluate($!info.code)</code>
<acronym>$!esc.evaluate($!info.acronym)</acronym>
<title>$!esc.evaluate($!info.title)</title>
<contracttype classid="" classname="" schemeid="" schemename=""/>
<funding>
<funder id="$!esc.evaluate($!info.funderId)"
shortname="$!esc.evaluate($!info.funderShortName)"
name="$!esc.evaluate($!info.funderName)"
jurisdiction="$!esc.evaluate($!info.jurisdiction)"/>
#if($!info.fundingId)
<funding_level_0 name="$!esc.evaluate($!info.fundingName)">$!esc.evaluate($!info.fundingId)</funding_level_0>
#end
</funding>
<websiteurl/>
</rel>
#end
</rels>
*/
//TODO add instances
/*
<children> <children>
<instance> <instance>
@ -164,8 +153,51 @@ public class XMLSolrSerializer {
</children> </children>
</result> </result>
*/ */
}
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) { private static void addStructuredField(final Element parent, final String fieldName, final String classid, final String scheme, final String value) {