Search in sources :

Example 6 with JsonParseException

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
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) Gson(com.google.gson.Gson) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) JsonParser(edu.harvard.iq.dataverse.util.json.JsonParser)

Example 7 with JsonParseException

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) {
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ForeignMetadataFormatMapping(edu.harvard.iq.dataverse.ForeignMetadataFormatMapping) FileNotFoundException(java.io.FileNotFoundException) Gson(com.google.gson.Gson) JsonObject(javax.json.JsonObject) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) IOException(java.io.IOException) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) FileInputStream(java.io.FileInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) EJBException(javax.ejb.EJBException) XMLInputFactory(javax.xml.stream.XMLInputFactory) JsonParser(edu.harvard.iq.dataverse.util.json.JsonParser)

Example 8 with JsonParseException

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) {
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ForeignMetadataFormatMapping(edu.harvard.iq.dataverse.ForeignMetadataFormatMapping) Gson(com.google.gson.Gson) JsonObject(javax.json.JsonObject) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) EJBException(javax.ejb.EJBException) XMLInputFactory(javax.xml.stream.XMLInputFactory) JsonParser(edu.harvard.iq.dataverse.util.json.JsonParser)

Example 9 with JsonParseException

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());
    }
}
Also used : Workflow(edu.harvard.iq.dataverse.workflow.Workflow) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) JsonParser(edu.harvard.iq.dataverse.util.json.JsonParser) POST(javax.ws.rs.POST)

Example 10 with JsonParseException

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();
    }
}
Also used : AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) Dataset(edu.harvard.iq.dataverse.Dataset) CreateDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateDatasetCommand) JsonObject(javax.json.JsonObject) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) Dataverse(edu.harvard.iq.dataverse.Dataverse) LinkedList(java.util.LinkedList) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) EJBException(javax.ejb.EJBException) JsonParsingException(javax.json.stream.JsonParsingException) ConstraintViolationException(javax.validation.ConstraintViolationException) StringReader(java.io.StringReader) JsonParsingException(javax.json.stream.JsonParsingException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)12 StringReader (java.io.StringReader)11 JsonObject (javax.json.JsonObject)11 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)6 JsonParser (edu.harvard.iq.dataverse.util.json.JsonParser)6 JsonReader (javax.json.JsonReader)6 Path (javax.ws.rs.Path)6 Gson (com.google.gson.Gson)5 POST (javax.ws.rs.POST)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 Dataset (edu.harvard.iq.dataverse.Dataset)4 Dataverse (edu.harvard.iq.dataverse.Dataverse)4 EJBException (javax.ejb.EJBException)4 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)3 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)3 CreateDatasetCommand (edu.harvard.iq.dataverse.engine.command.impl.CreateDatasetCommand)3 IOException (java.io.IOException)3 ConstraintViolation (javax.validation.ConstraintViolation)3 GsonBuilder (com.google.gson.GsonBuilder)2 DatasetField (edu.harvard.iq.dataverse.DatasetField)2