Search in sources :

Example 6 with MolgenisException

use of org.molgenis.emx2.MolgenisException in project molgenis-emx2 by molgenis.

the class TestReadWriteStores method testExcelStore.

@Test
public void testExcelStore() throws IOException {
    Path tmp = Files.createTempDirectory(null);
    try {
        Path excelFile = tmp.resolve("test.xlsx");
        System.out.println("defined excel file " + excelFile);
        TableStoreForXlsxFile store = new TableStoreForXlsxFile(excelFile);
        executeTest(store);
    } catch (MolgenisException e) {
        e.printStackTrace();
    } finally {
        Files.walk(tmp).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
    }
    if (Files.exists(tmp))
        throw new RuntimeException("TMP directory " + tmp + " not deleted. This should never happen.");
}
Also used : Path(java.nio.file.Path) MolgenisException(org.molgenis.emx2.MolgenisException) File(java.io.File) Test(org.junit.Test)

Example 7 with MolgenisException

use of org.molgenis.emx2.MolgenisException in project molgenis-emx2 by molgenis.

the class TestReadWriteStores method testCsvZipStore.

@Test
public void testCsvZipStore() throws IOException {
    Path tmp = Files.createTempDirectory(null);
    try {
        Path zipFile = tmp.resolve("test.zip");
        System.out.println("defined zip file " + zipFile);
        TableStoreForCsvInZipFile store = new TableStoreForCsvInZipFile(zipFile);
        executeTest(store);
    } catch (MolgenisException e) {
        e.printStackTrace();
    } finally {
        Files.walk(tmp).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
    }
    if (Files.exists(tmp))
        throw new RuntimeException("TMP directory " + tmp + " not deleted. This should never happen.");
}
Also used : Path(java.nio.file.Path) MolgenisException(org.molgenis.emx2.MolgenisException) File(java.io.File) Test(org.junit.Test)

Example 8 with MolgenisException

use of org.molgenis.emx2.MolgenisException in project molgenis-emx2 by molgenis.

the class TableStoreForCsvFilesDirectory method writeFile.

public void writeFile(String filePath, byte[] contents) {
    if (contents != null && contents.length > 0) {
        try {
            Path dir = directoryPath.resolve("_files");
            if (!Files.exists(dir)) {
                Files.createDirectories(dir);
            }
            Path file = directoryPath.resolve(filePath);
            try (OutputStream out = Files.newOutputStream(file)) {
                out.write(contents);
                out.flush();
            }
        } catch (Exception e) {
            throw new MolgenisException("Writing of file " + filePath + " failed: ", e);
        }
    }
}
Also used : Path(java.nio.file.Path) MolgenisException(org.molgenis.emx2.MolgenisException) MolgenisException(org.molgenis.emx2.MolgenisException)

Example 9 with MolgenisException

use of org.molgenis.emx2.MolgenisException in project molgenis-emx2 by molgenis.

the class TableStoreForCsvInZipFile method create.

private void create() {
    Map<String, String> env = new HashMap<>();
    env.put("create", "true");
    final URI zipUri = URI.create("jar:" + zipFilePath.toUri());
    try {
        FileSystem zipfs = FileSystems.newFileSystem(zipUri, env, null);
        zipfs.close();
    } catch (IOException ioe) {
        throw new MolgenisException("Import failed", ioe);
    }
}
Also used : FileSystem(java.nio.file.FileSystem) MolgenisException(org.molgenis.emx2.MolgenisException) URI(java.net.URI)

Example 10 with MolgenisException

use of org.molgenis.emx2.MolgenisException in project molgenis-emx2 by molgenis.

the class TableStoreForCsvInZipFile method getBinaryFileWrapper.

@Override
public BinaryFileWrapper getBinaryFileWrapper(String name) {
    try (ZipFile zf = new ZipFile(zipFilePath.toFile())) {
        ZipEntry entry = getEntry(zf, name);
        if (entry != null) {
            String contentType = URLConnection.guessContentTypeFromName(entry.getName());
            InputStream contents = zf.getInputStream(entry);
            return new BinaryFileWrapper(contentType, entry.getName(), contents.readAllBytes());
        } else {
            throw new MolgenisException("Import failed: file '" + name + "' not found in file.");
        }
    } catch (Exception e) {
        throw new MolgenisException("Import failed: file '" + name + "' resulted in error: ", e);
    }
}
Also used : ZipFile(java.util.zip.ZipFile) BinaryFileWrapper(org.molgenis.emx2.BinaryFileWrapper) ZipEntry(java.util.zip.ZipEntry) MolgenisException(org.molgenis.emx2.MolgenisException) MolgenisException(org.molgenis.emx2.MolgenisException)

Aggregations

MolgenisException (org.molgenis.emx2.MolgenisException)39 Path (java.nio.file.Path)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)6 Row (org.molgenis.emx2.Row)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 File (java.io.File)5 Column (org.molgenis.emx2.Column)5 ZipFile (java.util.zip.ZipFile)4 Schema (org.molgenis.emx2.Schema)4 Table (org.molgenis.emx2.Table)4 MetadataUtils.deleteColumn (org.molgenis.emx2.sql.MetadataUtils.deleteColumn)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 OutputStream (java.io.OutputStream)3 Writer (java.io.Writer)3 FileSystem (java.nio.file.FileSystem)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ZipEntry (java.util.zip.ZipEntry)3 org.molgenis.emx2 (org.molgenis.emx2)3