Search in sources :

Example 31 with Value

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);
            }
        }
    }
}
Also used : Value(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value)

Example 32 with Value

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();
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Optional.empty(java.util.Optional.empty) SummaryProp(nl.knaw.huygens.timbuctoo.v5.graphql.defaultconfiguration.SummaryProp) DataSetRepository(nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository) Optional.of(java.util.Optional.of) Predicate(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate) TIM_SUMMARYDESCRIPTIONPREDICATE(nl.knaw.huygens.timbuctoo.v5.util.RdfConstants.TIM_SUMMARYDESCRIPTIONPREDICATE) Map(java.util.Map) TIM_SUMMARYIMAGEPREDICATE(nl.knaw.huygens.timbuctoo.v5.util.RdfConstants.TIM_SUMMARYIMAGEPREDICATE) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) TIM_SUMMARYTITLEPREDICATE(nl.knaw.huygens.timbuctoo.v5.util.RdfConstants.TIM_SUMMARYTITLEPREDICATE) PredicateMutation(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation) PredicateMutation.value(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation.value) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Permission(nl.knaw.huygens.timbuctoo.v5.security.dto.Permission) ExecutionException(java.util.concurrent.ExecutionException) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) List(java.util.List) Optional(java.util.Optional) LazyTypeSubjectReference(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.berkeleydb.dto.LazyTypeSubjectReference) PredicateMutation.replace(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation.replace) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) SummaryProp(nl.knaw.huygens.timbuctoo.v5.graphql.defaultconfiguration.SummaryProp) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 33 with Value

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);
    }
}
Also used : PredicateMutation(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) LazyTypeSubjectReference(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.berkeleydb.dto.LazyTypeSubjectReference) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map)

Example 34 with Value

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);
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) TreeNode(com.fasterxml.jackson.core.TreeNode) Change(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change) Graph(nl.knaw.huygens.timbuctoo.v5.util.Graph) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Maps(com.google.common.collect.Maps) LinkedHashMap(java.util.LinkedHashMap) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) List(java.util.List) Stream(java.util.stream.Stream) RDF_TYPE(nl.knaw.huygens.timbuctoo.v5.util.RdfConstants.RDF_TYPE) Value(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value) Map(java.util.Map) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) JsonNode(com.fasterxml.jackson.databind.JsonNode) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) Collections(java.util.Collections) Value(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value) Change(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change)

Example 35 with Value

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);
    }
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) PredicateMutation.value(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation.value) DataSetRepository(nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository) DataSetWithDatabase(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.DataSetWithDatabase) Function(java.util.function.Function) PredicateMutation.getOrCreate(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation.getOrCreate) ContextData(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ContextData) Permission(nl.knaw.huygens.timbuctoo.v5.security.dto.Permission) ExecutionException(java.util.concurrent.ExecutionException) PredicateMutation.subject(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation.subject) Map(java.util.Map) MARKDOWN(nl.knaw.huygens.timbuctoo.v5.util.RdfConstants.MARKDOWN) MutationOperation(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation.MutationOperation) MutationHelpers.addMutation(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.MutationHelpers.addMutation) PredicateMutation.replace(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation.replace) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) PredicateMutation(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) PredicateMutation(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation) DataSetWithDatabase(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.DataSetWithDatabase) ContextData(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ContextData) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map)

Aggregations

Test (org.junit.Test)42 Value (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value)32 Graph (nl.knaw.huygens.timbuctoo.v5.util.Graph)22 ChangeMatcher.likeChange (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.ChangeMatcher.likeChange)18 Map (java.util.Map)15 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)15 ExecutionException (java.util.concurrent.ExecutionException)12 LogStorageFailedException (nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException)11 PredicateMutation (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation)11 EditMutationChangeLog (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.EditMutationChangeLog)10 QuadStore (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore)9 CursorQuad (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad)9 List (java.util.List)8 IOException (java.io.IOException)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Direction (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction)6 CustomProvenance (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CustomProvenance)6 Optional (java.util.Optional)5