Search in sources :

Example 51 with FileMetadata

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);
    }
}
Also used : PrivateUrlUser(edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser) RoleAssignment(edu.harvard.iq.dataverse.RoleAssignment) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) PrivateUrl(edu.harvard.iq.dataverse.privateurl.PrivateUrl)

Example 52 with FileMetadata

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);
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) Timestamp(java.sql.Timestamp) Date(java.util.Date) ConstraintViolation(javax.validation.ConstraintViolation)

Example 53 with FileMetadata

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);
}
Also used : FileMetadata(edu.harvard.iq.dataverse.FileMetadata)

Example 54 with FileMetadata

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;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) LinkedList(java.util.LinkedList)

Aggregations

FileMetadata (edu.harvard.iq.dataverse.FileMetadata)54 DataFile (edu.harvard.iq.dataverse.DataFile)30 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)26 ArrayList (java.util.ArrayList)23 Dataset (edu.harvard.iq.dataverse.Dataset)18 Test (org.junit.Test)13 Date (java.util.Date)12 IOException (java.io.IOException)10 Timestamp (java.sql.Timestamp)10 DataTable (edu.harvard.iq.dataverse.DataTable)5 DatasetField (edu.harvard.iq.dataverse.DatasetField)5 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)5 MocksFactory.makeDataset (edu.harvard.iq.dataverse.mocks.MocksFactory.makeDataset)5 SimpleDateFormat (java.text.SimpleDateFormat)5 HashMap (java.util.HashMap)5 Dataverse (edu.harvard.iq.dataverse.Dataverse)4 File (java.io.File)4 FileNotFoundException (java.io.FileNotFoundException)4 JsonObjectBuilder (javax.json.JsonObjectBuilder)4 DataFileTag (edu.harvard.iq.dataverse.DataFileTag)3