use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CustomProvenance in project timbuctoo by HuygensING.
the class ProvenanceChangeLogTest method getProvenanceForListObjectField.
@Test
public void getProvenanceForListObjectField() throws Exception {
CustomProvenance customProvenance = CustomProvenance.getCustomProvenance(ImmutableMap.of("fields", Lists.newArrayList(ImmutableMap.of("uri", NAME_URI, "isList", true, "object", ImmutableMap.of("type", TYPE_URI, "fields", Lists.newArrayList(ImmutableMap.of("uri", FIRST_NAME_URI, "isList", false, "valueType", STRING)))))));
when(dataSet.getCustomProvenance()).thenReturn(customProvenance);
String value1 = "value1";
Map<Object, Object> name1 = Maps.newHashMap();
name1.put("uri", SUBJECT_NAME_1);
name1.put(FIRST_NAME_FIELD, createPropertyInput(value1));
Map<Object, Object> name2 = Maps.newHashMap();
name2.put("uri", SUBJECT_NAME_2);
String value2 = "value2";
name2.put(FIRST_NAME_FIELD, createPropertyInput(value2));
Map<Object, Object> provenance = Maps.newHashMap();
provenance.put(NAME_FIELD, newArrayList(name1, name2));
Map<Object, Object> entity = Maps.newHashMap();
entity.put("provenance", provenance);
ProvenanceChangeLog instance = new ProvenanceChangeLog(entity);
List<Change> provChanges = instance.getProvenance(dataSet, SUBJECT).collect(toList());
assertThat(provChanges.size(), is(6));
assertThat(provChanges.get(0), is(likeChange().withSubject(SUBJECT).withPredicate(NAME_URI).withValues(new Value(SUBJECT_NAME_1, null)).oldValuesIsEmpty()));
assertThat(provChanges.get(1), is(likeChange().withSubject(SUBJECT_NAME_1).withPredicate(RDF_TYPE).withValues(new Value(TYPE_URI, null)).oldValuesIsEmpty()));
assertThat(provChanges.get(2), is(likeChange().withSubject(SUBJECT_NAME_1).withPredicate(FIRST_NAME_URI).withValues(new Value(value1, STRING)).oldValuesIsEmpty()));
assertThat(provChanges.get(3), is(likeChange().withSubject(SUBJECT).withPredicate(NAME_URI).withValues(new Value(SUBJECT_NAME_2, null)).oldValuesIsEmpty()));
assertThat(provChanges.get(4), is(likeChange().withSubject(SUBJECT_NAME_2).withPredicate(RDF_TYPE).withValues(new Value(TYPE_URI, null)).oldValuesIsEmpty()));
assertThat(provChanges.get(5), is(likeChange().withSubject(SUBJECT_NAME_2).withPredicate(FIRST_NAME_URI).withValues(new Value(value2, STRING)).oldValuesIsEmpty()));
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CustomProvenance in project timbuctoo by HuygensING.
the class ChangeLog method getProvenanceChanges.
protected Stream<Change> getProvenanceChanges(DataSet dataSet, Graph graph, String[] subjects, CustomProvenance provenance, Map<String, JsonNode> values) {
TypeNameStore typeNameStore = dataSet.getTypeNameStore();
Stream<Change> customProv = provenance.getFields().stream().filter(field -> field.getValueType() != null).flatMap(field -> {
String graphQlPred = typeNameStore.makeGraphQlnameForPredicate(field.getUri(), Direction.OUT, field.isList());
return Stream.of(subjects).map(subject -> new Change(graph, subject, field.getUri(), getValues(dataSet, values.get(graphQlPred)), Stream.empty()));
});
Stream<Change> customProvNested = provenance.getFields().stream().filter(field -> field.getObject() != null).flatMap(field -> {
String graphQlPred = typeNameStore.makeGraphQlnameForPredicate(field.getUri(), Direction.OUT, field.isList());
JsonNode objectValues = values.get(graphQlPred);
if (objectValues.isArray()) {
Spliterator<JsonNode> spliterator = Spliterators.spliteratorUnknownSize(objectValues.iterator(), Spliterator.ORDERED);
return StreamSupport.stream(spliterator, false).flatMap(newObjectValues -> getChangesForProvObject(dataSet, newObjectValues, graph, subjects, field));
}
return getChangesForProvObject(dataSet, objectValues, graph, subjects, field);
});
return Stream.concat(customProv, customProvNested);
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CustomProvenance in project timbuctoo by HuygensING.
the class DerivedSchemaGenerator method makeGraphQlTypes.
public DerivedSchemaContainer makeGraphQlTypes(String rootType, Map<String, Type> types, TypeNameStore nameStore, ReadOnlyChecker readOnlyChecker, CustomProvenance customProvenance) {
GraphQlNameGenerator nameGenerator = new GraphQlNameGenerator(nameStore);
DerivedSchemaContainer typesContainer = new DerivedSchemaContainer(rootType, nameGenerator, this.argumentsHelper, readOnlyChecker, customProvenance);
// FIXME find a better way to register standard types to the schema of a data set
typesContainer.valueType(RdfConstants.MARKDOWN);
typesContainer.valueType(RdfConstants.STRING);
typesContainer.valueType(RdfConstants.URI);
for (Type type : types.values()) {
DerivedObjectTypeSchemaGenerator typeSchemaGenerator = typesContainer.addObjectType(type.getName());
for (Predicate predicate : type.getPredicates()) {
fieldForDerivedType(predicate, typesContainer, typeSchemaGenerator, nameGenerator, rootType);
}
}
return typesContainer;
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CustomProvenance in project timbuctoo by HuygensING.
the class SetCustomProvenanceMutation method executeAction.
@Override
public Object executeAction(DataFetchingEnvironment environment) {
ImmutableContextData contextData = environment.getContext();
Optional<User> userOpt = contextData.getUser();
if (!userOpt.isPresent()) {
throw new RuntimeException("User should be logged in.");
}
User user = userOpt.get();
Optional<DataSet> dataSetOpt = dataSetRepository.getDataSet(user, ownerId, dataSetName);
if (!dataSetOpt.isPresent()) {
throw new RuntimeException("Data set is not available.");
}
DataSet dataSet = dataSetOpt.get();
if (!contextData.getUserPermissionCheck().hasPermission(dataSet.getMetadata(), Permission.SET_CUSTOM_PROV)) {
throw new RuntimeException("User should have permissions to set the custom provenance.");
}
TreeNode customProvenanceNode = OBJECT_MAPPER.valueToTree(environment.getArgument("customProvenance"));
try {
CustomProvenance customProvenance = OBJECT_MAPPER.treeToValue(customProvenanceNode, CustomProvenance.class);
validateCustomProvenance(customProvenance);
dataSet.setCustomProvenance(customProvenance);
} catch (IOException e) {
throw new RuntimeException(e);
}
return ImmutableMap.of("message", "Custom provenance is set.");
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CustomProvenance in project timbuctoo by HuygensING.
the class DerivedInputTypeSchemaGeneratorTest method returnsAnEmptyStringForReadOnlyTypes.
@Test
public void returnsAnEmptyStringForReadOnlyTypes() {
DerivedInputTypeSchemaGenerator instance = new DerivedInputTypeSchemaGenerator(READ_ONLY_TYPE, ROOT_TYPE, graphQlNameGenerator, derivedSchemaContainer, readOnlyChecker, new CustomProvenance(Collections.emptyList()));
Predicate predicate = predicate().withName("http://example.com/value").hasDirection(Direction.OUT).withValueType(RdfConstants.STRING).build();
instance.valueField(null, predicate, RdfConstants.STRING);
String schema = instance.getSchema().toString();
assertThat(schema, isEmptyString());
}
Aggregations