Search in sources :

Example 1 with MergeExplicitSchemas

use of nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeExplicitSchemas in project timbuctoo by HuygensING.

the class MergeExplicitSchemasTest method mergeeExplicitSchemaMergesSchemasWithDifferentCollections.

@Test
public void mergeeExplicitSchemaMergesSchemasWithDifferentCollections() throws Exception {
    Map<String, List<ExplicitField>> explicitSchema1 = new HashMap<>();
    ExplicitField explicitField1 = new ExplicitField("test:test1", false, Sets.newHashSet("String"), null);
    explicitSchema1.put("http://timbuctoo.huygens.knaw.nl/datasets/clusius/Places", Lists.newArrayList(explicitField1));
    Map<String, List<ExplicitField>> explicitSchema2 = new HashMap<>();
    ExplicitField explicitField2 = new ExplicitField("test:test2", false, null, Sets.newHashSet("String"));
    explicitSchema2.put("http://timbuctoo.huygens.knaw.nl/datasets/clusius/Persons", Lists.newArrayList(explicitField2));
    MergeExplicitSchemas mergeExplicitSchemas = new MergeExplicitSchemas();
    Map<String, List<ExplicitField>> mergedExplicitSchema = mergeExplicitSchemas.mergeExplicitSchemas(explicitSchema1, explicitSchema2);
    Map<String, List<ExplicitField>> expectedMergedSchema = new HashMap<>();
    expectedMergedSchema.put("http://timbuctoo.huygens.knaw.nl/datasets/clusius/Places", Lists.newArrayList(explicitField1));
    expectedMergedSchema.put("http://timbuctoo.huygens.knaw.nl/datasets/clusius/Persons", Lists.newArrayList(explicitField2));
    assertThat(mergedExplicitSchema, is(expectedMergedSchema));
}
Also used : ExplicitField(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitField) HashMap(java.util.HashMap) List(java.util.List) MergeExplicitSchemas(nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeExplicitSchemas) Test(org.junit.Test)

Example 2 with MergeExplicitSchemas

use of nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeExplicitSchemas in project timbuctoo by HuygensING.

the class ExtendSchemaMutation method get.

@Override
public Object get(DataFetchingEnvironment env) {
    DataSet dataSet = MutationHelpers.getDataSet(env, dataSetRepository::getDataSet);
    MutationHelpers.checkAdminPermissions(env, dataSet.getMetadata());
    final SchemaStore generatedSchema = dataSet.getSchemaStore();
    Map<String, Type> customTypes = new HashMap<>();
    List<ExplicitType> customSchema;
    Map<String, List<ExplicitField>> newCustomSchema = new HashMap<>();
    try {
        String customSchemaString = OBJECT_MAPPER.writeValueAsString(env.getArgument("customSchema"));
        customSchema = OBJECT_MAPPER.readValue(customSchemaString, new TypeReference<List<ExplicitType>>() {
        });
    } catch (IOException e) {
        throw new RuntimeException("Could not parse the schema input");
    }
    for (ExplicitType explicitType : customSchema) {
        customTypes.put(explicitType.getCollectionId(), explicitType.convertToType());
        newCustomSchema.put(explicitType.getCollectionId(), explicitType.getFields());
    }
    Map<String, List<ExplicitField>> existingCustomSchema = dataSet.getCustomSchema();
    Map<String, Type> existingCustomSchemaTypes = new HashMap<>();
    for (Map.Entry<String, List<ExplicitField>> entry : existingCustomSchema.entrySet()) {
        ExplicitType tempExplicitType = new ExplicitType(entry.getKey(), entry.getValue());
        existingCustomSchemaTypes.put(entry.getKey(), tempExplicitType.convertToType());
    }
    MergeSchemas mergeSchemas = new MergeSchemas();
    customTypes = mergeSchemas.mergeSchema(existingCustomSchemaTypes, customTypes);
    MergeExplicitSchemas mergeExplicitSchemas = new MergeExplicitSchemas();
    Map<String, List<ExplicitField>> mergedExplicitSchema = mergeExplicitSchemas.mergeExplicitSchemas(existingCustomSchema, newCustomSchema);
    try {
        dataSet.saveCustomSchema(mergedExplicitSchema);
    } catch (IOException e) {
        LOG.error("Saving the custom schema failed", e);
        throw new RuntimeException(e);
    }
    Map<String, Type> mergedSchema = mergeSchemas.mergeSchema(generatedSchema.getStableTypes(), customTypes);
    for (Map.Entry<String, Type> customType : customTypes.entrySet()) {
        if (!mergedSchema.containsKey(customType.getKey())) {
            return ImmutableMap.of("message", "Schema extension was unsuccessful.");
        }
    }
    return ImmutableMap.of("message", "Schema extended successfully.");
}
Also used : MergeSchemas(nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) SchemaStore(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.SchemaStore) HashMap(java.util.HashMap) MergeExplicitSchemas(nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeExplicitSchemas) IOException(java.io.IOException) ExplicitType(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitType) Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ImmutableMap(jersey.repackaged.com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) ExplicitType(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitType)

Example 3 with MergeExplicitSchemas

use of nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeExplicitSchemas in project timbuctoo by HuygensING.

the class MergeExplicitSchemas method mergeExplicitSchemas.

public Map<String, List<ExplicitField>> mergeExplicitSchemas(Map<String, List<ExplicitField>> explicitSchema1, Map<String, List<ExplicitField>> explicitSchema2) {
    Map<String, List<ExplicitField>> mergedExplicitSchema = new HashMap<>();
    for (Map.Entry<String, List<ExplicitField>> entry : explicitSchema1.entrySet()) {
        mergedExplicitSchema.put(entry.getKey(), entry.getValue());
    }
    explicitSchema2.forEach((collection, values) -> {
        if (mergedExplicitSchema.containsKey(collection)) {
            List<ExplicitField> mergedValues = new ArrayList<>();
            for (ExplicitField value : values) {
                mergedValues.add(findAndMergeExplicitFields(collection, value, mergedExplicitSchema));
            }
            for (ExplicitField value : mergedExplicitSchema.get(collection)) {
                boolean addValue = true;
                for (ExplicitField mergedValue : mergedValues) {
                    if (mergedValue.getUri().equals(value.getUri())) {
                        addValue = false;
                        break;
                    }
                }
                if (addValue) {
                    mergedValues.add(value);
                }
            }
            mergedExplicitSchema.put(collection, mergedValues);
        } else {
            mergedExplicitSchema.put(collection, values);
        }
    });
    return mergedExplicitSchema;
}
Also used : ExplicitField(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with MergeExplicitSchemas

use of nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeExplicitSchemas in project timbuctoo by HuygensING.

the class MergeExplicitSchemasTest method mergeExplicitSchemaMergesFieldsForSameCollection.

@Test
public void mergeExplicitSchemaMergesFieldsForSameCollection() throws Exception {
    Map<String, List<ExplicitField>> explicitSchema1 = new HashMap<>();
    ExplicitField explicitField1 = new ExplicitField("test:test1", false, Sets.newHashSet("String"), null);
    explicitSchema1.put("http://timbuctoo.huygens.knaw.nl/datasets/clusius/Places", Lists.newArrayList(explicitField1));
    Map<String, List<ExplicitField>> explicitSchema2 = new HashMap<>();
    ExplicitField explicitField2 = new ExplicitField("test:test2", false, null, Sets.newHashSet("String"));
    explicitSchema2.put("http://timbuctoo.huygens.knaw.nl/datasets/clusius/Places", Lists.newArrayList(explicitField2));
    MergeExplicitSchemas mergeExplicitSchemas = new MergeExplicitSchemas();
    Map<String, List<ExplicitField>> mergedExplicitSchema = mergeExplicitSchemas.mergeExplicitSchemas(explicitSchema1, explicitSchema2);
    assertThat(mergedExplicitSchema, hasKey("http://timbuctoo.huygens.knaw.nl/datasets/clusius/Places"));
    assertThat(mergedExplicitSchema.get("http://timbuctoo.huygens.knaw.nl/datasets/clusius/Places"), containsInAnyOrder(explicitField1, explicitField2));
}
Also used : ExplicitField(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitField) HashMap(java.util.HashMap) List(java.util.List) MergeExplicitSchemas(nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeExplicitSchemas) Test(org.junit.Test)

Example 5 with MergeExplicitSchemas

use of nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeExplicitSchemas in project timbuctoo by HuygensING.

the class MergeExplicitSchemasTest method mergeExplicitSchemaMergesFieldWithSameUriForSameCollection.

@Test
public void mergeExplicitSchemaMergesFieldWithSameUriForSameCollection() throws Exception {
    Map<String, List<ExplicitField>> explicitSchema1 = new HashMap<>();
    ExplicitField explicitField1 = new ExplicitField("test:test1", false, Sets.newHashSet("String"), null);
    explicitSchema1.put("http://timbuctoo.huygens.knaw.nl/datasets/clusius/Places", Lists.newArrayList(explicitField1));
    Map<String, List<ExplicitField>> explicitSchema2 = new HashMap<>();
    ExplicitField explicitField2 = new ExplicitField("test:test1", false, Sets.newHashSet("Integer"), null);
    explicitSchema2.put("http://timbuctoo.huygens.knaw.nl/datasets/clusius/Places", Lists.newArrayList(explicitField2));
    MergeExplicitSchemas mergeExplicitSchemas = new MergeExplicitSchemas();
    Map<String, List<ExplicitField>> mergedExplicitSchema = mergeExplicitSchemas.mergeExplicitSchemas(explicitSchema1, explicitSchema2);
    assertThat(mergedExplicitSchema, hasKey("http://timbuctoo.huygens.knaw.nl/datasets/clusius/Places"));
    MatcherAssert.assertThat(mergedExplicitSchema.get("http://timbuctoo.huygens.knaw.nl/datasets/clusius/Places"), Matchers.contains(ExplicitFieldMatcher.explicitField().withValues(Sets.newHashSet("Integer", "String"))));
}
Also used : ExplicitField(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitField) HashMap(java.util.HashMap) List(java.util.List) MergeExplicitSchemas(nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeExplicitSchemas) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)5 List (java.util.List)5 ExplicitField (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitField)4 MergeExplicitSchemas (nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeExplicitSchemas)4 Test (org.junit.Test)3 Map (java.util.Map)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ImmutableMap (jersey.repackaged.com.google.common.collect.ImmutableMap)1 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)1 SchemaStore (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.SchemaStore)1 ExplicitType (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitType)1 Type (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type)1 MergeSchemas (nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas)1