Skip to content

Commit

Permalink
fix: use GlobalId methods to create PID URLs/strings in DdiExportUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
vera committed Aug 26, 2024
1 parent b8a068e commit 70031f3
Showing 1 changed file with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import edu.harvard.iq.dataverse.ControlledVocabularyValue;
import edu.harvard.iq.dataverse.DatasetFieldConstant;
import edu.harvard.iq.dataverse.DvObjectContainer;
import edu.harvard.iq.dataverse.GlobalId;
import edu.harvard.iq.dataverse.api.dto.DatasetDTO;
import edu.harvard.iq.dataverse.api.dto.DatasetVersionDTO;
import edu.harvard.iq.dataverse.api.dto.FieldDTO;
Expand Down Expand Up @@ -79,6 +80,10 @@ public class DdiExportUtil {
public static final String NOTE_SUBJECT_CONTENTTYPE = "Content/MIME Type";
public static final String CITATION_BLOCK_NAME = "citation";

//Some tests don't send real PIDs that can be parsed
//Use constant empty PID in these cases
private static final String EMPTY_PID = "null:nullnullnull";

public static String datasetDtoAsJson2ddi(String datasetDtoAsJson) {
Gson gson = new Gson();
DatasetDTO datasetDto = gson.fromJson(datasetDtoAsJson, DatasetDTO.class);
Expand Down Expand Up @@ -162,13 +167,15 @@ private static void createStdyDscr(XMLStreamWriter xmlw, DatasetDTO datasetDto)

String persistentAuthority = datasetDto.getAuthority();
String persistentId = datasetDto.getIdentifier();
String persistentSeparator = datasetDto.getSeparator();

String pid = persistentProtocol + ":" + persistentAuthority + persistentSeparator + persistentId;
String pidUri = pid;
//Some tests don't send real PIDs - don't try to get their URL form
if(!pidUri.equals("null:nullnullnull")) {
pidUri= PidUtil.parseAsGlobalID(persistentProtocol, persistentAuthority, persistentId).asURL();
GlobalId pid = PidUtil.parseAsGlobalID(persistentProtocol, persistentAuthority, persistentId);
String pidUri, pidString;
if(pid != null) {
pidUri = pid.asURL();
pidString = pid.asString();
} else {
pidUri = EMPTY_PID;
pidString = EMPTY_PID;
}
// The "persistentAgency" tag is used for the "agency" attribute of the
// <IDNo> ddi section; back in the DVN3 days we used "handle" and "DOI"
Expand Down Expand Up @@ -198,7 +205,7 @@ private static void createStdyDscr(XMLStreamWriter xmlw, DatasetDTO datasetDto)
writeAttribute(xmlw, "agency", persistentAgency);


xmlw.writeCharacters(pid);
xmlw.writeCharacters(pidString);
xmlw.writeEndElement(); // IDNo
writeOtherIdElement(xmlw, version);
xmlw.writeEndElement(); // titlStmt
Expand Down Expand Up @@ -338,15 +345,21 @@ private static void writeDocDescElement (XMLStreamWriter xmlw, DatasetDTO datase

String persistentAuthority = datasetDto.getAuthority();
String persistentId = datasetDto.getIdentifier();
String persistentSeparator = datasetDto.getSeparator();

GlobalId pid = PidUtil.parseAsGlobalID(persistentProtocol, persistentAuthority, persistentId);
String pidString;
if(pid != null) {
pidString = pid.asString();
} else {
pidString = EMPTY_PID;
}

xmlw.writeStartElement("docDscr");
xmlw.writeStartElement("citation");
xmlw.writeStartElement("titlStmt");
writeFullElement(xmlw, "titl", dto2Primitive(version, DatasetFieldConstant.title), datasetDto.getMetadataLanguage());
xmlw.writeStartElement("IDNo");
writeAttribute(xmlw, "agency", persistentAgency);
xmlw.writeCharacters(persistentProtocol + ":" + persistentAuthority + persistentSeparator + persistentId);
xmlw.writeCharacters(pidString);
xmlw.writeEndElement(); // IDNo
xmlw.writeEndElement(); // titlStmt
xmlw.writeStartElement("distStmt");
Expand Down

0 comments on commit 70031f3

Please sign in to comment.