Search in sources :

Example 26 with MolgenisException

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

the class SqlColumnRefBackExecutor method createRefBackColumnConstraints.

// will create a dummy array column matching the toColumn we will link to
// will create a before insert trigger to update all REF instances in the other table that needs
// updating
static void createRefBackColumnConstraints(DSLContext jooq, Column ref) {
    try {
        // get ref table
        validateColumn(ref);
        String columNames = ref.getReferences().stream().map(Reference::getName).collect(Collectors.joining(","));
        // get the via column which is also in the 'toTable'
        String refBackColumnName = ref.getRefBack();
        if (refBackColumnName == null) {
            throw new MolgenisException("Create column failed: Create of REFBACK column '" + ref.getQualifiedName() + "' failed because refBack was not set.");
        }
        // create the trigger so that insert/update/delete on REFBACK column updates the
        // relationship
        Column refBack = ref.getRefBackColumn();
        if (refBack == null) {
            throw new MolgenisException("Set refBack on column '" + ref.getTableName() + "." + ref.getName() + "'failed: refBack column '" + ref.getRefBack() + "'not found");
        }
        if (refBack.isRef()) {
            createTriggerForRef(jooq, ref, true);
            createTriggerForRef(jooq, ref, false);
        } else if (refBack.isRefArray()) {
            createTriggerForRefArray(jooq, ref);
        } else {
            throw new MolgenisException("Create column failed: Create of REFBACK column(s) '" + ref.getTableName() + "." + getNames(ref) + "' failed because refBack '" + ref.getRefBack() + "' was not of type REF, REF_ARRAY");
        }
    } catch (DataAccessException dae) {
        throw new SqlMolgenisException("REFBACK column(s) '" + getNames(ref) + "' could not be created in table '" + ref.getTableName() + "'", dae);
    }
}
Also used : Column(org.molgenis.emx2.Column) SqlColumnExecutor.validateColumn(org.molgenis.emx2.sql.SqlColumnExecutor.validateColumn) MolgenisException(org.molgenis.emx2.MolgenisException) DataAccessException(org.jooq.exception.DataAccessException)

Example 27 with MolgenisException

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

the class MolgenisSession method getGraphqlForSchema.

public GraphQL getGraphqlForSchema(String schemaName) {
    logger.info("getting graphql schema '{}' for user '{}'", schemaName, getSessionUser());
    if (graphqlPerSchema.get(schemaName) == null) {
        Schema schema = database.getSchema(schemaName);
        if (schema == null)
            throw new MolgenisException("Schema not found: Schema with name '" + schemaName + "' does not exist or permission denied");
        graphqlPerSchema.put(schemaName, new GraphqlApiFactory().createGraphqlForSchema(schema));
        logger.info("created graphql schema '{}' for user '{}'", schemaName, getSessionUser());
    }
    logger.info("return graphql schema '{}' for user '{}'", schemaName, getSessionUser());
    return graphqlPerSchema.get(schemaName);
}
Also used : Schema(org.molgenis.emx2.Schema) MolgenisException(org.molgenis.emx2.MolgenisException) GraphqlApiFactory(org.molgenis.emx2.graphql.GraphqlApiFactory)

Example 28 with MolgenisException

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

the class TableStoreForCsvFilesDirectory method writeTable.

@Override
public void writeTable(String name, List<String> columnNames, Iterable<Row> rows) {
    Path relativePath = directoryPath.resolve(name + CSV_EXTENSION);
    try {
        Writer writer = Files.newBufferedWriter(relativePath);
        if (rows.iterator().hasNext()) {
            CsvTableWriter.write(rows, writer, separator);
        } else {
            // only header in case no rows provided
            writer.write(columnNames.stream().collect(Collectors.joining("" + separator)));
        }
        writer.close();
    } catch (IOException ioe) {
        throw new MolgenisException("Export failed", ioe);
    }
}
Also used : Path(java.nio.file.Path) MolgenisException(org.molgenis.emx2.MolgenisException) CsvTableWriter(org.molgenis.emx2.io.readers.CsvTableWriter)

Example 29 with MolgenisException

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

the class TableStoreForCsvInZipFile method writeTable.

@Override
public void writeTable(String name, List<String> columnNames, Iterable<Row> rows) {
    // 'Version' table)
    if (columnNames.isEmpty()) {
        return;
    }
    if (!Files.exists(zipFilePath)) {
        create();
    }
    try (FileSystem zipfs = open()) {
        Path pathInZipfile = zipfs.getPath(File.separator + name + CSV_EXTENSION);
        Writer writer = Files.newBufferedWriter(pathInZipfile);
        if (rows.iterator().hasNext()) {
            CsvTableWriter.write(rows, writer, COMMA);
        } else {
            // only header in case no rows provided
            writer.write(columnNames.stream().collect(Collectors.joining("" + COMMA)));
        }
        writer.close();
    } catch (IOException ioe) {
        throw new MolgenisException("Import failed", ioe);
    }
}
Also used : Path(java.nio.file.Path) FileSystem(java.nio.file.FileSystem) MolgenisException(org.molgenis.emx2.MolgenisException) CsvTableWriter(org.molgenis.emx2.io.readers.CsvTableWriter)

Example 30 with MolgenisException

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

the class TableStoreForCsvInZipFile method tableNames.

@Override
public Collection<String> tableNames() {
    List<String> result = new ArrayList<>();
    try (ZipFile zf = new ZipFile(zipFilePath.toFile())) {
        zf.stream().forEach(e -> {
            String name = e.getName();
            if (name.toLowerCase().endsWith(".csv") || name.toLowerCase().endsWith(".tsv")) {
                name = name.substring(0, name.length() - 4);
            }
            result.add(name);
        });
    } catch (IOException ioe) {
        throw new MolgenisException("Import failed: ", ioe);
    }
    return result;
}
Also used : ZipFile(java.util.zip.ZipFile) 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