use of org.molgenis.oneclickimporter.exceptions.UnknownFileTypeException in project molgenis by molgenis.
the class OneClickImportJob method getEntityType.
@Transactional
public List<EntityType> getEntityType(Progress progress, String filename) throws UnknownFileTypeException, IOException, InvalidFormatException, EmptySheetException {
File file = fileStore.getFile(filename);
String fileExtension = findExtensionFromPossibilities(filename, newHashSet("csv", "xlsx", "zip", "xls"));
progress.status("Preparing import");
List<DataCollection> dataCollections = newArrayList();
if (fileExtension == null) {
throw new UnknownFileTypeException(String.format("File [%s] does not have a valid extension, supported: [csv, xlsx, zip, xls]", filename));
} else if (fileExtension.equals("xls") || fileExtension.equals("xlsx")) {
List<Sheet> sheets = excelService.buildExcelSheetsFromFile(file);
dataCollections.addAll(oneClickImporterService.buildDataCollectionsFromExcel(sheets));
} else if (fileExtension.equals("csv")) {
List<String[]> lines = csvService.buildLinesFromFile(file);
dataCollections.add(oneClickImporterService.buildDataCollectionFromCsv(oneClickImporterNamingService.createValidIdFromFileName(filename), lines));
} else if (fileExtension.equals("zip")) {
List<File> filesInZip = unzip(file);
for (File fileInZip : filesInZip) {
String fileInZipExtension = findExtensionFromPossibilities(fileInZip.getName(), newHashSet("csv"));
if (fileInZipExtension != null) {
List<String[]> lines = csvService.buildLinesFromFile(fileInZip);
dataCollections.add(oneClickImporterService.buildDataCollectionFromCsv(oneClickImporterNamingService.createValidIdFromFileName(fileInZip.getName()), lines));
} else {
throw new UnknownFileTypeException("Zip file contains files which are not of type CSV");
}
}
}
List<EntityType> entityTypes = newArrayList();
String packageName = oneClickImporterNamingService.createValidIdFromFileName(filename);
dataCollections.forEach(dataCollection -> {
progress.status("Importing [" + dataCollection.getName() + "] into package [" + packageName + "]");
entityTypes.add(entityService.createEntityType(dataCollection, packageName));
});
return entityTypes;
}
Aggregations