use of eu.europeana.enrichment.api.internal.ReferenceTermContext in project metis-framework by europeana.
the class MetisRecordParser method parseReferences.
@Override
public Set<ReferenceTermContext> parseReferences(RDF rdf) {
// Get all direct references (also look in Europeana proxy as it may have been dereferenced - we
// use this below to follow sameAs links).
final List<ProxyType> proxies = Optional.ofNullable(rdf.getProxyList()).stream().flatMap(Collection::stream).filter(Objects::nonNull).collect(Collectors.toList());
final Map<String, Set<ProxyFieldType>> directReferences = new HashMap<>();
for (ProxyFieldType field : ProxyFieldType.values()) {
final Set<String> directLinks = proxies.stream().map(field::extractFieldLinksForEnrichment).flatMap(Set::stream).collect(Collectors.toSet());
for (String directLink : directLinks) {
directReferences.computeIfAbsent(directLink, key -> new HashSet<>()).add(field);
}
}
// Get all sameAs links from the directly referenced contextual entities.
final Map<String, Set<ProxyFieldType>> indirectReferences = new HashMap<>();
final Consumer<AboutType> contextualTypeProcessor = contextualClass -> {
final Set<ProxyFieldType> linkTypes = Optional.ofNullable(directReferences.get(contextualClass.getAbout())).orElseGet(Collections::emptySet);
if (!linkTypes.isEmpty()) {
for (String sameAsLink : getSameAsLinks(contextualClass)) {
indirectReferences.computeIfAbsent(sameAsLink, key -> new HashSet<>()).addAll(linkTypes);
}
}
};
Optional.ofNullable(rdf.getAgentList()).orElseGet(Collections::emptyList).forEach(contextualTypeProcessor);
Optional.ofNullable(rdf.getConceptList()).orElseGet(Collections::emptyList).forEach(contextualTypeProcessor);
Optional.ofNullable(rdf.getPlaceList()).orElseGet(Collections::emptyList).forEach(contextualTypeProcessor);
Optional.ofNullable(rdf.getTimeSpanList()).orElseGet(Collections::emptyList).forEach(contextualTypeProcessor);
// Merge the two maps.
final Map<String, Set<ProxyFieldType>> resultMap = mergeMapInto(directReferences, indirectReferences);
// Clean up the result: no null values. But objects we already have need to
// stay: maybe they are matched using a sameAs link.
resultMap.remove(null);
// Convert and done
final Set<ReferenceTermContext> result = new HashSet<>();
for (Map.Entry<String, Set<ProxyFieldType>> entry : resultMap.entrySet()) {
ReferenceTermContext value;
try {
value = new ReferenceTermContext(new URL(entry.getKey()), entry.getValue());
result.add(value);
} catch (MalformedURLException e) {
LOGGER.debug("Invalid enrichment reference found: {}", entry.getKey());
}
}
return result;
}
Aggregations