diff --git a/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteRegisterService.java b/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteRegisterService.java index 218e4c85474..f93df7f2725 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteRegisterService.java +++ b/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteRegisterService.java @@ -15,6 +15,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -23,6 +24,8 @@ import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; + +import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -540,34 +543,63 @@ public String generateXML(DvObject dvObject) { private String generateRelatedIdentifiers(DvObject dvObject) { StringBuilder sb = new StringBuilder(); + List relatedIdentifiers = new ArrayList<>(); if (dvObject.isInstanceofDataset()) { Dataset dataset = (Dataset) dvObject; + for (DatasetField field : dataset.getLatestVersion().getDatasetFields()) { + if (field.getDatasetFieldType().getName().equals("publication")) { + for (DatasetFieldCompoundValue compoundValue : field.getDatasetFieldCompoundValues()) { + String publicationCitation = null; + String publicationIDType = null; + String publicationIDNumber = null; + String publicationURL = null; + for (DatasetField child : compoundValue.getChildDatasetFields()) { + switch (child.getDatasetFieldType().getName()) { + case "publicationCitation": publicationCitation = child.getValue(); break; + case "publicationIDType": publicationIDType = child.getValue(); break; + case "publicationIDNumber": publicationIDNumber = child.getValue(); break; + case "publicationURL": publicationURL = child.getValue(); break; + default: break; + } + } + if (StringUtils.isNotBlank(publicationIDType) && StringUtils.isNotBlank(publicationIDNumber)) { + relatedIdentifiers.add(serializeIdenifier(publicationIDType, "Cites", publicationIDNumber)); + } + } + } + } if (!dataset.getFiles().isEmpty() && !(dataset.getFiles().get(0).getIdentifier() == null)) { datafileIdentifiers = new ArrayList<>(); for (DataFile dataFile : dataset.getFiles()) { if (!dataFile.getGlobalId().asString().isEmpty()) { - if (sb.toString().isEmpty()) { - sb.append(""); - } - sb.append("" + dataFile.getGlobalId() + ""); + relatedIdentifiers.add(serializeIdenifier("DOI", "HasPart", dataFile.getGlobalId().toString())); } } - if (!sb.toString().isEmpty()) { - sb.append(""); - } } } else if (dvObject.isInstanceofDataFile()) { DataFile df = (DataFile) dvObject; + relatedIdentifiers.add(serializeIdenifier("DOI", "IsPartOf", df.getOwner().getGlobalId().toString())); + } + + if (!relatedIdentifiers.isEmpty()) { sb.append(""); - sb.append("" + df.getOwner().getGlobalId() + ""); + sb.append(StringUtils.join(relatedIdentifiers, "\n")); sb.append(""); } return sb.toString(); } + private String serializeIdenifier(String schema, String relation, String identifier) { + if (!schema.toUpperCase().equals("DOI")) + identifier = schema.toLowerCase() + ":" + identifier; + return String.format( + "%s", + schema.toUpperCase(), relation, identifier + ); + } + public void generateFileIdentifiers(DvObject dvObject) { if (dvObject.isInstanceofDataset()) {