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());
}
}
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;
}
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)));
});
}
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();
}
}
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
}
Aggregations