Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5467 Return useful error message for DDI upload. #7107

Merged
merged 1 commit into from Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/main/java/edu/harvard/iq/dataverse/api/Dataverses.java
Expand Up @@ -102,7 +102,7 @@
import java.util.Arrays;
import java.util.Date;
import java.util.Optional;
import javax.persistence.NoResultException;
import javax.xml.stream.XMLStreamException;

/**
* A REST API for dataverses.
Expand Down Expand Up @@ -325,7 +325,7 @@ public Response importDataset(String jsonBody, @PathParam("identifier") String p
// TODO decide if I merge importddi with import just below (xml and json on same api, instead of 2 api)
@POST
@Path("{identifier}/datasets/:importddi")
public Response importDatasetDdi(String xml, @PathParam("identifier") String parentIdtf, @QueryParam("pid") String pidParam, @QueryParam("release") String releaseParam) throws ImportException {
public Response importDatasetDdi(String xml, @PathParam("identifier") String parentIdtf, @QueryParam("pid") String pidParam, @QueryParam("release") String releaseParam) {
try {
User u = findUserOrDie();
if (!u.isSuperuser()) {
Expand All @@ -335,9 +335,12 @@ public Response importDatasetDdi(String xml, @PathParam("identifier") String par
Dataset ds = null;
try {
ds = jsonParser().parseDataset(importService.ddiToJson(xml));
}
catch (JsonParseException jpe) {
return badRequest("Error parsing datas as Json: "+jpe.getMessage());
} catch (JsonParseException jpe) {
return badRequest("Error parsing data as Json: "+jpe.getMessage());
} catch (ImportException e) {
return badRequest("Invalid DOI found in the XML: "+e.getMessage());
} catch (XMLStreamException e) {
return badRequest("Invalid file content: "+e.getMessage());
}
ds.setOwner(owner);
if (nonEmpty(pidParam)) {
Expand Down
Expand Up @@ -171,12 +171,15 @@ public Map<String, String> mapDDI(ImportType importType, File ddiFile, DatasetD
}

private void processDDI(ImportType importType, XMLStreamReader xmlr, DatasetDTO datasetDTO, Map<String, String> filesMap) throws XMLStreamException, ImportException {

// make sure we have a codeBook
//while ( xmlr.next() == XMLStreamConstants.COMMENT ); // skip pre root comments
xmlr.nextTag();
xmlr.require(XMLStreamConstants.START_ELEMENT, null, "codeBook");

try {
xmlr.nextTag();
xmlr.require(XMLStreamConstants.START_ELEMENT, null, "codeBook");
} catch( XMLStreamException e) {
throw new XMLStreamException("It doesn't start with the XML element <codeBook>");
}

// Some DDIs provide an ID in the <codeBook> section.
// We are going to treat it as just another otherId.
// (we've seen instances where this ID was the only ID found in
Expand Down
Expand Up @@ -390,14 +390,8 @@ public Dataset doImportHarvestedDataset(DataverseRequest dataverseRequest, Harve
return importedDataset;
}

public JsonObject ddiToJson(String xmlToParse) throws ImportException{
DatasetDTO dsDTO = null;

try {
dsDTO = importDDIService.doImport(ImportType.IMPORT, xmlToParse);
} catch (XMLStreamException e) {
throw new ImportException("XMLStreamException" + e);
}
public JsonObject ddiToJson(String xmlToParse) throws ImportException, XMLStreamException {
DatasetDTO dsDTO = importDDIService.doImport(ImportType.IMPORT, xmlToParse);
// convert DTO to Json,
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(dsDTO);
Expand Down