Search in sources :

Example 1 with ReadOnlyChecker

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)))));
}
Also used : ReadOnlyChecker(nl.knaw.huygens.timbuctoo.v5.dataset.ReadOnlyChecker) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Matchers.containsString(org.hamcrest.Matchers.containsString) CustomProvenance(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CustomProvenance) Before(org.junit.Before)

Example 2 with ReadOnlyChecker

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);
    }
}
Also used : DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) IOException(java.io.IOException) DataSetCreationException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataSetCreationException) DataStorageSaveException(nl.knaw.huygens.timbuctoo.v5.datastorage.exceptions.DataStorageSaveException) PermissionFetchingException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.PermissionFetchingException) BasicDataSetMetaData(nl.knaw.huygens.timbuctoo.v5.dataset.dto.BasicDataSetMetaData) DataSetMetaData(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSetMetaData) BasicDataSetMetaData(nl.knaw.huygens.timbuctoo.v5.dataset.dto.BasicDataSetMetaData) DataStoreCreationException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataStoreCreationException) AuthorizationCreationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.AuthorizationCreationException)

Example 3 with ReadOnlyChecker

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);
                }
            }
        }
    }
}
Also used : DataSet.dataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet.dataSet) Set(java.util.Set) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) HashMap(java.util.HashMap) IOException(java.io.IOException) DataSetMetaData(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSetMetaData) BasicDataSetMetaData(nl.knaw.huygens.timbuctoo.v5.dataset.dto.BasicDataSetMetaData) DataStoreCreationException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataStoreCreationException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ReadOnlyChecker

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.");
}
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 5 with ReadOnlyChecker

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;
}
Also used : Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) Predicate(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate)

Aggregations

DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)4 DataSetMetaData (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSetMetaData)4 BasicDataSetMetaData (nl.knaw.huygens.timbuctoo.v5.dataset.dto.BasicDataSetMetaData)3 DataStoreCreationException (nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataStoreCreationException)3 Predicate (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate)3 IOException (java.io.IOException)2 ReadOnlyChecker (nl.knaw.huygens.timbuctoo.v5.dataset.ReadOnlyChecker)2 Type (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type)2 CustomProvenance (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CustomProvenance)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1