use of edu.harvard.iq.dataverse.util.json.JsonParseException in project dataverse by IQSS.
the class ImportGenericServiceBean method importDCTerms.
public void importDCTerms(String xmlToParse, DatasetVersion datasetVersion, DatasetFieldServiceBean datasetFieldSvc, MetadataBlockServiceBean blockService, SettingsServiceBean settingsService) {
DatasetDTO datasetDTO = this.initializeDataset();
try {
// Read docDescr and studyDesc into DTO objects.
Map<String, String> fileMap = mapDCTerms(xmlToParse, datasetDTO);
//
// convert DTO to Json,
Gson gson = new Gson();
String json = gson.toJson(datasetDTO.getDatasetVersion());
JsonReader jsonReader = Json.createReader(new StringReader(json));
JsonObject obj = jsonReader.readObject();
// and call parse Json to read it into a datasetVersion
DatasetVersion dv = new JsonParser(datasetFieldSvc, blockService, settingsService).parseDatasetVersion(obj, datasetVersion);
} catch (XMLStreamException | JsonParseException e) {
// EMK TODO: exception handling
e.printStackTrace();
}
// EMK TODO: Call methods for reading FileMetadata and related objects from xml, return list of FileMetadata objects.
/*try {
Map<String, DataTable> dataTableMap = new DataTableImportDDI().processDataDscr(xmlr);
} catch(Exception e) {
}*/
// Save Dataset and DatasetVersion in database
}
use of edu.harvard.iq.dataverse.util.json.JsonParseException in project dataverse by IQSS.
the class ImportGenericServiceBean method importXML.
public void importXML(File xmlFile, String foreignFormat, DatasetVersion datasetVersion) {
FileInputStream in = null;
XMLStreamReader xmlr = null;
// look up the foreign metadata mapping for this format:
ForeignMetadataFormatMapping mappingSupported = findFormatMappingByName(foreignFormat);
if (mappingSupported == null) {
throw new EJBException("Unknown/unsupported foreign metadata format " + foreignFormat);
}
try {
in = new FileInputStream(xmlFile);
XMLInputFactory xmlFactory = javax.xml.stream.XMLInputFactory.newInstance();
xmlr = xmlFactory.createXMLStreamReader(in);
DatasetDTO datasetDTO = processXML(xmlr, mappingSupported);
Gson gson = new Gson();
String json = gson.toJson(datasetDTO.getDatasetVersion());
logger.info("Json:\n" + json);
JsonReader jsonReader = Json.createReader(new StringReader(json));
JsonObject obj = jsonReader.readObject();
DatasetVersion dv = new JsonParser(datasetFieldSvc, blockService, settingsService).parseDatasetVersion(obj, datasetVersion);
} catch (FileNotFoundException ex) {
// Logger.getLogger("global").log(Level.SEVERE, null, ex);
throw new EJBException("ERROR occurred in mapDDI: File Not Found!");
} catch (XMLStreamException ex) {
// Logger.getLogger("global").log(Level.SEVERE, null, ex);
throw new EJBException("ERROR occurred while parsing XML (file " + xmlFile.getAbsolutePath() + "); ", ex);
} catch (JsonParseException ex) {
Logger.getLogger(ImportGenericServiceBean.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (xmlr != null) {
xmlr.close();
}
} catch (XMLStreamException ex) {
}
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
}
}
}
use of edu.harvard.iq.dataverse.util.json.JsonParseException in project dataverse by IQSS.
the class ImportGenericServiceBean method importXML.
public void importXML(String xmlToParse, String foreignFormat, DatasetVersion datasetVersion) {
StringReader reader = null;
XMLStreamReader xmlr = null;
ForeignMetadataFormatMapping mappingSupported = findFormatMappingByName(foreignFormat);
if (mappingSupported == null) {
throw new EJBException("Unknown/unsupported foreign metadata format " + foreignFormat);
}
try {
reader = new StringReader(xmlToParse);
XMLInputFactory xmlFactory = javax.xml.stream.XMLInputFactory.newInstance();
xmlr = xmlFactory.createXMLStreamReader(reader);
DatasetDTO datasetDTO = processXML(xmlr, mappingSupported);
Gson gson = new Gson();
String json = gson.toJson(datasetDTO.getDatasetVersion());
logger.fine(json);
JsonReader jsonReader = Json.createReader(new StringReader(json));
JsonObject obj = jsonReader.readObject();
DatasetVersion dv = new JsonParser(datasetFieldSvc, blockService, settingsService).parseDatasetVersion(obj, datasetVersion);
} catch (XMLStreamException ex) {
// Logger.getLogger("global").log(Level.SEVERE, null, ex);
throw new EJBException("ERROR occurred while parsing XML fragment (" + xmlToParse.substring(0, 64) + "...); ", ex);
} catch (JsonParseException ex) {
Logger.getLogger(ImportGenericServiceBean.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (xmlr != null) {
xmlr.close();
}
} catch (XMLStreamException ex) {
}
}
}
use of edu.harvard.iq.dataverse.util.json.JsonParseException in project dataverse by IQSS.
the class WorkflowsAdmin method addWorkflow.
@POST
public Response addWorkflow(JsonObject jsonWorkflow) {
JsonParser jp = new JsonParser();
try {
Workflow wf = jp.parseWorkflow(jsonWorkflow);
Workflow managedWf = workflows.save(wf);
return created("/admin/workflows/" + managedWf.getId(), json(managedWf));
} catch (JsonParseException ex) {
return badRequest("Can't parse Json: " + ex.getMessage());
}
}
use of edu.harvard.iq.dataverse.util.json.JsonParseException in project dataverse by IQSS.
the class Dataverses method createDataset.
@POST
@Path("{identifier}/datasets")
public Response createDataset(String jsonBody, @PathParam("identifier") String parentIdtf) {
try {
User u = findUserOrDie();
Dataverse owner = findDataverseOrDie(parentIdtf);
JsonObject json;
try (StringReader rdr = new StringReader(jsonBody)) {
json = Json.createReader(rdr).readObject();
} catch (JsonParsingException jpe) {
LOGGER.log(Level.SEVERE, "Json: {0}", jsonBody);
return error(Status.BAD_REQUEST, "Error parsing Json: " + jpe.getMessage());
}
Dataset ds = new Dataset();
ds.setOwner(owner);
JsonObject jsonVersion = json.getJsonObject("datasetVersion");
if (jsonVersion == null) {
return error(Status.BAD_REQUEST, "Json POST data are missing datasetVersion object.");
}
try {
try {
DatasetVersion version = new DatasetVersion();
version.setDataset(ds);
// Use the two argument version so that the version knows which dataset it's associated with.
version = jsonParser().parseDatasetVersion(jsonVersion, version);
// force "initial version" properties
version.setMinorVersionNumber(null);
version.setVersionNumber(null);
version.setVersionState(DatasetVersion.VersionState.DRAFT);
LinkedList<DatasetVersion> versions = new LinkedList<>();
versions.add(version);
version.setDataset(ds);
ds.setVersions(versions);
} catch (javax.ejb.TransactionRolledbackLocalException rbe) {
throw rbe.getCausedByException();
}
} catch (JsonParseException ex) {
LOGGER.log(Level.INFO, "Error parsing dataset version from Json", ex);
return error(Status.BAD_REQUEST, "Error parsing datasetVersion: " + ex.getMessage());
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error parsing dataset version from Json", e);
return error(Status.INTERNAL_SERVER_ERROR, "Error parsing datasetVersion: " + e.getMessage());
}
Dataset managedDs = execCommand(new CreateDatasetCommand(ds, createDataverseRequest(u)));
return created("/datasets/" + managedDs.getId(), Json.createObjectBuilder().add("id", managedDs.getId()).add("persistentId", managedDs.getGlobalId()));
} catch (WrappedResponse ex) {
return ex.getResponse();
}
}
Aggregations