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;
}
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");
}
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");
}
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();
}
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);
}
}
Aggregations