Search in sources :

Example 1 with DeleteMutationChangeLog

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.DeleteMutationChangeLog in project timbuctoo by HuygensING.

the class DeleteMutation method executeAction.

@Override
public Object executeAction(DataFetchingEnvironment environment) {
    final Graph graph = new Graph(environment.getArgument("graph"));
    final String uri = environment.getArgument("uri");
    final Map entity = environment.getArgument("entity");
    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.DELETE)) {
        throw new RuntimeException("User should have permissions to delete entities of the data set.");
    }
    try (Stream<CursorQuad> quads = dataSet.getQuadStore().getQuadsInGraph(uri, Optional.of(graph))) {
        if (quads.findAny().isEmpty()) {
            if (graph.isDefaultGraph()) {
                throw new RuntimeException("Subject with uri '" + uri + "' does not exist in the default graph");
            } else {
                throw new RuntimeException("Subject with uri '" + uri + "' does not exist in graph '" + graph + "'");
            }
        }
    }
    try {
        dataSet.getImportManager().generateLog(dataSet.getMetadata().getBaseUri(), null, new GraphQlToRdfPatch(graph.getUri(), uri, userUriCreator.create(user), new DeleteMutationChangeLog(graph, uri, entity))).get();
    } catch (LogStorageFailedException | JsonProcessingException | InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }
    return new RemovedEntity(graph.getUri(), uri);
}
Also used : User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) ImmutableContextData(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ImmutableContextData) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) DeleteMutationChangeLog(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.DeleteMutationChangeLog) Graph(nl.knaw.huygens.timbuctoo.v5.util.Graph) CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with DeleteMutationChangeLog

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.DeleteMutationChangeLog in project timbuctoo by HuygensING.

the class DeleteMutationChangeLogTest method getAdditionsReturnsNothing.

@Test
public void getAdditionsReturnsNothing() throws Exception {
    DeleteMutationChangeLog instance = new DeleteMutationChangeLog(new Graph(GRAPH), SUBJECT, null);
    Stream<Change> additions = instance.getAdditions(dataSet);
    assertThat(additions, StreamMatchers.empty());
}
Also used : Graph(nl.knaw.huygens.timbuctoo.v5.util.Graph) ChangeMatcher.likeChange(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.ChangeMatcher.likeChange) DeleteMutationChangeLog(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.DeleteMutationChangeLog) Test(org.junit.Test)

Example 3 with DeleteMutationChangeLog

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.DeleteMutationChangeLog in project timbuctoo by HuygensING.

the class DeleteMutationChangeLogTest method getDeletionsReturnsDeletions.

@Test
public void getDeletionsReturnsDeletions() throws Exception {
    String existingValue1 = "existingValue1";
    String existingValue2 = "existingValue2";
    DeleteMutationChangeLog instance = new DeleteMutationChangeLog(new Graph(GRAPH), SUBJECT, null);
    valuesInQuadStore(NAMES_PRED, existingValue1, existingValue2);
    List<Change> deletions = instance.getDeletions(dataSet).collect(toList());
    assertThat(deletions.size(), is(1));
    assertThat(deletions, contains(likeChange().valuesIsEmpty().withOldValues(new Value(existingValue1, STRING), new Value(existingValue2, STRING))));
}
Also used : Graph(nl.knaw.huygens.timbuctoo.v5.util.Graph) Value(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value) ChangeMatcher.likeChange(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.ChangeMatcher.likeChange) DeleteMutationChangeLog(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.DeleteMutationChangeLog) Test(org.junit.Test)

Example 4 with DeleteMutationChangeLog

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.DeleteMutationChangeLog in project timbuctoo by HuygensING.

the class DeleteMutationChangeLogTest method getReplacementsReturnsNothing.

@Test
public void getReplacementsReturnsNothing() throws Exception {
    DeleteMutationChangeLog instance = new DeleteMutationChangeLog(new Graph(GRAPH), SUBJECT, null);
    Stream<Change> replacements = instance.getReplacements(dataSet);
    assertThat(replacements, StreamMatchers.empty());
}
Also used : Graph(nl.knaw.huygens.timbuctoo.v5.util.Graph) ChangeMatcher.likeChange(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.ChangeMatcher.likeChange) DeleteMutationChangeLog(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.DeleteMutationChangeLog) Test(org.junit.Test)

Aggregations

DeleteMutationChangeLog (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.DeleteMutationChangeLog)4 Graph (nl.knaw.huygens.timbuctoo.v5.util.Graph)4 ChangeMatcher.likeChange (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.ChangeMatcher.likeChange)3 Test (org.junit.Test)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)1 CursorQuad (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad)1 LogStorageFailedException (nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException)1 ImmutableContextData (nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ImmutableContextData)1 Value (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value)1 User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)1