use of edu.harvard.iq.dataverse.FileMetadata in project dataverse by IQSS.
the class IngestServiceBean method addFilesToDataset.
// addFilesToDataset() takes a list of new DataFiles and attaches them to the parent
// Dataset (the files are attached to the dataset, and the fileMetadatas to the
// supplied version).
public void addFilesToDataset(DatasetVersion version, List<DataFile> newFiles) {
if (newFiles != null && newFiles.size() > 0) {
Dataset dataset = version.getDataset();
for (DataFile dataFile : newFiles) {
// These are all brand new files, so they should all have
// one filemetadata total. -- L.A.
FileMetadata fileMetadata = dataFile.getFileMetadatas().get(0);
String fileName = fileMetadata.getLabel();
// Attach the file to the dataset and to the version:
dataFile.setOwner(dataset);
version.getFileMetadatas().add(dataFile.getFileMetadata());
dataFile.getFileMetadata().setDatasetVersion(version);
dataset.getFiles().add(dataFile);
}
}
}
use of edu.harvard.iq.dataverse.FileMetadata in project dataverse by IQSS.
the class IngestUtil method checkForDuplicateFileNamesFinal.
/**
* Checks a list of new data files for duplicate names, renaming any
* duplicates to ensure that they are unique.
*
* @param version the dataset version
* @param newFiles the list of new data files to add to it
*/
public static void checkForDuplicateFileNamesFinal(DatasetVersion version, List<DataFile> newFiles) {
// Step 1: create list of existing path names from all FileMetadata in the DatasetVersion
// unique path name: directoryLabel + file separator + fileLabel
Set<String> pathNamesExisting = existingPathNamesAsSet(version);
// Step 2: check each new DataFile against the list of path names, if a duplicate create a new unique file name
for (Iterator<DataFile> dfIt = newFiles.iterator(); dfIt.hasNext(); ) {
FileMetadata fm = dfIt.next().getFileMetadata();
fm.setLabel(duplicateFilenameCheck(fm, pathNamesExisting));
}
}
use of edu.harvard.iq.dataverse.FileMetadata in project dataverse by IQSS.
the class TestIngest method datafile.
// @EJB
@Path("test/file")
@GET
@Produces({ "text/plain" })
public String datafile(@QueryParam("fileName") String fileName, @QueryParam("fileType") String fileType, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) /*throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/
{
String output = "";
if (StringUtil.isEmpty(fileName) || StringUtil.isEmpty(fileType)) {
output = output.concat("Usage: /api/ingest/test/file?fileName=PATH&fileType=TYPE");
return output;
}
BufferedInputStream fileInputStream = null;
try {
fileInputStream = new BufferedInputStream(new FileInputStream(new File(fileName)));
} catch (FileNotFoundException notfoundEx) {
fileInputStream = null;
}
if (fileInputStream == null) {
output = output.concat("Could not open file " + fileName + ".");
return output;
}
TabularDataFileReader ingestPlugin = ingestService.getTabDataReaderByMimeType(fileType);
if (ingestPlugin == null) {
output = output.concat("Could not locate an ingest plugin for type " + fileType + ".");
return output;
}
TabularDataIngest tabDataIngest = null;
try {
tabDataIngest = ingestPlugin.read(fileInputStream, null);
} catch (IOException ingestEx) {
output = output.concat("Caught an exception trying to ingest file " + fileName + ".");
return output;
}
try {
if (tabDataIngest != null) {
File tabFile = tabDataIngest.getTabDelimitedFile();
if (tabDataIngest.getDataTable() != null && tabFile != null && tabFile.exists()) {
String tabFilename = FileUtil.replaceExtension(fileName, "tab");
java.nio.file.Files.copy(Paths.get(tabFile.getAbsolutePath()), Paths.get(tabFilename), StandardCopyOption.REPLACE_EXISTING);
DataTable dataTable = tabDataIngest.getDataTable();
DataFile dataFile = new DataFile();
dataFile.setStorageIdentifier(tabFilename);
FileMetadata fileMetadata = new FileMetadata();
fileMetadata.setLabel(fileName);
dataFile.setDataTable(dataTable);
dataTable.setDataFile(dataFile);
fileMetadata.setDataFile(dataFile);
dataFile.getFileMetadatas().add(fileMetadata);
output = output.concat("NVARS: " + dataTable.getVarQuantity() + "\n");
output = output.concat("NOBS: " + dataTable.getCaseQuantity() + "\n");
try {
ingestService.produceSummaryStatistics(dataFile, tabFile);
output = output.concat("UNF: " + dataTable.getUnf() + "\n");
} catch (IOException ioex) {
output = output.concat("UNF: failed to calculate\n" + "\n");
}
for (int i = 0; i < dataTable.getVarQuantity(); i++) {
String vartype = "";
// if ("continuous".equals(dataTable.getDataVariables().get(i).getVariableIntervalType().getName())) {
if (dataTable.getDataVariables().get(i).isIntervalContinuous()) {
vartype = "numeric-continuous";
} else {
if (dataTable.getDataVariables().get(i).isTypeNumeric()) {
vartype = "numeric-discrete";
} else {
String formatCategory = dataTable.getDataVariables().get(i).getFormatCategory();
if ("time".equals(formatCategory)) {
vartype = "character-time";
} else if ("date".equals(formatCategory)) {
vartype = "character-date";
} else {
vartype = "character";
}
}
}
output = output.concat("VAR" + i + " ");
output = output.concat(dataTable.getDataVariables().get(i).getName() + " ");
output = output.concat(vartype + " ");
output = output.concat(dataTable.getDataVariables().get(i).getUnf());
output = output.concat("\n");
}
} else {
output = output.concat("Ingest failed to produce tab file or data table for file " + fileName + ".");
return output;
}
} else {
output = output.concat("Ingest resulted in a null tabDataIngest object for file " + fileName + ".");
return output;
}
} catch (IOException ex) {
output = output.concat("Caught an exception trying to save ingested data for file " + fileName + ".");
return output;
}
return output;
}
use of edu.harvard.iq.dataverse.FileMetadata in project dataverse by IQSS.
the class FileUtilTest method testIsPubliclyDownloadable2.
@Test
public void testIsPubliclyDownloadable2() {
FileMetadata nonRestrictedFileMetadata = new FileMetadata();
DatasetVersion dsv = new DatasetVersion();
TermsOfUseAndAccess termsOfUseAndAccess = new TermsOfUseAndAccess();
termsOfUseAndAccess.setTermsOfUse("be excellent to each other");
dsv.setTermsOfUseAndAccess(termsOfUseAndAccess);
dsv.setVersionState(DatasetVersion.VersionState.RELEASED);
nonRestrictedFileMetadata.setDatasetVersion(dsv);
Dataset dataset = new Dataset();
dsv.setDataset(dataset);
nonRestrictedFileMetadata.setRestricted(false);
assertEquals(false, FileUtil.isPubliclyDownloadable(nonRestrictedFileMetadata));
}
use of edu.harvard.iq.dataverse.FileMetadata in project dataverse by IQSS.
the class FileUtilTest method testIsPubliclyDownloadable.
@Test
public void testIsPubliclyDownloadable() {
assertEquals(false, FileUtil.isPubliclyDownloadable(null));
FileMetadata restrictedFileMetadata = new FileMetadata();
restrictedFileMetadata.setRestricted(true);
assertEquals(false, FileUtil.isPubliclyDownloadable(restrictedFileMetadata));
FileMetadata nonRestrictedFileMetadata = new FileMetadata();
DatasetVersion dsv = new DatasetVersion();
dsv.setVersionState(DatasetVersion.VersionState.RELEASED);
nonRestrictedFileMetadata.setDatasetVersion(dsv);
Dataset dataset = new Dataset();
dsv.setDataset(dataset);
nonRestrictedFileMetadata.setRestricted(false);
assertEquals(true, FileUtil.isPubliclyDownloadable(nonRestrictedFileMetadata));
}
Aggregations