Search in sources :

Example 41 with DatasetVersion

use of edu.harvard.iq.dataverse.DatasetVersion in project dataverse by IQSS.

the class ExportService method exportFormat.

// This method finds the exporter for the format requested,
// then produces the dataset metadata as a JsonObject, then calls
// the "cacheExport()" method that will save the produced output
// in a file in the dataset directory.
public void exportFormat(Dataset dataset, String formatName) throws ExportException {
    try {
        Iterator<Exporter> exporters = loader.iterator();
        while (exporters.hasNext()) {
            Exporter e = exporters.next();
            if (e.getProviderName().equals(formatName)) {
                DatasetVersion releasedVersion = dataset.getReleasedVersion();
                if (releasedVersion == null) {
                    throw new IllegalStateException("No Released Version");
                }
                JsonPrinter jsonPrinter = new JsonPrinter(settingsService);
                final JsonObjectBuilder datasetAsJsonBuilder = jsonPrinter.jsonAsDatasetDto(releasedVersion);
                cacheExport(releasedVersion, formatName, datasetAsJsonBuilder.build(), e);
            }
        }
    } catch (ServiceConfigurationError serviceError) {
        throw new ExportException("Service configuration error during export. " + serviceError.getMessage());
    } catch (IllegalStateException e) {
        throw new ExportException("No published version found during export. " + dataset.getGlobalId());
    }
}
Also used : DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) ServiceConfigurationError(java.util.ServiceConfigurationError) Exporter(edu.harvard.iq.dataverse.export.spi.Exporter) JsonObjectBuilder(javax.json.JsonObjectBuilder) JsonPrinter(edu.harvard.iq.dataverse.util.json.JsonPrinter)

Example 42 with DatasetVersion

use of edu.harvard.iq.dataverse.DatasetVersion in project dataverse by IQSS.

the class UpdateDatasetVersionCommand method execute.

@Override
public DatasetVersion execute(CommandContext ctxt) throws CommandException {
    Dataset ds = newVersion.getDataset();
    ctxt.permissions().checkEditDatasetLock(ds, getRequest(), this);
    DatasetVersion latest = ds.getLatestVersion();
    if (latest == null) {
        throw new IllegalCommandException("Dataset " + ds.getId() + " does not have a latest version.", this);
    }
    if (!latest.isDraft()) {
        throw new IllegalCommandException("Cannot update a dataset version that's not a draft", this);
    }
    DatasetVersion edit = ds.getEditVersion();
    edit.setDatasetFields(newVersion.getDatasetFields());
    edit.setDatasetFields(edit.initDatasetFields());
    Set<ConstraintViolation> constraintViolations = edit.validate();
    if (!constraintViolations.isEmpty()) {
        String validationFailedString = "Validation failed:";
        for (ConstraintViolation constraintViolation : constraintViolations) {
            validationFailedString += " " + constraintViolation.getMessage();
        }
        throw new IllegalCommandException(validationFailedString, this);
    }
    Iterator<DatasetField> dsfIt = edit.getDatasetFields().iterator();
    while (dsfIt.hasNext()) {
        if (dsfIt.next().removeBlankDatasetFieldValues()) {
            dsfIt.remove();
        }
    }
    Timestamp now = new Timestamp(new Date().getTime());
    edit.setLastUpdateTime(now);
    ds.setModificationTime(now);
    DatasetVersion managed = ctxt.em().merge(edit);
    boolean doNormalSolrDocCleanUp = true;
    ctxt.index().indexDataset(managed.getDataset(), doNormalSolrDocCleanUp);
    return managed;
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) Dataset(edu.harvard.iq.dataverse.Dataset) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) ConstraintViolation(javax.validation.ConstraintViolation) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 43 with DatasetVersion

use of edu.harvard.iq.dataverse.DatasetVersion in project dataverse by IQSS.

the class Datasets method getDataset.

@GET
@Path("{id}")
public Response getDataset(@PathParam("id") String id) {
    return response(req -> {
        final Dataset retrieved = execCommand(new GetDatasetCommand(req, findDatasetOrDie(id)));
        final DatasetVersion latest = execCommand(new GetLatestAccessibleDatasetVersionCommand(req, retrieved));
        final JsonObjectBuilder jsonbuilder = json(retrieved);
        return allowCors(ok(jsonbuilder.add("latestVersion", (latest != null) ? json(latest) : null)));
    });
}
Also used : GetLatestAccessibleDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.GetLatestAccessibleDatasetVersionCommand) Dataset(edu.harvard.iq.dataverse.Dataset) GetDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.GetDatasetCommand) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonObjectBuilder(javax.json.JsonObjectBuilder) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 44 with DatasetVersion

use of edu.harvard.iq.dataverse.DatasetVersion in project dataverse by IQSS.

the class Datasets method updateDraftVersion.

@PUT
@Path("{id}/versions/{versionId}")
public Response updateDraftVersion(String jsonBody, @PathParam("id") String id, @PathParam("versionId") String versionId) {
    if (!":draft".equals(versionId)) {
        return error(Response.Status.BAD_REQUEST, "Only the :draft version can be updated");
    }
    try (StringReader rdr = new StringReader(jsonBody)) {
        DataverseRequest req = createDataverseRequest(findUserOrDie());
        Dataset ds = findDatasetOrDie(id);
        JsonObject json = Json.createReader(rdr).readObject();
        DatasetVersion incomingVersion = jsonParser().parseDatasetVersion(json);
        // clear possibly stale fields from the incoming dataset version.
        // creation and modification dates are updated by the commands.
        incomingVersion.setId(null);
        incomingVersion.setVersionNumber(null);
        incomingVersion.setMinorVersionNumber(null);
        incomingVersion.setVersionState(DatasetVersion.VersionState.DRAFT);
        incomingVersion.setDataset(ds);
        incomingVersion.setCreateTime(null);
        incomingVersion.setLastUpdateTime(null);
        boolean updateDraft = ds.getLatestVersion().isDraft();
        DatasetVersion managedVersion = execCommand(updateDraft ? new UpdateDatasetVersionCommand(req, incomingVersion) : new CreateDatasetVersionCommand(req, ds, incomingVersion));
        return ok(json(managedVersion));
    } catch (JsonParseException ex) {
        logger.log(Level.SEVERE, "Semantic error parsing dataset version Json: " + ex.getMessage(), ex);
        return error(Response.Status.BAD_REQUEST, "Error parsing dataset version: " + ex.getMessage());
    } catch (WrappedResponse ex) {
        return ex.getResponse();
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) CreateDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateDatasetVersionCommand) Dataset(edu.harvard.iq.dataverse.Dataset) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) UpdateDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetVersionCommand) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 45 with DatasetVersion

use of edu.harvard.iq.dataverse.DatasetVersion 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)

Aggregations

DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)85 Dataset (edu.harvard.iq.dataverse.Dataset)43 Test (org.junit.Test)36 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)25 ArrayList (java.util.ArrayList)24 DataFile (edu.harvard.iq.dataverse.DataFile)19 JsonObject (javax.json.JsonObject)15 Date (java.util.Date)13 StringReader (java.io.StringReader)11 IOException (java.io.IOException)10 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)9 Timestamp (java.sql.Timestamp)9 EJBException (javax.ejb.EJBException)9 JsonParser (edu.harvard.iq.dataverse.util.json.JsonParser)8 JsonObjectBuilder (javax.json.JsonObjectBuilder)8 JsonReader (javax.json.JsonReader)8 DatasetField (edu.harvard.iq.dataverse.DatasetField)7 Dataverse (edu.harvard.iq.dataverse.Dataverse)7 RoleAssignment (edu.harvard.iq.dataverse.RoleAssignment)7 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)7