Search in sources :

Example 1 with MolgenisException

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

the class Column method getReferences.

/**
 * will return self in case of single, and multiple in case of composite key wrapper
 */
public List<Reference> getReferences() {
    // no ref
    if (getRefTable() == null) {
        return new ArrayList<>();
    }
    List<Column> pkeys = getRefTable().getPrimaryKeyColumns();
    List<Reference> refColumns = new ArrayList<>();
    // check if primary key exists
    if (pkeys.size() == 0) {
        throw new MolgenisException("Error in column '" + getName() + "': Reference to " + getRefTableName() + " fails because that table has no primary key");
    }
    // create the refs
    Column refLink = getRefLinkColumn();
    for (Column keyPart : pkeys) {
        if (keyPart.isReference()) {
            for (Reference ref : keyPart.getReferences()) {
                ColumnType type = ref.getPrimitiveType();
                if (!isRef()) {
                    type = getArrayType(type);
                }
                List<String> path = ref.getPath();
                path.add(0, keyPart.getName());
                String name = null;
                if (refLink != null) {
                    for (Reference overlap : refLink.getReferences()) {
                        if (overlap.getTargetTable().equals(ref.getTargetTable()) && overlap.getTargetColumn().equals(ref.getTargetColumn())) {
                            name = overlap.getName();
                        }
                    }
                }
                if (name == null) {
                    name = getName();
                    if (pkeys.size() > 1) {
                        name += COMPOSITE_REF_SEPARATOR + ref.getName();
                    }
                }
                refColumns.add(new Reference(this, name, ref.getName(), getColumnType(), type, keyPart.getColumnType().isArray(), ref.getTargetTable(), ref.getTargetColumn(), ref.isRequired() || this.isRequired(), path));
            }
        } else {
            ColumnType type = keyPart.getColumnType();
            // all but ref is array
            if (!isRef()) {
                type = getArrayType(type);
            }
            // create the ref
            String name = getName();
            if (pkeys.size() > 1) {
                name += COMPOSITE_REF_SEPARATOR + keyPart.getName();
            }
            refColumns.add(new Reference(this, name, keyPart.getName(), getColumnType(), type, getColumnType().isArray(), getRefTableName(), keyPart.getName(), keyPart.isRequired() || this.isRequired(), new ArrayList<>(List.of(keyPart.getName()))));
        }
    }
    // clean up in case only one
    if (refColumns.stream().filter(r -> r.getName().startsWith(getName())).count() == 1) {
        refColumns = refColumns.stream().map(r -> {
            if (r.getName().startsWith(getName()))
                r.setName(getName());
            return r;
        }).collect(Collectors.toList());
    }
    // remove duplicates
    HashSet<Object> seen = new HashSet<>();
    refColumns.removeIf(e -> !seen.add(e.getName()));
    return refColumns;
}
Also used : ColumnType(org.molgenis.emx2.ColumnType) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 2 with MolgenisException

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

the class TestGraphqlCrossSchemaRefs method execute.

private JsonNode execute(String query) throws IOException {
    String result = convertExecutionResultToJson(graphql.execute(query));
    JsonNode node = new ObjectMapper().readTree(result);
    if (node.get("errors") != null) {
        throw new MolgenisException(node.get("errors").get(0).get("message").asText(), "");
    }
    return new ObjectMapper().readTree(result).get("data");
}
Also used : MolgenisException(org.molgenis.emx2.MolgenisException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with MolgenisException

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

the class TestGraphqlAdminFields method execute.

private JsonNode execute(String query) throws IOException {
    String result = convertExecutionResultToJson(grapql.execute(query));
    JsonNode node = new ObjectMapper().readTree(result);
    if (node.get("errors") != null) {
        throw new MolgenisException(node.get("errors").get(0).get("message").asText());
    }
    return new ObjectMapper().readTree(result).get("data");
}
Also used : MolgenisException(org.molgenis.emx2.MolgenisException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with MolgenisException

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

the class GraphqlCustomExceptionHandler method onException.

@Override
public DataFetcherExceptionHandlerResult onException(DataFetcherExceptionHandlerParameters handlerParameters) {
    final Throwable exception = handlerParameters.getException();
    final SourceLocation sourceLocation = handlerParameters.getSourceLocation();
    final ResultPath path = handlerParameters.getPath();
    GraphQLError error = new ExceptionWhileDataFetching(path, exception, sourceLocation);
    final ErrorClassification errorType = error.getErrorType();
    if (exception instanceof MolgenisException) {
        error = new GraphQLError() {

            @Override
            public String getMessage() {
                return exception.toString();
            }

            @Override
            public List<SourceLocation> getLocations() {
                return List.of(sourceLocation);
            }

            @Override
            public ErrorClassification getErrorType() {
                return errorType;
            }
        };
    }
    return DataFetcherExceptionHandlerResult.newResult().error(error).build();
}
Also used : SourceLocation(graphql.language.SourceLocation) ErrorClassification(graphql.ErrorClassification) ResultPath(graphql.execution.ResultPath) MolgenisException(org.molgenis.emx2.MolgenisException) GraphQLError(graphql.GraphQLError) ExceptionWhileDataFetching(graphql.ExceptionWhileDataFetching) List(java.util.List)

Example 5 with MolgenisException

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

the class TestReadWriteStores method executeTest.

public static void executeTest(TableStore store) throws IOException, MolgenisException {
    List<Row> rows = new ArrayList<>();
    int count = 10;
    for (int i = 1; i <= count; i++) {
        rows.add(new Row().setString("stringCol", "test" + i).setInt("intCol", i).setDecimal("decimalCol", Double.valueOf(i / 2)).setUuid("uuidCol", UUID.randomUUID()).setDate("dateCol", LocalDate.of(2019, 12, 12)).setDateTime("datetimeCol", LocalDateTime.now()).setBool("boolCol", true).setStringArray("stringarrayCol", new String[] { "a", "b,including comma," }).setIntArray("intarrayCol", new Integer[] { 1, 2 }).setDecimalArray("doubleArrayCol", new Double[] { 1.0, 2.0 }).setDecimalArray("doubleArrayCol", new Double[] { 1.0, 2.0 }).setDateArray("dateArray", new LocalDate[] { LocalDate.of(2019, 12, 12), LocalDate.of(2019, 12, 12) }).setDateTimeArray("datetimeArrayCol", new LocalDateTime[] { LocalDateTime.now(), LocalDateTime.now() }).setBoolArray("booleanArrayCol", new Boolean[] { true, false }));
    }
    StopWatch.start("created some rows");
    // write them
    store.writeTable("test", List.of(), rows);
    store.writeTable("test2", List.of(), rows);
    StopWatch.print("wrote them to " + store.getClass().getSimpleName(), count);
    List<Row> rows2 = StreamSupport.stream(store.readTable("test2").spliterator(), false).collect(Collectors.toList());
    // for (Row r : rows2) System.out.println(r);
    StopWatch.print("fromReader them back from " + store.getClass().getSimpleName(), count);
    // compare
    CompareTools.assertEquals(rows, rows2);
    // write another one
    store.writeTable("test3", List.of(), rows);
    StopWatch.print("wrote them to " + store.getClass().getSimpleName(), count);
    rows2 = StreamSupport.stream(store.readTable("test3").spliterator(), false).collect(Collectors.toList());
    // for (Row r : rows2) System.out.println(r);
    StopWatch.print("fromReader them back from " + store.getClass().getSimpleName(), count);
    // compare
    CompareTools.assertEquals(rows, rows2);
    StopWatch.print("compared succesfully");
    // write empty
    store.writeTable("test4", List.of("empty"), new ArrayList<>());
    // test that reading store that doesn't exist errors properly
    try {
        store.readTable("fake");
        fail("should have failed");
    } catch (MolgenisException me) {
        System.out.println("errored correctly:" + me);
    }
}
Also used : ArrayList(java.util.ArrayList) MolgenisException(org.molgenis.emx2.MolgenisException) Row(org.molgenis.emx2.Row) LocalDate(java.time.LocalDate)

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