Search in sources :

Example 1 with ProxyFieldType

use of eu.europeana.enrichment.api.internal.ProxyFieldType 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;
}
Also used : Concept(eu.europeana.metis.schema.jibx.Concept) URL(java.net.URL) AggregationFieldType(eu.europeana.enrichment.api.internal.AggregationFieldType) TimeSpanType(eu.europeana.metis.schema.jibx.TimeSpanType) LoggerFactory(org.slf4j.LoggerFactory) RecordParser(eu.europeana.enrichment.api.internal.RecordParser) HashMap(java.util.HashMap) RDF(eu.europeana.metis.schema.jibx.RDF) StringUtils(org.apache.commons.lang3.StringUtils) ProxyFieldType(eu.europeana.enrichment.api.internal.ProxyFieldType) ResourceType(eu.europeana.metis.schema.jibx.ResourceType) HashSet(java.util.HashSet) AboutType(eu.europeana.metis.schema.jibx.AboutType) RdfEntityUtils(eu.europeana.enrichment.utils.RdfEntityUtils) FieldType(eu.europeana.enrichment.api.internal.FieldType) SearchTermContext(eu.europeana.enrichment.api.internal.SearchTermContext) Map(java.util.Map) FieldValue(eu.europeana.enrichment.api.internal.FieldValue) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) PlaceType(eu.europeana.metis.schema.jibx.PlaceType) ReferenceTermContext(eu.europeana.enrichment.api.internal.ReferenceTermContext) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) AgentType(eu.europeana.metis.schema.jibx.AgentType) Objects(java.util.Objects) Consumer(java.util.function.Consumer) List(java.util.List) ProxyType(eu.europeana.metis.schema.jibx.ProxyType) Optional(java.util.Optional) Collections(java.util.Collections) MalformedURLException(java.net.MalformedURLException) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ReferenceTermContext(eu.europeana.enrichment.api.internal.ReferenceTermContext) URL(java.net.URL) ProxyFieldType(eu.europeana.enrichment.api.internal.ProxyFieldType) Collection(java.util.Collection) ProxyType(eu.europeana.metis.schema.jibx.ProxyType) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) AboutType(eu.europeana.metis.schema.jibx.AboutType)

Example 2 with ProxyFieldType

use of eu.europeana.enrichment.api.internal.ProxyFieldType in project metis-framework by europeana.

the class RdfEntityUtilsTest method testAppendLinkToEuropeanaProxyAddSameChoiceType.

@Test
void testAppendLinkToEuropeanaProxyAddSameChoiceType() {
    Choice choice = new Choice();
    Coverage coverage = new Coverage();
    Resource resource = new Resource();
    resource.setResource("http://differentdummylink.com");
    coverage.setResource(resource);
    choice.setCoverage(coverage);
    List<Choice> choices = new ArrayList<>();
    choices.add(choice);
    PROXY_EUROPEANA.setChoiceList(choices);
    TEST_RDF.setProxyList(Collections.singletonList(PROXY_EUROPEANA));
    String link = "http://dummylink.com";
    Set<ProxyFieldType> linkTypes = new HashSet<>();
    linkTypes.add(ProxyFieldType.DC_COVERAGE);
    RdfEntityUtils.appendLinkToEuropeanaProxy(TEST_RDF, link, linkTypes);
    assertEquals(2, TEST_RDF.getProxyList().get(0).getChoiceList().size());
    assertEquals("http://differentdummylink.com", TEST_RDF.getProxyList().get(0).getChoiceList().get(0).getCoverage().getResource().getResource());
    assertEquals(link, TEST_RDF.getProxyList().get(0).getChoiceList().get(1).getCoverage().getResource().getResource());
}
Also used : ProxyFieldType(eu.europeana.enrichment.api.internal.ProxyFieldType) Choice(eu.europeana.metis.schema.jibx.EuropeanaType.Choice) Resource(eu.europeana.metis.schema.jibx.ResourceOrLiteralType.Resource) ArrayList(java.util.ArrayList) Coverage(eu.europeana.metis.schema.jibx.Coverage) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with ProxyFieldType

use of eu.europeana.enrichment.api.internal.ProxyFieldType in project metis-framework by europeana.

the class RdfEntityUtilsTest method testAppendLinkToEuropeanaProxyAlreadyExists.

@Test
void testAppendLinkToEuropeanaProxyAlreadyExists() {
    String link = "http://dummylink.com";
    Choice choice = new Choice();
    Coverage coverage = new Coverage();
    Resource resource = new Resource();
    resource.setResource(link);
    coverage.setResource(resource);
    choice.setCoverage(coverage);
    List<Choice> choices = new ArrayList<>();
    choices.add(choice);
    PROXY_EUROPEANA.setChoiceList(choices);
    TEST_RDF.setProxyList(Collections.singletonList(PROXY_EUROPEANA));
    Set<ProxyFieldType> linkTypes = new HashSet<>();
    linkTypes.add(ProxyFieldType.DC_COVERAGE);
    RdfEntityUtils.appendLinkToEuropeanaProxy(TEST_RDF, link, linkTypes);
    assertEquals(1, TEST_RDF.getProxyList().get(0).getChoiceList().size());
    assertEquals(link, TEST_RDF.getProxyList().get(0).getChoiceList().get(0).getCoverage().getResource().getResource());
}
Also used : ProxyFieldType(eu.europeana.enrichment.api.internal.ProxyFieldType) Choice(eu.europeana.metis.schema.jibx.EuropeanaType.Choice) Resource(eu.europeana.metis.schema.jibx.ResourceOrLiteralType.Resource) ArrayList(java.util.ArrayList) Coverage(eu.europeana.metis.schema.jibx.Coverage) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with ProxyFieldType

use of eu.europeana.enrichment.api.internal.ProxyFieldType in project metis-framework by europeana.

the class EnricherImpl method cleanupPreviousEnrichmentEntities.

@Override
public void cleanupPreviousEnrichmentEntities(RDF rdf) {
    final ProxyType europeanaProxy = RdfEntityUtils.getEuropeanaProxy(rdf);
    // Find the correct links
    final Set<String> europeanaLinks = Arrays.stream(ProxyFieldType.values()).map(proxyFieldType -> proxyFieldType.extractFieldLinksForEnrichment(europeanaProxy)).flatMap(Collection::stream).filter(europeanaLinkPattern.asPredicate()).collect(Collectors.toSet());
    RdfEntityUtils.removeMatchingEntities(rdf, europeanaLinks);
}
Also used : EntityMergeEngine(eu.europeana.enrichment.utils.EntityMergeEngine) EnrichmentBase(eu.europeana.enrichment.api.external.model.EnrichmentBase) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) EntityResolver(eu.europeana.enrichment.api.internal.EntityResolver) ReferenceTermContext(eu.europeana.enrichment.api.internal.ReferenceTermContext) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) RecordParser(eu.europeana.enrichment.api.internal.RecordParser) Set(java.util.Set) EnrichmentException(eu.europeana.enrichment.rest.client.exceptions.EnrichmentException) RDF(eu.europeana.metis.schema.jibx.RDF) Collectors(java.util.stream.Collectors) ProxyFieldType(eu.europeana.enrichment.api.internal.ProxyFieldType) RdfEntityUtils(eu.europeana.enrichment.utils.RdfEntityUtils) List(java.util.List) CollectionUtils(org.apache.commons.collections.CollectionUtils) SearchTermContext(eu.europeana.enrichment.api.internal.SearchTermContext) EnrichmentUtils(eu.europeana.enrichment.utils.EnrichmentUtils) Map(java.util.Map) ProxyType(eu.europeana.metis.schema.jibx.ProxyType) Entry(java.util.Map.Entry) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Collection(java.util.Collection) ProxyType(eu.europeana.metis.schema.jibx.ProxyType)

Example 5 with ProxyFieldType

use of eu.europeana.enrichment.api.internal.ProxyFieldType in project metis-framework by europeana.

the class RdfEntityUtils method appendLinkToEuropeanaProxy.

/**
 * Add a link to the specified {@link AboutType} to the EuropeanaProxy.
 *
 * @param rdf the rdf to append to
 * @param link the about value to link
 * @param linkTypes the types of the link to add in the europeana proxy.
 */
public static void appendLinkToEuropeanaProxy(RDF rdf, String link, Set<ProxyFieldType> linkTypes) {
    final Map<ProxyFieldType, Set<String>> allProxyLinksPerType = getAllProxyLinksPerType(rdf);
    final ProxyType europeanaProxy = getEuropeanaProxy(rdf);
    for (ProxyFieldType linkType : linkTypes) {
        final boolean alreadyExists = Optional.ofNullable(allProxyLinksPerType.get(linkType)).orElseGet(Collections::emptySet).contains(link);
        if (!alreadyExists) {
            final List<EuropeanaType.Choice> choices = Optional.ofNullable(europeanaProxy.getChoiceList()).orElseGet(ArrayList::new);
            choices.add(linkType.createChoice(link));
            europeanaProxy.setChoiceList(choices);
        }
    }
    replaceProxy(rdf, europeanaProxy);
}
Also used : ProxyFieldType(eu.europeana.enrichment.api.internal.ProxyFieldType) Set(java.util.Set) ArrayList(java.util.ArrayList) ProxyType(eu.europeana.metis.schema.jibx.ProxyType)

Aggregations

ProxyFieldType (eu.europeana.enrichment.api.internal.ProxyFieldType)6 ProxyType (eu.europeana.metis.schema.jibx.ProxyType)4 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 List (java.util.List)3 RecordParser (eu.europeana.enrichment.api.internal.RecordParser)2 ReferenceTermContext (eu.europeana.enrichment.api.internal.ReferenceTermContext)2 SearchTermContext (eu.europeana.enrichment.api.internal.SearchTermContext)2 RdfEntityUtils (eu.europeana.enrichment.utils.RdfEntityUtils)2 Coverage (eu.europeana.metis.schema.jibx.Coverage)2 Choice (eu.europeana.metis.schema.jibx.EuropeanaType.Choice)2 RDF (eu.europeana.metis.schema.jibx.RDF)2 Resource (eu.europeana.metis.schema.jibx.ResourceOrLiteralType.Resource)2 Collections (java.util.Collections)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.jupiter.api.Test)2 Logger (org.slf4j.Logger)2