Search in sources :

Example 11 with FileMetadata

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

the class IngestUtilTest method testCheckForDuplicateFileNamesWithEmptyDirectoryLabels.

@Test
public /**
 * Test adding duplicate file name labels to a dataset version with empty
 * directory labels. This should simulate what happens when uploading a file
 * via the file upload UI.
 */
void testCheckForDuplicateFileNamesWithEmptyDirectoryLabels() 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.setDirectoryLabel("");
    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.setDirectoryLabel("");
    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 12 with FileMetadata

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

the class RestrictFileCommandTest method testRestrictPublishedFile.

@Test
public void testRestrictPublishedFile() throws Exception {
    file.setOwner(dataset);
    dataset.setPublicationDate(new Timestamp(new Date().getTime()));
    RestrictFileCommand cmd = new RestrictFileCommand(file, makeRequest(), restrict);
    engine.submit(cmd);
    // asserts
    assertTrue(!file.isRestricted());
    for (FileMetadata fmw : dataset.getEditVersion().getFileMetadatas()) {
        if (file.equals(fmw.getDataFile())) {
            assertEquals(fmw, file.getFileMetadata());
            assertTrue(fmw.isRestricted());
        }
    }
}
Also used : FileMetadata(edu.harvard.iq.dataverse.FileMetadata) Timestamp(java.sql.Timestamp) Date(java.util.Date) Test(org.junit.Test)

Example 13 with FileMetadata

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

the class FileUtilTest method testGetCiteDataFileFilename.

@Test
public void testGetCiteDataFileFilename() {
    assertEquals(null, FileUtil.getCiteDataFileFilename(null, null));
    FileMetadata png = new FileMetadata();
    png.setLabel("trees.png");
    assertEquals("trees.png-endnote.xml", FileUtil.getCiteDataFileFilename(png, FileUtil.FileCitationExtension.ENDNOTE));
    assertEquals("trees.png.ris", FileUtil.getCiteDataFileFilename(png, FileUtil.FileCitationExtension.RIS));
    assertEquals("trees.png.bib", FileUtil.getCiteDataFileFilename(png, FileUtil.FileCitationExtension.BIBTEX));
    assertEquals(null, FileUtil.getCiteDataFileFilename(png, null));
    FileMetadata tabular = new FileMetadata();
    tabular.setLabel("50by1000.tab");
    assertEquals("50by1000-endnote.xml", FileUtil.getCiteDataFileFilename(tabular, FileUtil.FileCitationExtension.ENDNOTE));
    assertEquals("50by1000.ris", FileUtil.getCiteDataFileFilename(tabular, FileUtil.FileCitationExtension.RIS));
    assertEquals("50by1000.bib", FileUtil.getCiteDataFileFilename(tabular, FileUtil.FileCitationExtension.BIBTEX));
}
Also used : FileMetadata(edu.harvard.iq.dataverse.FileMetadata) Test(org.junit.Test)

Example 14 with FileMetadata

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

the class JsonPrinter method jsonDataFileList.

public static JsonObjectBuilder jsonDataFileList(List<DataFile> dataFiles) {
    if (dataFiles == null) {
        throw new NullPointerException("dataFiles cannot be null");
    }
    JsonObjectBuilder bld = jsonObjectBuilder();
    List<FileMetadata> dataFileList = dataFiles.stream().map(x -> x.getFileMetadata()).collect(Collectors.toList());
    bld.add("files", jsonFileMetadatas(dataFileList));
    return bld;
}
Also used : AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) NullSafeJsonBuilder.jsonObjectBuilder(edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder.jsonObjectBuilder) Arrays(java.util.Arrays) DatasetDistributor(edu.harvard.iq.dataverse.DatasetDistributor) DataFileTag(edu.harvard.iq.dataverse.DataFileTag) Date(java.util.Date) SettingsServiceBean(edu.harvard.iq.dataverse.settings.SettingsServiceBean) User(edu.harvard.iq.dataverse.authorization.users.User) Permission(edu.harvard.iq.dataverse.authorization.Permission) Map(java.util.Map) DataverseFacet(edu.harvard.iq.dataverse.DataverseFacet) ExplicitGroup(edu.harvard.iq.dataverse.authorization.groups.impl.explicit.ExplicitGroup) Collector(java.util.stream.Collector) DatasetField(edu.harvard.iq.dataverse.DatasetField) IpGroup(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup) EnumSet(java.util.EnumSet) JsonObject(javax.json.JsonObject) DatasetFieldValue(edu.harvard.iq.dataverse.DatasetFieldValue) Collection(java.util.Collection) DatasetFieldWalker(edu.harvard.iq.dataverse.util.DatasetFieldWalker) Set(java.util.Set) DataFile(edu.harvard.iq.dataverse.DataFile) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) MetadataBlock(edu.harvard.iq.dataverse.MetadataBlock) BinaryOperator(java.util.function.BinaryOperator) DataverseTheme(edu.harvard.iq.dataverse.DataverseTheme) DataverseContact(edu.harvard.iq.dataverse.DataverseContact) List(java.util.List) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonObjectBuilder(javax.json.JsonObjectBuilder) RoleAssignment(edu.harvard.iq.dataverse.RoleAssignment) StringUtil(edu.harvard.iq.dataverse.util.StringUtil) JsonArrayBuilder(javax.json.JsonArrayBuilder) Util(edu.harvard.iq.dataverse.api.Util) AuthenticationProviderRow(edu.harvard.iq.dataverse.authorization.providers.AuthenticationProviderRow) Deque(java.util.Deque) Function(java.util.function.Function) Supplier(java.util.function.Supplier) TreeSet(java.util.TreeSet) IpAddress(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddress) ArrayList(java.util.ArrayList) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) DataverseRole(edu.harvard.iq.dataverse.authorization.DataverseRole) IpAddressRange(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddressRange) Workflow(edu.harvard.iq.dataverse.workflow.Workflow) BiConsumer(java.util.function.BiConsumer) Json(javax.json.Json) LinkedList(java.util.LinkedList) Dataverse(edu.harvard.iq.dataverse.Dataverse) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) BuiltinUser(edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinUser) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) Dataset(edu.harvard.iq.dataverse.Dataset) JsonArray(javax.json.JsonArray) TermsOfUseAndAccess(edu.harvard.iq.dataverse.TermsOfUseAndAccess) Collections(edu.emory.mathcs.backport.java.util.Collections) PrivateUrl(edu.harvard.iq.dataverse.privateurl.PrivateUrl) Collectors.toList(java.util.stream.Collectors.toList) WorkflowStepData(edu.harvard.iq.dataverse.workflow.step.WorkflowStepData) ShibGroup(edu.harvard.iq.dataverse.authorization.groups.impl.shib.ShibGroup) RoleAssigneeDisplayInfo(edu.harvard.iq.dataverse.authorization.RoleAssigneeDisplayInfo) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 15 with FileMetadata

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

the class FileUtil method createSingleDataFile.

private static DataFile createSingleDataFile(DatasetVersion version, File tempFile, String fileName, String contentType, DataFile.ChecksumType checksumType, boolean addToDataset) {
    if (tempFile == null) {
        return null;
    }
    DataFile datafile = new DataFile(contentType);
    datafile.setModificationTime(new Timestamp(new Date().getTime()));
    /**
     * @todo Think more about when permissions on files are modified.
     * Obviously, here at create time files have some sort of permissions,
     * even if these permissions are *implied*, by ViewUnpublishedDataset at
     * the dataset level, for example.
     */
    datafile.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    FileMetadata fmd = new FileMetadata();
    // TODO: add directoryLabel?
    fmd.setLabel(fileName);
    if (addToDataset) {
        datafile.setOwner(version.getDataset());
    }
    fmd.setDataFile(datafile);
    datafile.getFileMetadatas().add(fmd);
    if (addToDataset) {
        if (version.getFileMetadatas() == null) {
            version.setFileMetadatas(new ArrayList<>());
        }
        version.getFileMetadatas().add(fmd);
        fmd.setDatasetVersion(version);
        version.getDataset().getFiles().add(datafile);
    }
    generateStorageIdentifier(datafile);
    if (!tempFile.renameTo(new File(getFilesTempDirectory() + "/" + datafile.getStorageIdentifier()))) {
        return null;
    }
    try {
        // We persist "SHA1" rather than "SHA-1".
        datafile.setChecksumType(checksumType);
        datafile.setChecksumValue(CalculateCheckSum(getFilesTempDirectory() + "/" + datafile.getStorageIdentifier(), datafile.getChecksumType()));
    } catch (Exception cksumEx) {
        logger.warning("Could not calculate " + checksumType + " signature for the new file " + fileName);
    }
    return datafile;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) Timestamp(java.sql.Timestamp) DataFile(edu.harvard.iq.dataverse.DataFile) File(java.io.File) Date(java.util.Date) FileExceedsMaxSizeException(edu.harvard.iq.dataverse.datasetutility.FileExceedsMaxSizeException) XMLStreamException(javax.xml.stream.XMLStreamException) MissingResourceException(java.util.MissingResourceException) FileNotFoundException(java.io.FileNotFoundException) EJBException(javax.ejb.EJBException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

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