Search in sources :

Example 1 with UpdateDatasetVersionCommand

use of edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetVersionCommand 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 2 with UpdateDatasetVersionCommand

use of edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetVersionCommand in project dataverse by IQSS.

the class FileRecordWriter method updateDatasetVersion.

// utils
/**
 * Update the dataset version using the command engine so permissions and constraints are enforced.
 * Log errors to both the glassfish log and in the job context, as the exit status "failed".
 *
 * @param version dataset version
 */
private void updateDatasetVersion(DatasetVersion version) {
    // update version using the command engine to enforce user permissions and constraints
    if (dataset.getVersions().size() == 1 && version.getVersionState() == DatasetVersion.VersionState.DRAFT) {
        try {
            Command<DatasetVersion> cmd;
            cmd = new UpdateDatasetVersionCommand(new DataverseRequest(user, (HttpServletRequest) null), version);
            commandEngine.submit(cmd);
        } catch (CommandException ex) {
            String commandError = "CommandException updating DatasetVersion from batch job: " + ex.getMessage();
            getJobLogger().log(Level.SEVERE, commandError);
            jobContext.setExitStatus("FAILED");
        }
    } else {
        String constraintError = "ConstraintException updating DatasetVersion form batch job: dataset must be a " + "single version in draft mode.";
        getJobLogger().log(Level.SEVERE, constraintError);
        jobContext.setExitStatus("FAILED");
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) UpdateDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetVersionCommand)

Aggregations

DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)2 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)2 UpdateDatasetVersionCommand (edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetVersionCommand)2 Dataset (edu.harvard.iq.dataverse.Dataset)1 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)1 CreateDatasetVersionCommand (edu.harvard.iq.dataverse.engine.command.impl.CreateDatasetVersionCommand)1 JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)1 StringReader (java.io.StringReader)1 JsonObject (javax.json.JsonObject)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1