use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type in project timbuctoo by HuygensING.
the class SerializerExecutionStrategy method execute.
@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
Map<String, java.util.List<Field>> fields = parameters.fields();
GraphQLObjectType parentType = parameters.typeInfo().castType(GraphQLObjectType.class);
return super.execute(executionContext, parameters).thenApply(sourceResult -> {
Map<String, Object> data = sourceResult.getData();
if (parameters.source() instanceof TypedValue) {
String value = ((TypedValue) parameters.source()).getValue();
String typename = ((TypedValue) parameters.source()).getType();
Value result;
if (value == null) {
result = null;
} else if (data.containsKey("__typename")) {
result = Value.create(value, typename, (String) data.get("__typename"));
} else {
result = Value.create(value, typename);
}
return new ExecutionResultImpl(result, sourceResult.getErrors(), sourceResult.getExtensions());
} else if (parameters.source() instanceof SubjectReference) {
final String uri = ((SubjectReference) parameters.source()).getSubjectUri();
final Set<String> types = ((SubjectReference) parameters.source()).getTypes();
final String graphqlType = getDirectiveArgument(parentType, "rdfType", "uri").orElse(null);
String type;
if (graphqlType != null && types.contains(graphqlType)) {
type = graphqlType;
} else {
Optional<String> firstType = types.stream().sorted().findFirst();
if (firstType.isPresent()) {
type = firstType.get();
} else {
LOG.error("No type present on " + uri + ". Expected at least TIM_UNKNOWN");
type = RdfConstants.UNKNOWN;
}
}
LinkedHashMap<PredicateInfo, Serializable> copy = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : data.entrySet()) {
final String graphqlFieldName = entry.getKey();
final GraphQLFieldDefinition fieldDesc = parentType.getFieldDefinition(entry.getKey());
Optional<String> predicateUri = getDirectiveArgument(fieldDesc, "rdf", "predicate");
Optional<Direction> direction = getDirectiveArgument(fieldDesc, "rdf", "direction").map(Direction::valueOf);
final PredicateInfo predicateInfo;
predicateInfo = predicateUri.map(predUri -> PredicateInfo.predicateInfo(graphqlFieldName, predUri, direction.orElse(Direction.OUT))).orElseGet(() -> PredicateInfo.predicateInfo(graphqlFieldName, null, Direction.OUT));
if (entry.getValue() == null || entry.getValue() instanceof Serializable) {
copy.put(predicateInfo, (Serializable) entry.getValue());
} else {
copy.put(predicateInfo, Value.fromRawJavaType(entry.getValue()));
}
}
return new ExecutionResultImpl(Entity.entity(uri, type, copy), sourceResult.getErrors(), sourceResult.getExtensions());
} else if (parameters.source() instanceof PaginatedList) {
PaginatedList<? extends DatabaseResult> source = (PaginatedList) parameters.source();
return new ExecutionResultImpl(serializableList(source.getPrevCursor().orElse(null), source.getNextCursor().orElse(null), ((GraphqlIntrospectionList) data.get("items")).getItems()), sourceResult.getErrors(), sourceResult.getExtensions());
} else if (executionContext.getGraphQLSchema().getQueryType() == parentType) {
return new ExecutionResultImpl(QueryContainer.queryContainer(sourceResult.getData()), sourceResult.getErrors(), sourceResult.getExtensions());
} else {
LinkedHashMap<String, Serializable> copy = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : data.entrySet()) {
if (entry.getValue() == null || entry.getValue() instanceof Serializable) {
copy.put(entry.getKey(), (Serializable) entry.getValue());
} else {
copy.put(entry.getKey(), GraphqlIntrospectionValue.fromRawJavaType(entry.getValue()));
}
}
return new ExecutionResultImpl(graphqlIntrospectionObject(copy), sourceResult.getErrors(), sourceResult.getExtensions());
}
});
}
use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type in project timbuctoo by HuygensING.
the class Rdf4jRdfParser method importRdf.
@Override
public void importRdf(CachedLog input, String baseUri, String defaultGraph, RdfProcessor rdfProcessor) throws RdfProcessingFailedException {
try {
RDFFormat format = Rio.getParserFormatForMIMEType(input.getMimeType().toString()).orElseThrow(() -> new UnsupportedRDFormatException(input.getMimeType() + " is not a supported rdf type."));
RDFParser rdfParser = Rio.createParser(format);
rdfParser.setPreserveBNodeIDs(true);
rdfParser.setRDFHandler(new TimRdfHandler(rdfProcessor, defaultGraph, input.getFile().getName()));
rdfParser.parse(input.getReader(), baseUri);
} catch (IOException | RDFParseException | UnsupportedRDFormatException e) {
throw new RdfProcessingFailedException(e);
} catch (RDFHandlerException e) {
if (e.getCause() instanceof RdfProcessingFailedException) {
throw (RdfProcessingFailedException) e.getCause();
} else {
throw new RdfProcessingFailedException(e);
}
}
}
use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type in project timbuctoo by HuygensING.
the class JsonLdSerialization method writeContext.
private void writeContext(Set<PredicateInfo> context) throws IOException {
generator.writeFieldName("@context");
generator.writeStartObject();
// ignore the data wrapper by marking it as an index map and as the @graph container
generator.writeFieldName("data");
generator.writeStartObject();
generator.writeStringField("@id", "@graph");
generator.writeStringField("@container", "@index");
generator.writeEndObject();
generator.writeStringField("value", "@value");
generator.writeStringField("type", "@type");
for (PredicateInfo entry : context) {
if (entry.getUri().isPresent()) {
if (entry.getDirection() == Direction.IN) {
generator.writeFieldName(entry.getSafeName());
generator.writeStartObject();
if (entry.getDirection() == Direction.IN) {
generator.writeStringField("@reverse", entry.getUri().get());
} else {
generator.writeStringField("@id", entry.getUri().get());
}
generator.writeEndObject();
} else {
generator.writeStringField(entry.getSafeName(), entry.getUri().get());
}
} else {
generator.writeNullField(entry.getSafeName());
}
}
generator.writeEndObject();
}
use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type in project timbuctoo by HuygensING.
the class MergeSchemasTest method mergeSchemaMergesMatchingPredicates.
@Test
public void mergeSchemaMergesMatchingPredicates() throws Exception {
final MergeSchemas mergeSchemas = new MergeSchemas();
Map<String, Type> generatedSchema = new HashMap<>();
Type predType1 = createTypeWithPredicate("generated", Direction.IN);
predType1.getPredicate("generated", Direction.IN).setHasBeenList(true);
predType1.getPredicate("generated", Direction.IN).setOwner(new Type("testOwner"));
generatedSchema.put("Type", predType1);
Map<String, Type> customSchema = new HashMap<>();
Type predType2 = createTypeWithPredicate("generated", Direction.IN);
predType2.getPredicate("generated", Direction.IN).setHasBeenList(false);
predType2.getPredicate("generated", Direction.IN).setOwner(new Type("testOwner"));
customSchema.put("Type", predType2);
Map<String, Type> mergedSchema = mergeSchemas.mergeSchema(generatedSchema, customSchema);
assertThat(mergedSchema, hasEntry(is("Type"), hasProperty("predicates", contains(predicateMatcher().withName("generated").withDirection(Direction.IN).withWasList(true)))));
assertThat(mergedSchema, hasEntry(is("Type"), hasProperty("predicates", not(hasItem(predicateMatcher().withName("generated").withDirection(Direction.IN).withWasList(false))))));
}
use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type in project timbuctoo by HuygensING.
the class JsonProvenanceToRdfPatchTest method doesOptimisticLocking.
@Test
public void doesOptimisticLocking() throws Exception {
String examplePatch = "{\n" + " \"@type\":\"prov:Activity\",\n" + " \"http://www.w3.org/ns/prov#generates\":[\n" + " {\n" + " \"@type\":\"prov:Entity\",\n" + " \"specializationOf\":{\n" + " \"@id\":\"http://example.com/the/actual/entity1\"\n" + " },\n" + " \"wasRevisionOf\":{\n" + " \"@id\":\"http://example.org/revision1\"\n" + " },\n" + " \"additions\":[\n" + " {\n" + " \"@type\":\"http://timbuctoo.huygens.knaw.nl/v5/vocabulary#mutation\",\n" + " \"predicate\":\"http://example.org/pred1\",\n" + " \"value\":\"value1\"\n" + " }\n" + " ]\n" + " }\n" + " ],\n" + context + "}";
QuadStore testQuadStore = new DummyQuadStore().with("http://example.org/entity1", RdfConstants.TIM_LATEST_REVISION, "http://example.org/revision2", null);
boolean exceptionWasThrown = false;
try {
fromCurrentState(new DocumentLoader(), examplePatch, testQuadStore, "http://example.org/users/myUser", UUID.randomUUID().toString(), CLOCK);
} catch (ConcurrentUpdateException e) {
exceptionWasThrown = true;
}
assertThat(exceptionWasThrown, is(true));
}
Aggregations