use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value in project timbuctoo by HuygensING.
the class GraphQlToRdfPatch method addPlan.
private void addPlan(RdfPatchSerializer saver, DataSet dataSet, String newRevision, String association) throws LogStorageFailedException {
String planUri = dataSetObjectUri(dataSet, "plan");
saver.addDelQuad(true, association, PROV_HAD_PLAN, planUri, null, null, graphUri);
saver.addDelQuad(true, planUri, RDF_TYPE, PROV_PLAN, null, null, graphUri);
// fill plan
// add additions
boolean additionsPresent;
try (Stream<Change> additions = changeLog.getAdditions(dataSet)) {
additionsPresent = additions.findAny().isPresent();
}
if (additionsPresent) {
try (Stream<Change> additions = changeLog.getAdditions(dataSet)) {
String actionPluralUri = dataSetObjectUri(dataSet, "additions");
saver.addDelQuad(true, planUri, timPredicate("additions"), actionPluralUri, null, null, graphUri);
saver.addDelQuad(true, actionPluralUri, RDF_TYPE, timType("Additions"), null, null, graphUri);
for (Iterator<Change> changes = additions.iterator(); changes.hasNext(); ) {
Change change = changes.next();
String changeUri = dataSetObjectUri(dataSet, "addition");
saver.addDelQuad(true, actionPluralUri, timPredicate("hasAddition"), changeUri, null, null, graphUri);
saver.addDelQuad(true, changeUri, RDF_TYPE, timType("Addition"), null, null, graphUri);
String predicate = change.getPredicate();
saver.addDelQuad(true, changeUri, timPredicate("hasKey"), predicate, null, null, graphUri);
saver.addDelQuad(true, predicate, RDF_TYPE, timType("ChangeKey"), null, null, graphUri);
String prefValueUri = null;
for (Value value : change.getValues()) {
String uri = dataSetObjectUri(dataSet, "value");
createValue(saver, changeUri, prefValueUri, value, uri);
prefValueUri = uri;
}
}
}
}
// add deletions
boolean deletionsPresent;
try (Stream<Change> deletions = changeLog.getDeletions(dataSet)) {
deletionsPresent = deletions.findAny().isPresent();
}
if (deletionsPresent) {
try (Stream<Change> deletions = changeLog.getDeletions(dataSet)) {
String actionPluralUri = dataSetObjectUri(dataSet, "deletions");
saver.addDelQuad(true, planUri, timPredicate("deletions"), actionPluralUri, null, null, graphUri);
saver.addDelQuad(true, actionPluralUri, RDF_TYPE, timType("Deletions"), null, null, graphUri);
for (Iterator<Change> changes = deletions.iterator(); changes.hasNext(); ) {
Change change = changes.next();
String changeUri = dataSetObjectUri(dataSet, "deletion");
saver.addDelQuad(true, actionPluralUri, timPredicate("hasDeletion"), changeUri, null, null, graphUri);
saver.addDelQuad(true, changeUri, RDF_TYPE, timType("Deletion"), null, null, graphUri);
String predicate = change.getPredicate();
saver.addDelQuad(true, changeUri, timPredicate("hasKey"), predicate, null, null, graphUri);
saver.addDelQuad(true, predicate, RDF_TYPE, timType("ChangeKey"), null, null, graphUri);
String prefValueUri = null;
try (Stream<Value> oldValues = change.getOldValues()) {
for (Iterator<Value> values = oldValues.iterator(); values.hasNext(); ) {
Value value = values.next();
String uri = dataSetObjectUri(dataSet, "value");
createValue(saver, changeUri, prefValueUri, value, uri);
prefValueUri = uri;
}
}
}
}
}
// add replacements
boolean replacementsPresent;
try (Stream<Change> replacements = changeLog.getReplacements(dataSet)) {
replacementsPresent = replacements.findAny().isPresent();
}
if (replacementsPresent) {
try (Stream<Change> replacements = changeLog.getReplacements(dataSet)) {
String actionPluralUri = dataSetObjectUri(dataSet, "replacements");
saver.addDelQuad(true, planUri, timPredicate("replacements"), actionPluralUri, null, null, graphUri);
saver.addDelQuad(true, actionPluralUri, RDF_TYPE, timType("Replacements"), null, null, graphUri);
for (Iterator<Change> changes = replacements.iterator(); changes.hasNext(); ) {
Change change = changes.next();
String changeUri = dataSetObjectUri(dataSet, "replacement");
saver.addDelQuad(true, actionPluralUri, timPredicate("hasReplacement"), changeUri, null, null, graphUri);
saver.addDelQuad(true, changeUri, RDF_TYPE, timType("Replacement"), null, null, graphUri);
String predicate = change.getPredicate();
saver.addDelQuad(true, changeUri, timPredicate("hasKey"), predicate, null, null, graphUri);
saver.addDelQuad(true, predicate, RDF_TYPE, timType("ChangeKey"), null, null, graphUri);
String prefValueUri = null;
for (Value value : change.getValues()) {
String uri = dataSetObjectUri(dataSet, "value");
createValue(saver, changeUri, prefValueUri, value, uri);
prefValueUri = uri;
}
try (Stream<Value> oldValues = change.getOldValues()) {
for (Iterator<Value> values = oldValues.iterator(); values.hasNext(); ) {
Value value = values.next();
String uri = dataSetObjectUri(dataSet, "oldValue");
createOldValue(saver, changeUri, prefValueUri, value, uri);
prefValueUri = uri;
}
}
}
}
}
// add custom provenance
if (changeLog.getProvenance(dataSet, subjectUri).findAny().isPresent()) {
String customProvUri = dataSetObjectUri(dataSet, "customProv");
saver.addDelQuad(true, planUri, timPredicate("hasCustomProv"), customProvUri, null, null, graphUri);
saver.addDelQuad(true, customProvUri, RDF_TYPE, timType("CustomProv"), null, null, graphUri);
Stream<Change> customProvChanges = changeLog.getProvenance(dataSet, newRevision, customProvUri);
for (Iterator<Change> changes = customProvChanges.iterator(); changes.hasNext(); ) {
Change change = changes.next();
for (Value value : change.getValues()) {
saver.addDelQuad(true, change.getSubject(), change.getPredicate(), value.getRawValue(), value.getType(), null, graphUri);
}
}
}
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value in project timbuctoo by HuygensING.
the class SummaryPropsMutation method getValue.
private Optional<String> getValue(Map viewConfig, String valueName, DataSet dataSet) {
Object value = viewConfig.get(valueName);
if (value != null) {
try {
String valueAsString = OBJECT_MAPPER.writeValueAsString(value);
// check if the value can be parsed to SummaryProp
SummaryProp summaryProp = OBJECT_MAPPER.readValue(valueAsString, SummaryProp.class);
List<String> undirectedPath = SummaryProp.getUndirectedPath(summaryProp);
List<String> predicates = dataSet.getSchemaStore().getStableTypes().values().stream().flatMap(type -> type.getPredicates().stream()).map(Predicate::getName).collect(Collectors.toList());
List<String> unknownPredicates = undirectedPath.stream().filter(step -> !predicates.contains(step)).collect(Collectors.toList());
if (!unknownPredicates.isEmpty()) {
throw new RuntimeException(valueName + " contains predicates unknown in the data set: " + unknownPredicates);
}
return of(valueAsString);
} catch (JsonProcessingException e) {
throw new RuntimeException(String.format("Could not process '%s' property", valueName));
}
}
return empty();
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value in project timbuctoo by HuygensING.
the class SummaryPropsMutation method executeAction.
@Override
public Object executeAction(DataFetchingEnvironment env) {
DataSet dataSet = MutationHelpers.getDataSet(env, dataSetRepository::getDataSet);
MutationHelpers.checkPermission(env, dataSet.getMetadata(), Permission.CHANGE_SUMMARYPROPS);
try {
String collectionUri = env.getArgument("collectionUri");
Map data = env.getArgument("summaryProperties");
final PredicateMutation mutation = new PredicateMutation();
mutation.entity(collectionUri, getValue(data, "title", dataSet).map(v -> replace(TIM_SUMMARYTITLEPREDICATE, value(v))).orElse(null), getValue(data, "image", dataSet).map(v -> replace(TIM_SUMMARYIMAGEPREDICATE, value(v))).orElse(null), getValue(data, "description", dataSet).map(v -> replace(TIM_SUMMARYDESCRIPTIONPREDICATE, value(v))).orElse(null));
MutationHelpers.addMutation(dataSet, mutation);
return new LazyTypeSubjectReference(collectionUri, Optional.empty(), dataSet);
} catch (LogStorageFailedException | InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value in project timbuctoo by HuygensING.
the class CreateMutationChangeLog method getAdditions.
@Override
public Stream<Change> getAdditions(DataSet dataSet) {
Change typeAddition = new Change(graph, subject, RDF_TYPE, Collections.singletonList(new Value(typeUri, null)), Stream.empty());
Stream<Change> additions = changeLog.getCreations().entrySet().stream().filter(entry -> !entry.getValue().isNull() && !(entry.getValue().isArray() && entry.getValue().size() == 0)).map(entry -> createChange(dataSet, entry.getKey(), entry.getValue()));
return Stream.concat(Stream.of(typeAddition), additions);
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value in project timbuctoo by HuygensING.
the class DataSetMetadataMutation method executeAction.
@Override
public Object executeAction(DataFetchingEnvironment env) {
DataSet dataSet = MutationHelpers.getDataSet(env, dataSetRepository::getDataSet);
MutationHelpers.checkPermission(env, dataSet.getMetadata(), Permission.EDIT_DATASET_METADATA);
try {
Map md = env.getArgument("metadata");
final String baseUri = dataSet.getMetadata().getBaseUri().endsWith("/") ? dataSet.getMetadata().getBaseUri() : dataSet.getMetadata().getBaseUri() + "/";
addMutation(dataSet, new PredicateMutation().entity(baseUri, this.<String>parseProp(md, "title", v -> replace("http://purl.org/dc/terms/title", value(v))), this.<String>parseProp(md, "description", v -> replace("http://purl.org/dc/terms/description", value(v, MARKDOWN))), this.<String>parseProp(md, "imageUrl", v -> replace("http://xmlns.com/foaf/0.1/depiction", value(v))), this.<Map>parseProp(md, "license", v -> this.<String>parseProp(v, "uri", uri -> replace("http://purl.org/dc/terms/license", subject(uri)))), this.<Map>parseProp(md, "owner", owner -> getOrCreate("http://purl.org/dc/terms/rightsHolder", baseUri + "rightsHolder", this.<String>parseProp(owner, "name", v -> replace("http://schema.org/name", value(v))), this.<String>parseProp(owner, "email", v -> replace("http://schema.org/email", value(v))))), this.<Map>parseProp(md, "contact", owner -> getOrCreate("http://schema.org/ContactPoint", baseUri + "ContactPoint", this.<String>parseProp(owner, "name", v -> replace("http://schema.org/name", value(v))), this.<String>parseProp(owner, "email", v -> replace("http://schema.org/email", value(v))))), this.<Map>parseProp(md, "provenanceInfo", owner -> getOrCreate("http://purl.org/dc/terms/provenance", baseUri + "Provenance", this.<String>parseProp(owner, "title", v -> replace("http://purl.org/dc/terms/title", value(v))), this.<String>parseProp(owner, "body", v -> replace("http://purl.org/dc/terms/description", value(v, MARKDOWN)))))));
return new DataSetWithDatabase(dataSet, env.<ContextData>getContext().getUserPermissionCheck());
} catch (LogStorageFailedException | InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
Aggregations