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