Search in sources :

Example 1 with ImmutableContextData

use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ImmutableContextData in project timbuctoo by HuygensING.

the class DefaultIndexConfigMutation method executeAction.

@Override
protected Object executeAction(DataFetchingEnvironment env) {
    final User user = MutationHelpers.getUser(env);
    Optional<DataSet> dataSetOpt = dataSetRepository.getDataSet(user, ownerId, dataSetName);
    if (!dataSetOpt.isPresent()) {
        throw new RuntimeException("Dataset does not exist");
    }
    final DataSet dataSet = dataSetOpt.get();
    ImmutableContextData contextData = env.getContext();
    if (!contextData.getUserPermissionCheck().hasPermission(dataSet.getMetadata(), Permission.CONFIG_INDEX)) {
        throw new RuntimeException("User has no permissions to change the index configuration.");
    }
    final ReadOnlyChecker readOnlyChecker = dataSet.getReadOnlyChecker();
    final PredicateMutation predicateMutation = new PredicateMutation();
    final TypeNameStore typeNameStore = dataSet.getTypeNameStore();
    final PredicateMutation[] resetIndexPredicates = dataSet.getSchemaStore().getStableTypes().values().stream().map(Type::getName).filter(uri -> !readOnlyChecker.isReadonlyType(uri)).map(collectionUri -> createIndexPredicate(predicateMutation, typeNameStore, collectionUri)).toArray(PredicateMutation[]::new);
    try {
        MutationHelpers.addMutations(dataSet, resetIndexPredicates);
    } catch (LogStorageFailedException | ExecutionException | InterruptedException e) {
        throw new RuntimeException(e);
    }
    return ImmutableMap.of("message", "Index is rest.");
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) PredicateMutation.value(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation.value) ImmutableMap(com.google.common.collect.ImmutableMap) DataSetRepository(nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository) Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) Tuple(nl.knaw.huygens.timbuctoo.util.Tuple) TIM_HASINDEXERCONFIG(nl.knaw.huygens.timbuctoo.v5.util.RdfConstants.TIM_HASINDEXERCONFIG) Permission(nl.knaw.huygens.timbuctoo.v5.security.dto.Permission) ExecutionException(java.util.concurrent.ExecutionException) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) ImmutableContextData(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ImmutableContextData) DataSetMetaData(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSetMetaData) Optional(java.util.Optional) 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) TypeNameStore(nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore) ReadOnlyChecker(nl.knaw.huygens.timbuctoo.v5.dataset.ReadOnlyChecker) PredicateMutation(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) ImmutableContextData(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ImmutableContextData) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) ReadOnlyChecker(nl.knaw.huygens.timbuctoo.v5.dataset.ReadOnlyChecker) TypeNameStore(nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore) PredicateMutation(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ImmutableContextData

use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ImmutableContextData 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 3 with ImmutableContextData

use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ImmutableContextData in project timbuctoo by HuygensING.

the class EditMutation 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.WRITE)) {
        throw new RuntimeException("User should have permissions to edit 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 + "' already exists in the default graph");
            } else {
                throw new RuntimeException("Subject with uri '" + uri + "' already exists in graph '" + graph + "'");
            }
        }
    }
    try {
        dataSet.getImportManager().generateLog(dataSet.getMetadata().getBaseUri(), null, new GraphQlToRdfPatch(graph.getUri(), uri, userUriCreator.create(user), new EditMutationChangeLog(graph, uri, entity))).get();
    } catch (LogStorageFailedException | JsonProcessingException | InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }
    return subjectFetcher.getItemInGraph(uri, Optional.of(graph), dataSet);
}
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) EditMutationChangeLog(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.EditMutationChangeLog) 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 4 with ImmutableContextData

use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ImmutableContextData in project timbuctoo by HuygensING.

the class CreateMutation 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.CREATE)) {
        throw new RuntimeException("User should have permissions to create entities of the data set.");
    }
    try (Stream<CursorQuad> quads = dataSet.getQuadStore().getQuadsInGraph(uri, Optional.of(graph))) {
        if (quads.findAny().isPresent()) {
            if (graph.isDefaultGraph()) {
                throw new RuntimeException("Subject with uri '" + uri + "' already exists in the default graph");
            } else {
                throw new RuntimeException("Subject with uri '" + uri + "' already exists in graph '" + graph + "'");
            }
        }
    }
    try {
        dataSet.getImportManager().generateLog(dataSet.getMetadata().getBaseUri(), null, new GraphQlToRdfPatch(graph.getUri(), uri, userUriCreator.create(user), new CreateMutationChangeLog(graph, uri, typeUri, entity))).get();
    } catch (LogStorageFailedException | JsonProcessingException | InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }
    return subjectFetcher.getItemInGraph(uri, Optional.of(graph), dataSet);
}
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) 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) CreateMutationChangeLog(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog)

Example 5 with ImmutableContextData

use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ImmutableContextData 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.");
}
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) TreeNode(com.fasterxml.jackson.core.TreeNode) IOException(java.io.IOException) CustomProvenance(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CustomProvenance)

Aggregations

DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)5 ImmutableContextData (nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ImmutableContextData)5 User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)5 ExecutionException (java.util.concurrent.ExecutionException)4 LogStorageFailedException (nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 Map (java.util.Map)3 CursorQuad (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad)3 Graph (nl.knaw.huygens.timbuctoo.v5.util.Graph)3 TreeNode (com.fasterxml.jackson.core.TreeNode)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1 IOException (java.io.IOException)1 Optional (java.util.Optional)1 Tuple (nl.knaw.huygens.timbuctoo.util.Tuple)1 DataSetRepository (nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository)1 ReadOnlyChecker (nl.knaw.huygens.timbuctoo.v5.dataset.ReadOnlyChecker)1 DataSetMetaData (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSetMetaData)1 TypeNameStore (nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore)1 Type (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type)1