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