Search in sources :

Example 1 with InvalidFileException

use of nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException in project timbuctoo by HuygensING.

the class DataPerfectLoader method writeZipToTempDir.

private File writeZipToTempDir(File source) throws IOException, InvalidFileException {
    File output = createTempDir();
    try (ZipInputStream zis = new ZipInputStream(new FileInputStream(source))) {
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        try {
            while ((ze = zis.getNextEntry()) != null) {
                String fileName = ze.getName();
                File newFile = new File(output.getAbsolutePath() + File.separator + fileName);
                if (ze.isDirectory()) {
                    newFile.mkdirs();
                } else {
                    // create all non exists folders
                    // else you will hit FileNotFoundException for compressed folder
                    new File(newFile.getParent()).mkdirs();
                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
                        int len;
                        while ((len = zis.read(buffer)) > 0) {
                            fos.write(buffer, 0, len);
                        }
                    }
                }
            }
        } catch (ZipException e) {
            throw new InvalidFileException("The dataperfect archive should be sent as a ZIP encoded directory");
        }
    }
    return output;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) InvalidFileException(nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) ZipException(java.util.zip.ZipException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with InvalidFileException

use of nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException in project timbuctoo by HuygensING.

the class BulkUploadService method saveToDb.

public void saveToDb(String vreName, Loader loader, List<Tuple<String, File>> tempFiles, String vreLabel, Consumer<String> statusUpdate) throws IOException, InvalidFileException {
    String fileNamesDisplay;
    if (tempFiles.size() == 1) {
        fileNamesDisplay = tempFiles.get(0).getLeft();
    } else {
        fileNamesDisplay = "multiple files: " + tempFiles.stream().map(Tuple::getLeft).collect(joining(", "));
    }
    try (TinkerpopSaver saver = new TinkerpopSaver(vres, graphwrapper, vreName, vreLabel, maxVertices, fileNamesDisplay)) {
        try {
            loader.loadData(tempFiles, new Importer(new StateMachine(saver), new ResultReporter(statusUpdate)));
            saver.setUploadFinished(vreName, Vre.PublishState.MAPPING_CREATION);
        } catch (IOException | InvalidFileException e) {
            saver.setUploadFinished(vreName, Vre.PublishState.UPLOAD_FAILED);
            throw e;
        }
    }
}
Also used : StateMachine(nl.knaw.huygens.timbuctoo.bulkupload.parsingstatemachine.StateMachine) InvalidFileException(nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException) TinkerpopSaver(nl.knaw.huygens.timbuctoo.database.tinkerpop.TinkerpopSaver) IOException(java.io.IOException) ResultReporter(nl.knaw.huygens.timbuctoo.bulkupload.parsingstatemachine.ResultReporter) Importer(nl.knaw.huygens.timbuctoo.bulkupload.parsingstatemachine.Importer)

Example 3 with InvalidFileException

use of nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException in project timbuctoo by HuygensING.

the class AllSheetLoaderTest method importExcel.

@Test
public void importExcel() throws Exception {
    URL excelFileUrl = AllSheetLoaderTest.class.getResource("test.xlsx");
    File excelFile = new File(excelFileUrl.toURI());
    AllSheetLoader instance = new AllSheetLoader();
    try {
        List<String> results = new ArrayList<>();
        AtomicBoolean failure = new AtomicBoolean(false);
        instance.loadData(newArrayList(tuple("test.csv", excelFile)), ImporterStubs.withCustomReporter(logline -> {
            if (logline.matches("failure.*")) {
                failure.set(true);
            }
            results.add(logline);
        }));
        if (failure.get()) {
            throw new RuntimeException("Failure during import: \n" + String.join("\n", results));
        }
    } catch (InvalidFileException e) {
        throw new RuntimeException(e);
    }
}
Also used : InvalidFileException(nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) URL(java.net.URL) ImporterStubs(nl.knaw.huygens.timbuctoo.bulkupload.parsingstatemachine.ImporterStubs) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Tuple.tuple(nl.knaw.huygens.timbuctoo.util.Tuple.tuple) File(java.io.File) ArrayList(java.util.ArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InvalidFileException(nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 4 with InvalidFileException

use of nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException in project timbuctoo by HuygensING.

the class CsvLoaderTest method importCsv.

@Test
public void importCsv() throws Exception {
    File csvFile = new File(CsvLoaderTest.class.getResource("test.csv").toURI());
    CsvLoader loader = new CsvLoader(new HashMap<>());
    try {
        List<String> results = new ArrayList<>();
        AtomicBoolean failure = new AtomicBoolean(false);
        loader.loadData(newArrayList(tuple("testcollection.csv", csvFile)), ImporterStubs.withCustomReporter(logline -> {
            if (logline.matches("failure.*")) {
                failure.set(true);
            }
            results.add(logline);
        }));
        if (failure.get()) {
            throw new RuntimeException("Failure during import: \n" + String.join("\n", results));
        }
    } catch (InvalidFileException e) {
        throw new RuntimeException(e);
    }
}
Also used : InvalidFileException(nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ImporterStubs(nl.knaw.huygens.timbuctoo.bulkupload.parsingstatemachine.ImporterStubs) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) HashMap(java.util.HashMap) Tuple.tuple(nl.knaw.huygens.timbuctoo.util.Tuple.tuple) File(java.io.File) ArrayList(java.util.ArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InvalidFileException(nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.junit.Test)

Example 5 with InvalidFileException

use of nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException in project timbuctoo by HuygensING.

the class DataPerfectLoader method loadData.

@Override
public void loadData(List<Tuple<String, File>> files, Importer importer) throws InvalidFileException, IOException {
    Database dbPerfectdb = null;
    try {
        File output = writeZipToTempDir(files.get(0).getRight());
        File dirWithDb = unwrapToplevelDirIfNeeded(output);
        File structureFile = getStructureFile(dirWithDb);
        dbPerfectdb = new Database(structureFile);
        importDbPerfect(importer, dbPerfectdb);
        delete(output);
    } catch (DataPerfectLibException e) {
        throw new InvalidFileException("Not a valid dataperfect file", e);
    } finally {
        try {
            if (dbPerfectdb != null) {
                dbPerfectdb.close();
            }
        } catch (IOException e) {
            LOG.error("Something went wrong while closing the resources in the data import", e);
        }
    }
}
Also used : DataPerfectLibException(nl.knaw.dans.common.dataperfect.DataPerfectLibException) InvalidFileException(nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException) Database(nl.knaw.dans.common.dataperfect.Database) IOException(java.io.IOException) File(java.io.File)

Aggregations

InvalidFileException (nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException)6 File (java.io.File)5 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ImporterStubs (nl.knaw.huygens.timbuctoo.bulkupload.parsingstatemachine.ImporterStubs)2 Tuple.tuple (nl.knaw.huygens.timbuctoo.util.Tuple.tuple)2 Test (org.junit.Test)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 ZipEntry (java.util.zip.ZipEntry)1 ZipException (java.util.zip.ZipException)1 ZipInputStream (java.util.zip.ZipInputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1