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.");
}
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);
}
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);
}
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);
}
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.");
}
Aggregations