use of nl.knaw.huygens.timbuctoo.v5.dataset.ReadOnlyChecker in project timbuctoo by HuygensING.
the class DerivedInputTypeSchemaGeneratorTest method setUp.
@Before
public void setUp() throws Exception {
graphQlNameGenerator = mock(GraphQlNameGenerator.class);
when(graphQlNameGenerator.createObjectTypeName(ROOT_TYPE, TYPE_URI)).thenReturn(TYPE);
derivedSchemaContainer = mock(DerivedSchemaContainer.class);
when(derivedSchemaContainer.propertyInputType(anyList())).thenReturn("PropertyInput");
readOnlyChecker = new ReadOnlyChecker() {
@Override
public boolean isReadonlyPredicate(String predicateIri) {
return READ_ONLY_PRED.equals(predicateIri);
}
@Override
public boolean isReadonlyType(String typeUri) {
return READ_ONLY_TYPE.equals(typeUri);
}
};
instanceNoProv = new DerivedInputTypeSchemaGenerator(TYPE_URI, ROOT_TYPE, graphQlNameGenerator, derivedSchemaContainer, readOnlyChecker, new CustomProvenance(Collections.emptyList()));
instanceProv = new DerivedInputTypeSchemaGenerator(TYPE_URI, ROOT_TYPE, graphQlNameGenerator, derivedSchemaContainer, readOnlyChecker, CustomProvenance.getCustomProvenance(ImmutableMap.of("fields", Lists.newArrayList(ImmutableMap.of("uri", "http://example.org/name", "isList", false, "valueType", STRING)))));
}
use of nl.knaw.huygens.timbuctoo.v5.dataset.ReadOnlyChecker in project timbuctoo by HuygensING.
the class DataSetRepository method createDataSet.
public DataSet createDataSet(User user, String dataSetId, List<ImportInfo> importInfos) throws DataStoreCreationException, IllegalDataSetNameException, DataSetCreationException {
// The ownerId might not be valid (i.e. a safe string). We make it safe here:
// dataSetId is under the control of the user so we simply throw if it's not valid
String ownerPrefix = "u" + user.getPersistentId();
if (dataStorage.dataSetExists(ownerPrefix, dataSetId)) {
throw new DataSetCreationException("DataSet already exists on disk.");
}
final String baseUri = rdfIdHelper.dataSetBaseUri(ownerPrefix, dataSetId);
String uriPrefix;
if (!baseUri.endsWith("/") && !baseUri.endsWith("#") && !baseUri.endsWith("?")) {
// #boo&foo=bar
if (baseUri.contains("#") || baseUri.contains("?")) {
if (baseUri.endsWith("&")) {
uriPrefix = baseUri;
} else {
uriPrefix = baseUri + "&";
}
} else {
uriPrefix = baseUri + "/";
}
} else {
uriPrefix = baseUri;
}
final DataSetMetaData dataSet = new BasicDataSetMetaData(ownerPrefix, dataSetId, baseUri, uriPrefix, false, publicByDefault, importInfos);
try {
dataStorage.getDataSetStorage(ownerPrefix, dataSetId).saveMetaData(dataSet);
} catch (DataStorageSaveException e) {
throw new DataStoreCreationException(e);
}
synchronized (dataSetMap) {
Map<String, DataSet> userDataSets = dataSetMap.computeIfAbsent(ownerPrefix, key -> new HashMap<>());
if (!userDataSets.containsKey(dataSetId)) {
try {
permissionFetcher.initializeOwnerAuthorization(user, dataSet.getOwnerId(), dataSet.getDataSetId());
DataSet createdDataset = dataSet(dataSet, executorService, rdfBaseUri, dataStoreFactory, () -> onUpdated.accept(dataSet.getCombinedId()), dataStorage.getDataSetStorage(ownerPrefix, dataSetId), readOnlyChecker);
userDataSets.put(dataSetId, createdDataset);
dataSetsUpdatedListeners.forEach(createdDataset::subscribeToDataChanges);
} catch (PermissionFetchingException | AuthorizationCreationException | IOException e) {
LOG.error("Could not create data set", e);
throw new DataStoreCreationException(e);
}
}
return userDataSets.get(dataSetId);
}
}
use of nl.knaw.huygens.timbuctoo.v5.dataset.ReadOnlyChecker in project timbuctoo by HuygensING.
the class DataSetRepository method loadDataSetsFromJson.
private void loadDataSetsFromJson() throws IOException {
Map<String, Set<DataSetMetaData>> ownerMetadataMap = dataStorage.loadDataSetMetaData();
synchronized (dataSetMap) {
for (Map.Entry<String, Set<DataSetMetaData>> entry : ownerMetadataMap.entrySet()) {
String ownerId = entry.getKey();
Set<DataSetMetaData> ownerMetaDatas = entry.getValue();
HashMap<String, DataSet> ownersSets = new HashMap<>();
dataSetMap.put(ownerId, ownersSets);
for (DataSetMetaData dataSetMetaData : ownerMetaDatas) {
String dataSetName = dataSetMetaData.getDataSetId();
try {
DataSet value = dataSet(dataSetMetaData, executorService, rdfBaseUri, dataStoreFactory, () -> onUpdated.accept(dataSetMetaData.getCombinedId()), dataStorage.getDataSetStorage(ownerId, dataSetName), readOnlyChecker);
ownersSets.put(dataSetName, value);
dataSetsUpdatedListeners.forEach(value::subscribeToDataChanges);
} catch (DataStoreCreationException e) {
throw new IOException(e);
}
}
}
}
}
use of nl.knaw.huygens.timbuctoo.v5.dataset.ReadOnlyChecker 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.dataset.ReadOnlyChecker in project timbuctoo by HuygensING.
the class DerivedSchemaGenerator method makeGraphQlTypes.
public DerivedSchemaContainer makeGraphQlTypes(String rootType, Map<String, Type> types, TypeNameStore nameStore, ReadOnlyChecker readOnlyChecker, CustomProvenance customProvenance) {
GraphQlNameGenerator nameGenerator = new GraphQlNameGenerator(nameStore);
DerivedSchemaContainer typesContainer = new DerivedSchemaContainer(rootType, nameGenerator, this.argumentsHelper, readOnlyChecker, customProvenance);
// FIXME find a better way to register standard types to the schema of a data set
typesContainer.valueType(RdfConstants.MARKDOWN);
typesContainer.valueType(RdfConstants.STRING);
typesContainer.valueType(RdfConstants.URI);
for (Type type : types.values()) {
DerivedObjectTypeSchemaGenerator typeSchemaGenerator = typesContainer.addObjectType(type.getName());
for (Predicate predicate : type.getPredicates()) {
fieldForDerivedType(predicate, typesContainer, typeSchemaGenerator, nameGenerator, rootType);
}
}
return typesContainer;
}
Aggregations