From 3b67d7be1e544162c340c4b6de63df1c53b8139a Mon Sep 17 00:00:00 2001 From: helkv Date: Thu, 14 Sep 2023 17:10:08 +0200 Subject: [PATCH] #61 Small adaptions + Refactoring - Add check dvObject.isInstanceofDataset() - Refactoring --- .../dataverse/DOIDataCiteRegisterService.java | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteRegisterService.java b/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteRegisterService.java index 5b5786e79d1..1d4387671f6 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteRegisterService.java +++ b/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteRegisterService.java @@ -567,37 +567,54 @@ public String generateXML(DvObject dvObject) { xmlMetadata = xmlMetadata.replace("{$contributors}", contributorsElement.toString()); - xmlMetadata = addGrantNumberMetadata(dvObject, xmlMetadata); + xmlMetadata = this.addFundingReferences(dvObject, xmlMetadata); return xmlMetadata; } - private String addGrantNumberMetadata(DvObject dvObject, String xmlMetadata) { + private String addFundingReferences(DvObject dvObject, String xmlMetadata) { try { - Dataset dataset = (Dataset) dvObject; - List> grantNumberChildValues = extractGrantNumberValues(dataset); - org.w3c.dom.Document xmlDocument = DataCiteMetadataUtil.parseXml(xmlMetadata); - xmlDocument = addGrantNumberMetadata(grantNumberChildValues, xmlDocument); - xmlMetadata = DataCiteMetadataUtil.prettyPrintXML(xmlDocument, 4); + if (dvObject.isInstanceofDataset()) { + Dataset dataset = (Dataset) dvObject; + List> grantNumberChildValues = this.extractGrantNumberValues(dataset); + if (!grantNumberChildValues.isEmpty()) { + org.w3c.dom.Document xmlDocument = DataCiteMetadataUtil.parseXml(xmlMetadata); + xmlDocument = this.appendFundingReferences(grantNumberChildValues, xmlDocument); + xmlMetadata = DataCiteMetadataUtil.prettyPrintXML(xmlDocument, 4); + } + } } catch(Exception e) { - logger.log(Level.SEVERE, "Error adding grantNumber to the DataCite Metadata: {0}", e.getMessage()); + logger.log(Level.SEVERE, "Error adding fundingReferences to the DataCite Metadata: {0}", e.getMessage()); } return xmlMetadata; } - public List> extractGrantNumberValues(Dataset dataset) { + private List> extractGrantNumberValues(Dataset dataset) { List> grantNumberChildValues = new ArrayList<>(); List grantNumberDatasetFields = DataCiteMetadataUtil.searchForFirstLevelDatasetFields(dataset, DatasetFieldConstant.grantNumber); + //There should only be one DatasetField with name 'grantNumber' (Premise: There are values for grantNumber) if(!grantNumberDatasetFields.isEmpty()){ - //There should only be one 'grantNumber' DatasetField - DatasetField datasetField = grantNumberDatasetFields.get(0); - grantNumberChildValues = DataCiteMetadataUtil.extractCompoundValueChildDatasetFieldValues(datasetField); + DatasetField grantNumber = grantNumberDatasetFields.get(0); + grantNumberChildValues = DataCiteMetadataUtil.extractCompoundValueChildDatasetFieldValues(grantNumber); } return grantNumberChildValues; } - public org.w3c.dom.Document addGrantNumberMetadata(List> grantNumberChildValues, org.w3c.dom.Document xmlDocument) { + /** + *
+     * Appends fundingReferences to the DataCite xml.
+     * Mappings:
+     * - grantNumberAgency -> funderName
+     * - grantNumberValue -> awardNumber
+     * 
+ * + * @param grantNumberChildValues + * @param xmlDocument + * @return The xmlDocument with fundingReferences + */ + private org.w3c.dom.Document appendFundingReferences(List> grantNumberChildValues, org.w3c.dom.Document xmlDocument) { for (Map childValue : grantNumberChildValues) { + // funderName (=grantNumberAgency) is a required subfield of fundingReference if (childValue.containsKey(DatasetFieldConstant.grantNumberAgency)) { if(xmlDocument.getElementsByTagName("fundingReferences").getLength() == 0){ DataCiteMetadataUtil.appendElementToDocument(xmlDocument, "resource", "fundingReferences", null); @@ -786,7 +803,7 @@ public static org.w3c.dom.Document parseXml(String xml) throws ParserConfigurati } /** - * Append Element to the last parent element. + * Append Element to the last parent element in order. * * @param document * @param parentTagName