Search in sources :

Example 1 with UnknownFileTypeException

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;
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DataCollection(org.molgenis.oneclickimporter.model.DataCollection) UnknownFileTypeException(org.molgenis.oneclickimporter.exceptions.UnknownFileTypeException) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) File(java.io.File) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 File (java.io.File)1 List (java.util.List)1 EntityType (org.molgenis.data.meta.model.EntityType)1 UnknownFileTypeException (org.molgenis.oneclickimporter.exceptions.UnknownFileTypeException)1 DataCollection (org.molgenis.oneclickimporter.model.DataCollection)1 Transactional (org.springframework.transaction.annotation.Transactional)1