private Map<String, String> getCurrentParts(MetadataKey key) throws MetadataResolvingException { Map<String, String> keyParts = new HashMap<>(); keyParts.put(key.getPartName(), key.getId()); while (!key.getChilds().isEmpty()) { if (key.getChilds().size() > 1) { final List<String> keyNames = key.getChilds().stream().map(MetadataKey::getId).collect(toList()); throw buildException(format("MetadataKey used for Metadata resolution must only have one child per level. " + "Key '%s' has %s as children.", key.getId(), keyNames)); } key = key.getChilds().iterator().next(); keyParts.put(key.getPartName(), key.getId()); } return keyParts; }
protected <T extends ComponentModel> void assertResolvedKey(MetadataResult<ComponentMetadataDescriptor<T>> result, MetadataKey metadataKey) { assertThat(result.get().getMetadataAttributes().getKey().isPresent(), is(true)); MetadataKey resultKey = result.get().getMetadataAttributes().getKey().get(); assertSameKey(metadataKey, resultKey); MetadataKey child = metadataKey.getChilds().stream().findFirst().orElseGet(() -> null); MetadataKey otherChild = resultKey.getChilds().stream().findFirst().orElseGet(() -> null); while (child != null && otherChild != null) { assertSameKey(child, otherChild); child = child.getChilds().stream().findFirst().orElseGet(() -> null); otherChild = otherChild.getChilds().stream().findFirst().orElseGet(() -> null); } assertThat(child == null && otherChild == null, is(true)); }
/** * Given a {@link MetadataKey}, this is navigated recursively cloning each {@link MetadataKey} of the tree structure creating a * {@link MultilevelMetadataKeyBuilder} and adding the partName of each {@link MetadataKey} found. * * @param key {@link MetadataKey} to be cloned and enriched * @param partOrderMapping {@link Map} that contains the mapping of the name of each part of the {@link MetadataKey} * @param level the current level of the part of the {@link MetadataKey} to be cloned and enriched * @return a {@link MetadataKeyBuilder} with the cloned and enriched keys */ private MetadataKeyBuilder cloneAndEnrichMetadataKey(MetadataKey key, Map<Integer, ParameterModel> partOrderMapping, int level) { final MetadataKeyBuilder keyBuilder = newKey(key.getId(), partOrderMapping.get(level).getName()).withDisplayName(key.getDisplayName()); key.getProperties().forEach(keyBuilder::withProperty); key.getChilds().forEach(childKey -> keyBuilder.withChild(cloneAndEnrichMetadataKey(childKey, partOrderMapping, level + 1))); return keyBuilder; } }
@Test public void twoLevelPartialMultilevelKeys() throws Exception { location = Location.builder().globalName("twoLevelPartialMultiLevelKeyResolver").addProcessorsPart().addIndexPart(0).build(); final MetadataResult<MetadataKeysContainer> metadataKeysResult = metadataService.getMetadataKeys(location); assertSuccessResult(metadataKeysResult); final Set<MetadataKey> continents = getKeysFromContainer(metadataKeysResult.get()); assertThat(continents, hasSize(1)); Set<MetadataKey> countries = continents.iterator().next().getChilds(); assertThat(countries, hasSize(1)); Set<MetadataKey> cities = countries.iterator().next().getChilds(); assertThat(cities, hasSize(2)); assertThat(cities, hasItem(metadataKeyWithId(BUENOS_AIRES).withPartName(CITY))); assertThat(cities, hasItem(metadataKeyWithId(LA_PLATA).withPartName(CITY))); }
public MetadataResult<MetadataKeysContainer> getEntityKeys(MetadataContext context) { try { QueryEntityResolver queryEntityResolver = resolverFactory.getQueryEntityResolver(); Set<MetadataKey> entityKeys = queryEntityResolver.getEntityKeys(context); final MetadataKeysContainerBuilder keyBuilder = MetadataKeysContainerBuilder.getInstance(); if (entityKeys.stream().anyMatch(key -> key.getChilds().size() > 0)) { return failure(newFailure() .withMessage("Error retrieving entity keys, Only single level keys are supported for entity metadata") .withReason("There are at least one key that contains childs") .withFailureCode(INVALID_METADATA_KEY).onKeys()); } return success(keyBuilder.add(queryEntityResolver.getResolverName(), entityKeys).build()); } catch (Exception e) { return failure(newFailure(e).onKeys()); } }
private Map<String, String> getCurrentParts(MetadataKey key) throws MetadataResolvingException { Map<String, String> keyParts = new HashMap<>(); keyParts.put(key.getPartName(), key.getId()); while (!key.getChilds().isEmpty()) { if (key.getChilds().size() > 1) { final List<String> keyNames = key.getChilds().stream().map(MetadataKey::getId).collect(toList()); throw buildException(format("MetadataKey used for Metadata resolution must only have one child per level. " + "Key '%s' has %s as children.", key.getId(), keyNames)); } key = key.getChilds().iterator().next(); keyParts.put(key.getPartName(), key.getId()); } return keyParts; }
protected <T extends ComponentModel> void assertResolvedKey(MetadataResult<ComponentMetadataDescriptor<T>> result, MetadataKey metadataKey) { assertThat(result.get().getMetadataAttributes().getKey().isPresent(), is(true)); MetadataKey resultKey = result.get().getMetadataAttributes().getKey().get(); assertSameKey(metadataKey, resultKey); MetadataKey child = metadataKey.getChilds().stream().findFirst().orElseGet(() -> null); MetadataKey otherChild = resultKey.getChilds().stream().findFirst().orElseGet(() -> null); while (child != null && otherChild != null) { assertSameKey(child, otherChild); child = child.getChilds().stream().findFirst().orElseGet(() -> null); otherChild = otherChild.getChilds().stream().findFirst().orElseGet(() -> null); } assertThat(child == null && otherChild == null, is(true)); }
/** * Given a {@link MetadataKey}, this is navigated recursively cloning each {@link MetadataKey} of the tree structure creating a * {@link MultilevelMetadataKeyBuilder} and adding the partName of each {@link MetadataKey} found. * * @param key {@link MetadataKey} to be cloned and enriched * @param partOrderMapping {@link Map} that contains the mapping of the name of each part of the {@link MetadataKey} * @param level the current level of the part of the {@link MetadataKey} to be cloned and enriched * @return a {@link MetadataKeyBuilder} with the cloned and enriched keys */ private MetadataKeyBuilder cloneAndEnrichMetadataKey(MetadataKey key, Map<Integer, ParameterModel> partOrderMapping, int level) { final MetadataKeyBuilder keyBuilder = newKey(key.getId(), partOrderMapping.get(level).getName()).withDisplayName(key.getDisplayName()); key.getProperties().forEach(keyBuilder::withProperty); key.getChilds().forEach(childKey -> keyBuilder.withChild(cloneAndEnrichMetadataKey(childKey, partOrderMapping, level + 1))); return keyBuilder; } }
@Test public void twoLevelPartialMultilevelKeys() throws Exception { location = Location.builder().globalName("twoLevelPartialMultiLevelKeyResolver").addProcessorsPart().addIndexPart(0).build(); final MetadataResult<MetadataKeysContainer> metadataKeysResult = metadataService.getMetadataKeys(location); assertSuccessResult(metadataKeysResult); final Set<MetadataKey> continents = getKeysFromContainer(metadataKeysResult.get()); assertThat(continents, hasSize(1)); Set<MetadataKey> countries = continents.iterator().next().getChilds(); assertThat(countries, hasSize(1)); Set<MetadataKey> cities = countries.iterator().next().getChilds(); assertThat(cities, hasSize(2)); assertThat(cities, hasItem(metadataKeyWithId(BUENOS_AIRES).withPartName(CITY))); assertThat(cities, hasItem(metadataKeyWithId(LA_PLATA).withPartName(CITY))); }
public MetadataResult<MetadataKeysContainer> getEntityKeys(MetadataContext context) { try { QueryEntityResolver queryEntityResolver = resolverFactory.getQueryEntityResolver(); Set<MetadataKey> entityKeys = queryEntityResolver.getEntityKeys(context); final MetadataKeysContainerBuilder keyBuilder = MetadataKeysContainerBuilder.getInstance(); if (entityKeys.stream().anyMatch(key -> key.getChilds().size() > 0)) { return failure(newFailure() .withMessage("Error retrieving entity keys, Only single level keys are supported for entity metadata") .withReason("There are at least one key that contains childs") .withFailureCode(INVALID_METADATA_KEY).onKeys()); } return success(keyBuilder.add(queryEntityResolver.getResolverName(), entityKeys).build()); } catch (Exception e) { return failure(newFailure(e).onKeys()); } }