use of edu.harvard.iq.dataverse.FileMetadata in project dataverse by IQSS.
the class DeleteDatasetVersionCommand method executeImpl.
@Override
protected void executeImpl(CommandContext ctxt) throws CommandException {
ctxt.permissions().checkEditDatasetLock(doomed, getRequest(), this);
// if you are deleting a dataset that only has 1 draft, we are actually destroying the dataset
if (doomed.getVersions().size() == 1) {
ctxt.engine().submit(new DestroyDatasetCommand(doomed, getRequest()));
} else {
// we are only deleting a version
// todo: for now, it's only the latest and if it's a draft
// but we should add the ability to destroy a specific version
DatasetVersion doomedVersion = doomed.getLatestVersion();
if (doomedVersion.isDraft()) {
Long versionId = doomedVersion.getId();
// files
Iterator<FileMetadata> fmIt = doomedVersion.getFileMetadatas().iterator();
while (fmIt.hasNext()) {
FileMetadata fmd = fmIt.next();
if (!fmd.getDataFile().isReleased()) {
// if file is draft (ie. new to this version, delete
// and remove fileMetadata from list (so that it won't try to merge)
ctxt.engine().submit(new DeleteDataFileCommand(fmd.getDataFile(), getRequest()));
fmIt.remove();
}
}
DatasetVersion doomedAndMerged = ctxt.em().merge(doomedVersion);
ctxt.em().remove(doomedAndMerged);
// remove version from ds obj before indexing....
Iterator<DatasetVersion> dvIt = doomed.getVersions().iterator();
while (dvIt.hasNext()) {
DatasetVersion dv = dvIt.next();
if (versionId.equals(dv.getId())) {
dvIt.remove();
}
}
/**
* DeleteDatasetDraft, which is required by this command,
* DeleteDatasetVersionCommand is not sufficient for running
* GetPrivateUrlCommand nor DeletePrivateUrlCommand, both of
* which require ManageDatasetPermissions because
* DeletePrivateUrlCommand calls RevokeRoleCommand which
* requires ManageDatasetPermissions when executed on a dataset
* so we make direct calls to the service bean so that a lowly
* Contributor who does NOT have ManageDatasetPermissions can
* still successfully delete a Private URL.
*/
PrivateUrl privateUrl = ctxt.privateUrl().getPrivateUrlFromDatasetId(doomed.getId());
if (privateUrl != null) {
logger.fine("Deleting Private URL for dataset id " + doomed.getId());
PrivateUrlUser privateUrlUser = new PrivateUrlUser(doomed.getId());
List<RoleAssignment> roleAssignments = ctxt.roles().directRoleAssignments(privateUrlUser, doomed);
for (RoleAssignment roleAssignment : roleAssignments) {
ctxt.roles().revoke(roleAssignment);
}
}
boolean doNormalSolrDocCleanUp = true;
ctxt.index().indexDataset(doomed, doNormalSolrDocCleanUp);
return;
}
throw new IllegalCommandException("Cannot delete a released version", this);
}
}
use of edu.harvard.iq.dataverse.FileMetadata in project dataverse by IQSS.
the class CreateDatasetVersionCommand method execute.
@Override
public DatasetVersion execute(CommandContext ctxt) throws CommandException {
DatasetVersion latest = dataset.getLatestVersion();
if (latest.isWorkingCopy()) {
// before throwing an Exception
if (newVersion.getVersionState().equals(VersionState.DRAFT)) {
throw new IllegalCommandException("Latest version is already a draft. Cannot add another draft", this);
}
}
newVersion.setDataset(dataset);
newVersion.setDatasetFields(newVersion.initDatasetFields());
Set<ConstraintViolation> constraintViolations = newVersion.validate();
if (!constraintViolations.isEmpty()) {
String validationFailedString = "Validation failed:";
for (ConstraintViolation constraintViolation : constraintViolations) {
validationFailedString += " " + constraintViolation.getMessage();
}
throw new IllegalCommandException(validationFailedString, this);
}
Iterator<DatasetField> dsfIt = newVersion.getDatasetFields().iterator();
while (dsfIt.hasNext()) {
if (dsfIt.next().removeBlankDatasetFieldValues()) {
dsfIt.remove();
}
}
Iterator<DatasetField> dsfItSort = newVersion.getDatasetFields().iterator();
while (dsfItSort.hasNext()) {
dsfItSort.next().setValueDisplayOrder();
}
List<FileMetadata> newVersionMetadatum = new ArrayList<>(latest.getFileMetadatas().size());
for (FileMetadata fmd : latest.getFileMetadatas()) {
FileMetadata fmdCopy = fmd.createCopy();
fmdCopy.setDatasetVersion(newVersion);
newVersionMetadatum.add(fmdCopy);
}
newVersion.setFileMetadatas(newVersionMetadatum);
Timestamp now = new Timestamp(new Date().getTime());
newVersion.setCreateTime(now);
newVersion.setLastUpdateTime(now);
dataset.setModificationTime(now);
newVersion.setDataset(dataset);
final List<DatasetVersion> currentVersions = dataset.getVersions();
ArrayList<DatasetVersion> dsvs = new ArrayList<>(currentVersions.size());
dsvs.addAll(currentVersions);
dsvs.add(0, newVersion);
dataset.setVersions(dsvs);
// ctxt.index().indexDataset(dataset);
return ctxt.datasets().storeVersion(newVersion);
}
use of edu.harvard.iq.dataverse.FileMetadata in project dataverse by IQSS.
the class OptionalFileParams method addOptionalParams.
/**
* Add parameters to a DataFile object
*/
public void addOptionalParams(DataFile df) throws DataFileTagException {
if (df == null) {
throw new NullPointerException("The datafile cannot be null!");
}
FileMetadata fm = df.getFileMetadata();
// ---------------------------
if (hasDescription()) {
fm.setDescription(this.getDescription());
}
// ---------------------------
// Add categories
// ---------------------------
addCategoriesToDataFile(fm);
// ---------------------------
// Add DataFileTags
// ---------------------------
addFileDataTagsToFile(df);
}
use of edu.harvard.iq.dataverse.FileMetadata in project dataverse by IQSS.
the class JsonParser method parseFiles.
public List<FileMetadata> parseFiles(JsonArray metadatasJson, DatasetVersion dsv) throws JsonParseException {
List<FileMetadata> fileMetadatas = new LinkedList<>();
if (metadatasJson != null) {
for (JsonObject filemetadataJson : metadatasJson.getValuesAs(JsonObject.class)) {
String label = filemetadataJson.getString("label");
String directoryLabel = filemetadataJson.getString("directoryLabel", null);
String description = filemetadataJson.getString("description", null);
FileMetadata fileMetadata = new FileMetadata();
fileMetadata.setLabel(label);
fileMetadata.setDirectoryLabel(directoryLabel);
fileMetadata.setDescription(description);
fileMetadata.setDatasetVersion(dsv);
DataFile dataFile = parseDataFile(filemetadataJson.getJsonObject("dataFile"));
fileMetadata.setDataFile(dataFile);
dataFile.getFileMetadatas().add(fileMetadata);
dataFile.setOwner(dsv.getDataset());
if (dsv.getDataset().getFiles() == null) {
dsv.getDataset().setFiles(new ArrayList<>());
}
dsv.getDataset().getFiles().add(dataFile);
fileMetadatas.add(fileMetadata);
fileMetadata.setCategories(getCategories(filemetadataJson, dsv.getDataset()));
}
}
return fileMetadatas;
}
Aggregations