Search in sources :

Example 46 with FileMetadata

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

the class IngestUtilTest method testRecalculateDatasetVersionUNF.

@Test
public void testRecalculateDatasetVersionUNF() {
    IngestUtil.recalculateDatasetVersionUNF(null);
    DatasetVersion dsvNoFile = new DatasetVersion();
    IngestUtil.recalculateDatasetVersionUNF(dsvNoFile);
    assertEquals(null, dsvNoFile.getUNF());
    List<Dataset> datasets = new ArrayList<>();
    Dataset dataset = new Dataset();
    dataset.setProtocol("doi");
    dataset.setAuthority("fakeAuthority");
    dataset.setIdentifier("12345");
    DatasetVersion dsv1 = new DatasetVersion();
    dsv1.setDataset(dataset);
    dsv1.setId(42l);
    dsv1.setVersionState(DatasetVersion.VersionState.DRAFT);
    List<DatasetVersion> datasetVersions = new ArrayList<>();
    datasetVersions.add(dsv1);
    DataFile datafile1 = new DataFile("application/octet-stream");
    DataTable dataTable = new DataTable();
    dataTable.setUnf("unfOnDataTable");
    datafile1.setDataTable(dataTable);
    assertEquals(true, datafile1.isTabularData());
    FileMetadata fmd1 = new FileMetadata();
    fmd1.setId(1L);
    fmd1.setLabel("datafile1.txt");
    fmd1.setDataFile(datafile1);
    datafile1.getFileMetadatas().add(fmd1);
    dsv1.getFileMetadatas().add(fmd1);
    fmd1.setDatasetVersion(dsv1);
    dataset.setVersions(datasetVersions);
    datasets.add(dataset);
    assertEquals(null, dsv1.getUNF());
    IngestUtil.recalculateDatasetVersionUNF(dsv1);
    assertEquals("UNF:6:rDlgOhoEkEQQdwtLRHjmtw==", dsv1.getUNF());
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DataTable(edu.harvard.iq.dataverse.DataTable) Dataset(edu.harvard.iq.dataverse.Dataset) MocksFactory.makeDataset(edu.harvard.iq.dataverse.mocks.MocksFactory.makeDataset) ArrayList(java.util.ArrayList) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) Test(org.junit.Test)

Example 47 with FileMetadata

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

the class IngestUtilTest method testCheckForDuplicateFileNamesWithDirectories.

@Test
public /**
 * Test adding duplicate file name labels with directories, including a
 * duplicate file name label in another directory.
 */
void testCheckForDuplicateFileNamesWithDirectories() throws Exception {
    SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd");
    // create dataset
    Dataset dataset = makeDataset();
    // create dataset version
    DatasetVersion datasetVersion = dataset.getEditVersion();
    datasetVersion.setCreateTime(dateFmt.parse("20001012"));
    datasetVersion.setLastUpdateTime(datasetVersion.getLastUpdateTime());
    datasetVersion.setId(MocksFactory.nextId());
    datasetVersion.setReleaseTime(dateFmt.parse("20010101"));
    datasetVersion.setVersionState(DatasetVersion.VersionState.RELEASED);
    datasetVersion.setMinorVersionNumber(0L);
    datasetVersion.setVersionNumber(1L);
    datasetVersion.setFileMetadatas(new ArrayList<>());
    // create datafiles
    List<DataFile> dataFileList = new ArrayList<>();
    DataFile datafile1 = new DataFile("application/octet-stream");
    datafile1.setStorageIdentifier("subdir/datafile1.txt");
    datafile1.setFilesize(200);
    datafile1.setModificationTime(new Timestamp(new Date().getTime()));
    datafile1.setCreateDate(new Timestamp(new Date().getTime()));
    datafile1.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    datafile1.setOwner(dataset);
    datafile1.setIngestDone();
    datafile1.setChecksumType(DataFile.ChecksumType.SHA1);
    datafile1.setChecksumValue("Unknown");
    // set metadata and add version
    FileMetadata fmd1 = new FileMetadata();
    fmd1.setId(1L);
    fmd1.setLabel("datafile1.txt");
    fmd1.setDirectoryLabel("subdir");
    fmd1.setDataFile(datafile1);
    datafile1.getFileMetadatas().add(fmd1);
    datasetVersion.getFileMetadatas().add(fmd1);
    fmd1.setDatasetVersion(datasetVersion);
    dataFileList.add(datafile1);
    DataFile datafile2 = new DataFile("application/octet-stream");
    datafile2.setStorageIdentifier("subdir/datafile2.txt");
    datafile2.setFilesize(200);
    datafile2.setModificationTime(new Timestamp(new Date().getTime()));
    datafile2.setCreateDate(new Timestamp(new Date().getTime()));
    datafile2.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    datafile2.setOwner(dataset);
    datafile2.setIngestDone();
    datafile2.setChecksumType(DataFile.ChecksumType.SHA1);
    datafile2.setChecksumValue("Unknown");
    // set metadata and add version
    FileMetadata fmd2 = new FileMetadata();
    fmd2.setId(2L);
    fmd2.setLabel("datafile2.txt");
    fmd2.setDirectoryLabel("subdir");
    fmd2.setDataFile(datafile2);
    datafile2.getFileMetadatas().add(fmd2);
    datasetVersion.getFileMetadatas().add(fmd2);
    fmd2.setDatasetVersion(datasetVersion);
    dataFileList.add(datafile2);
    DataFile datafile3 = new DataFile("application/octet-stream");
    datafile3.setStorageIdentifier("datafile2.txt");
    datafile3.setFilesize(200);
    datafile3.setModificationTime(new Timestamp(new Date().getTime()));
    datafile3.setCreateDate(new Timestamp(new Date().getTime()));
    datafile3.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    datafile3.setOwner(dataset);
    datafile3.setIngestDone();
    datafile3.setChecksumType(DataFile.ChecksumType.SHA1);
    datafile3.setChecksumValue("Unknown");
    // set metadata and add version
    FileMetadata fmd3 = new FileMetadata();
    fmd3.setId(3L);
    fmd3.setLabel("datafile2.txt");
    fmd3.setDataFile(datafile3);
    datafile3.getFileMetadatas().add(fmd3);
    dataFileList.add(datafile3);
    IngestUtil.checkForDuplicateFileNamesFinal(datasetVersion, dataFileList);
    boolean file1NameAltered = false;
    boolean file2NameAltered = false;
    boolean file3NameAltered = true;
    for (DataFile df : dataFileList) {
        if (df.getFileMetadata().getLabel().equals("datafile1-1.txt")) {
            file1NameAltered = true;
        }
        if (df.getFileMetadata().getLabel().equals("datafile2-1.txt")) {
            file2NameAltered = true;
        }
        if (df.getFileMetadata().getLabel().equals("datafile2.txt")) {
            file3NameAltered = false;
        }
    }
    // check filenames are unique
    assertEquals(file1NameAltered, true);
    assertEquals(file2NameAltered, true);
    assertEquals(file3NameAltered, false);
    // add duplicate file in root
    datasetVersion.getFileMetadatas().add(fmd3);
    fmd3.setDatasetVersion(datasetVersion);
    // try to add data files with "-1" duplicates and see if it gets incremented to "-2"
    IngestUtil.checkForDuplicateFileNamesFinal(datasetVersion, dataFileList);
    for (DataFile df : dataFileList) {
        if (df.getFileMetadata().getLabel().equals("datafile1-2.txt")) {
            file1NameAltered = true;
        }
        if (df.getFileMetadata().getLabel().equals("datafile2-2.txt")) {
            file2NameAltered = true;
        }
        if (df.getFileMetadata().getLabel().equals("datafile2-1.txt")) {
            file3NameAltered = true;
        }
    }
    // check filenames are unique
    assertEquals(file1NameAltered, true);
    assertEquals(file2NameAltered, true);
    assertEquals(file3NameAltered, true);
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Dataset(edu.harvard.iq.dataverse.Dataset) MocksFactory.makeDataset(edu.harvard.iq.dataverse.mocks.MocksFactory.makeDataset) ArrayList(java.util.ArrayList) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) SimpleDateFormat(java.text.SimpleDateFormat) Timestamp(java.sql.Timestamp) Date(java.util.Date) Test(org.junit.Test)

Example 48 with FileMetadata

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

the class IngestUtilTest method testCheckForDuplicateFileNamesNoDirectories.

@Test
public /**
 * Test adding duplicate file name labels to a dataset version with no
 * subdirectories.
 */
void testCheckForDuplicateFileNamesNoDirectories() throws Exception {
    SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd");
    // create dataset
    Dataset dataset = makeDataset();
    // create dataset version
    DatasetVersion datasetVersion = dataset.getEditVersion();
    datasetVersion.setCreateTime(dateFmt.parse("20001012"));
    datasetVersion.setLastUpdateTime(datasetVersion.getLastUpdateTime());
    datasetVersion.setId(MocksFactory.nextId());
    datasetVersion.setReleaseTime(dateFmt.parse("20010101"));
    datasetVersion.setVersionState(DatasetVersion.VersionState.RELEASED);
    datasetVersion.setMinorVersionNumber(0L);
    datasetVersion.setVersionNumber(1L);
    datasetVersion.setFileMetadatas(new ArrayList<>());
    // create datafiles
    List<DataFile> dataFileList = new ArrayList<>();
    DataFile datafile1 = new DataFile("application/octet-stream");
    datafile1.setStorageIdentifier("datafile1.txt");
    datafile1.setFilesize(200);
    datafile1.setModificationTime(new Timestamp(new Date().getTime()));
    datafile1.setCreateDate(new Timestamp(new Date().getTime()));
    datafile1.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    datafile1.setOwner(dataset);
    datafile1.setIngestDone();
    datafile1.setChecksumType(DataFile.ChecksumType.SHA1);
    datafile1.setChecksumValue("Unknown");
    // set metadata and add verson
    FileMetadata fmd1 = new FileMetadata();
    fmd1.setId(1L);
    fmd1.setLabel("datafile1.txt");
    fmd1.setDataFile(datafile1);
    datafile1.getFileMetadatas().add(fmd1);
    datasetVersion.getFileMetadatas().add(fmd1);
    fmd1.setDatasetVersion(datasetVersion);
    dataFileList.add(datafile1);
    DataFile datafile2 = new DataFile("application/octet-stream");
    datafile2.setStorageIdentifier("datafile2.txt");
    datafile2.setFilesize(200);
    datafile2.setModificationTime(new Timestamp(new Date().getTime()));
    datafile2.setCreateDate(new Timestamp(new Date().getTime()));
    datafile2.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    datafile2.setOwner(dataset);
    datafile2.setIngestDone();
    datafile2.setChecksumType(DataFile.ChecksumType.SHA1);
    datafile2.setChecksumValue("Unknown");
    // set metadata and add version
    FileMetadata fmd2 = new FileMetadata();
    fmd2.setId(2L);
    fmd2.setLabel("datafile2.txt");
    fmd2.setDataFile(datafile2);
    datafile2.getFileMetadatas().add(fmd2);
    datasetVersion.getFileMetadatas().add(fmd2);
    fmd2.setDatasetVersion(datasetVersion);
    dataFileList.add(datafile2);
    IngestUtil.checkForDuplicateFileNamesFinal(datasetVersion, dataFileList);
    boolean file1NameAltered = false;
    boolean file2NameAltered = false;
    for (DataFile df : dataFileList) {
        if (df.getFileMetadata().getLabel().equals("datafile1-1.txt")) {
            file1NameAltered = true;
        }
        if (df.getFileMetadata().getLabel().equals("datafile2-1.txt")) {
            file2NameAltered = true;
        }
    }
    // check filenames are unique and unaltered
    assertEquals(file1NameAltered, true);
    assertEquals(file2NameAltered, true);
    // try to add data files with "-1" duplicates and see if it gets incremented to "-2"
    IngestUtil.checkForDuplicateFileNamesFinal(datasetVersion, dataFileList);
    for (DataFile df : dataFileList) {
        if (df.getFileMetadata().getLabel().equals("datafile1-2.txt")) {
            file1NameAltered = true;
        }
        if (df.getFileMetadata().getLabel().equals("datafile2-2.txt")) {
            file2NameAltered = true;
        }
    }
    // check filenames are unique and unaltered
    assertEquals(file1NameAltered, true);
    assertEquals(file2NameAltered, true);
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Dataset(edu.harvard.iq.dataverse.Dataset) MocksFactory.makeDataset(edu.harvard.iq.dataverse.mocks.MocksFactory.makeDataset) ArrayList(java.util.ArrayList) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) SimpleDateFormat(java.text.SimpleDateFormat) Timestamp(java.sql.Timestamp) Date(java.util.Date) Test(org.junit.Test)

Example 49 with FileMetadata

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

the class AddReplaceFileHelper method step_085_auto_remove_filemetadata_to_replace_from_working_version.

/**
 * Go through the working DatasetVersion and remove the
 * FileMetadata of the file to replace
 *
 * @return
 */
private boolean step_085_auto_remove_filemetadata_to_replace_from_working_version() {
    msgt("step_085_auto_remove_filemetadata_to_replace_from_working_version 1");
    if (!isFileReplaceOperation()) {
        // Shouldn't happen!
        this.addErrorSevere(getBundleErr("only_replace_operation") + " (step_085_auto_remove_filemetadata_to_replace_from_working_version");
        return false;
    }
    msg("step_085_auto_remove_filemetadata_to_replace_from_working_version 2");
    if (this.hasError()) {
        return false;
    }
    msgt("File to replace getId: " + fileToReplace.getId());
    Iterator<FileMetadata> fmIt = workingVersion.getFileMetadatas().iterator();
    msgt("Clear file to replace");
    int cnt = 0;
    while (fmIt.hasNext()) {
        cnt++;
        FileMetadata fm = fmIt.next();
        msg(cnt + ") next file: " + fm);
        msg("   getDataFile().getId(): " + fm.getDataFile().getId());
        if (fm.getDataFile().getId() != null) {
            if (Objects.equals(fm.getDataFile().getId(), fileToReplace.getId())) {
                msg("Let's remove it!");
                // If this is a tabular data file with a UNF, we'll need
                // to recalculate the version UNF, once the file is removed:
                boolean recalculateUNF = !StringUtils.isEmpty(fm.getDataFile().getUnf());
                if (workingVersion.getId() != null) {
                    // If this is an existing draft (i.e., this draft version
                    // is already saved in the dataset, we'll also need to remove this filemetadata
                    // explicitly:
                    msg(" this is an existing draft version...");
                    fileService.removeFileMetadata(fm);
                    // remove the filemetadata from the list of filemetadatas
                    // attached to the datafile object as well, for a good
                    // measure:
                    fileToReplace.getFileMetadatas().remove(fm);
                // (and yes, we can do .remove(fm) safely - if this released
                // file is part of an existing draft, we know that the
                // filemetadata object also exists in the database, and thus
                // has the id, and can be identified unambiguously.
                }
                // and remove it from the list of filemetadatas attached
                // to the version object, via the iterator:
                fmIt.remove();
                if (recalculateUNF) {
                    msg("recalculating the UNF");
                    ingestService.recalculateDatasetVersionUNF(workingVersion);
                    msg("UNF recalculated: " + workingVersion.getUNF());
                }
                return true;
            }
        }
    }
    msg("No matches found!");
    addErrorSevere(getBundleErr("failed_to_remove_old_file_from_dataset"));
    runMajorCleanup();
    return false;
}
Also used : FileMetadata(edu.harvard.iq.dataverse.FileMetadata)

Example 50 with FileMetadata

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

the class AddReplaceFileHelper method removeUnSavedFilesFromWorkingVersion.

/**
 * We are outta here!  Remove everything unsaved from the edit version!
 *
 * @return
 */
private boolean removeUnSavedFilesFromWorkingVersion() {
    msgt("Clean up: removeUnSavedFilesFromWorkingVersion");
    // -----------------------------------------------------------
    // (1) Remove all new FileMetadata objects
    // -----------------------------------------------------------
    // Iterator<FileMetadata> fmIt = dataset.getEditVersion().getFileMetadatas().iterator();//
    // dataset.getEditVersion().getFileMetadatas().iterator();//
    Iterator<FileMetadata> fmIt = workingVersion.getFileMetadatas().iterator();
    while (fmIt.hasNext()) {
        FileMetadata fm = fmIt.next();
        if (fm.getDataFile().getId() == null) {
            fmIt.remove();
        }
    }
    // -----------------------------------------------------------
    // (2) Remove all new DataFile objects
    // -----------------------------------------------------------
    Iterator<DataFile> dfIt = dataset.getFiles().iterator();
    msgt("Clear Files");
    while (dfIt.hasNext()) {
        DataFile df = dfIt.next();
        if (df.getId() == null) {
            dfIt.remove();
        }
    }
    return true;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) FileMetadata(edu.harvard.iq.dataverse.FileMetadata)

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