From 0f337b32a1ed8f01c91745434c672543704fbf9c Mon Sep 17 00:00:00 2001 From: jochen_vermeulen Date: Thu, 25 Jul 2024 15:57:21 +0200 Subject: [PATCH] MET-6053: Some code improvements. --- .../service/MongoDereferenceService.java | 61 ++++++++++++------- .../service/TransformedEntity.java | 16 +++-- .../service/MongoDereferenceServiceTest.java | 24 ++++---- 3 files changed, 62 insertions(+), 39 deletions(-) diff --git a/metis-dereference/metis-dereference-service/src/main/java/eu/europeana/metis/dereference/service/MongoDereferenceService.java b/metis-dereference/metis-dereference-service/src/main/java/eu/europeana/metis/dereference/service/MongoDereferenceService.java index ee6d6f8aa..9ddff5644 100644 --- a/metis-dereference/metis-dereference-service/src/main/java/eu/europeana/metis/dereference/service/MongoDereferenceService.java +++ b/metis-dereference/metis-dereference-service/src/main/java/eu/europeana/metis/dereference/service/MongoDereferenceService.java @@ -22,8 +22,8 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.Collection; +import java.util.EnumSet; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; @@ -43,6 +43,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +/** + * Implementation of {@link DereferenceService} that uses the MongoDB for retrieving vocabularies + * and for caching. + */ @Component public class MongoDereferenceService implements DereferenceService { @@ -95,8 +99,8 @@ public DereferenceResult dereference(String resourceId) { // Deserialize the entity final EnrichmentBase deserializedEntity; try { - deserializedEntity = resource.getTransformedEntity() == null ? null - : EnrichmentBaseConverter.convertToEnrichmentBase(resource.getTransformedEntity()); + deserializedEntity = resource.getEntity() == null ? null + : EnrichmentBaseConverter.convertToEnrichmentBase(resource.getEntity()); } catch (JAXBException e) { LOGGER.info("Problem occurred while parsing transformed entity {}.", resourceId, e); return new DereferenceResult(DereferenceResultStatus.ENTITY_FOUND_XML_XSLT_ERROR); @@ -127,8 +131,8 @@ private Pair resolveBroaderValue(String final TransformedEntity resource = dereferenceSingleResource(resourceId); final EnrichmentBase deserializedEntity; try { - deserializedEntity = resource.getTransformedEntity() == null ? null - : EnrichmentBaseConverter.convertToEnrichmentBase(resource.getTransformedEntity()); + deserializedEntity = resource.getEntity() == null ? null + : EnrichmentBaseConverter.convertToEnrichmentBase(resource.getEntity()); } catch (JAXBException e) { LOGGER.info("Problem occurred while parsing transformed entity {}.", resourceId, e); return new ImmutablePair<>(null, DereferenceResultStatus.ENTITY_FOUND_XML_XSLT_ERROR); @@ -179,31 +183,21 @@ private TransformedEntity dereferenceSingleResource(String resourceId) { private TransformedEntity performDereferenceAlgorithmForSingleResource(String resourceId) { // Find matching vocabularies, report if there are none. - final VocabularyCandidates vocabularyCandidates; - try { - vocabularyCandidates = VocabularyCandidates.findVocabulariesForUrl(resourceId, - vocabularyDao::getByUriSearch); - } catch (URISyntaxException e) { - // Shouldn't happen as we checked this before. - LOGGER.warn(String.format("Problem occurred while dereferencing resource %s.", - resourceId), e); - return new TransformedEntity(null, null, DereferenceResultStatus.FAILURE); - } - if (vocabularyCandidates.isEmpty()) { - return new TransformedEntity(null, null, - DereferenceResultStatus.NO_VOCABULARY_MATCHING); + final Pair vocabularyCandidates = getCandidateVocabularies(resourceId); + if (vocabularyCandidates.getRight() != DereferenceResultStatus.SUCCESS) { + return new TransformedEntity(null, null, vocabularyCandidates.getRight()); } // If there are vocabularies, we attempt to obtain the original entity from source. final OriginalEntity originalEntity = retrieveOriginalEntity(resourceId, - vocabularyCandidates.getVocabulariesSuffixes()); + vocabularyCandidates.getLeft().getVocabulariesSuffixes()); if (originalEntity.getResultStatus() != DereferenceResultStatus.SUCCESS) { return new TransformedEntity(null, null, originalEntity.getResultStatus()); } // If we managed to obtain the original entity, we will try to transform it. - final Set statuses = new HashSet<>(); - for (Vocabulary vocabulary : vocabularyCandidates.getVocabularies()) { + final Set statuses = EnumSet.noneOf(DereferenceResultStatus.class); + for (Vocabulary vocabulary : vocabularyCandidates.getLeft().getVocabularies()) { final TransformedEntity transformedEntity = transformEntity(vocabulary, originalEntity.getEntity(), resourceId); if (transformedEntity.getResultStatus() == DereferenceResultStatus.SUCCESS) { @@ -220,6 +214,29 @@ private TransformedEntity performDereferenceAlgorithmForSingleResource(String re return new TransformedEntity(null, null, status); } + private Pair getCandidateVocabularies(String resourceId) { + + // Find matching vocabularies. + final VocabularyCandidates vocabularyCandidates; + try { + vocabularyCandidates = VocabularyCandidates.findVocabulariesForUrl(resourceId, + vocabularyDao::getByUriSearch); + } catch (URISyntaxException e) { + // Shouldn't happen as we checked this before. + LOGGER.warn(String.format("Problem occurred while dereferencing resource %s.", + resourceId), e); + return new ImmutablePair<>(null, DereferenceResultStatus.FAILURE); + } + + // Report if there are none. + if (vocabularyCandidates.isEmpty()) { + return new ImmutablePair<>(null, DereferenceResultStatus.NO_VOCABULARY_MATCHING); + } + + // Return result. + return new ImmutablePair<>(vocabularyCandidates, DereferenceResultStatus.SUCCESS); + } + private TransformedEntity transformEntity(Vocabulary vocabulary, final String originalEntity, final String resourceId) { try { @@ -297,7 +314,7 @@ private TransformedEntity getFromCache(String resourceId) { private void saveToCache(String resourceId, TransformedEntity transformedEntity) { final ProcessedEntity entityToCache = new ProcessedEntity(); entityToCache.setResourceId(resourceId); - entityToCache.setXml(transformedEntity.getTransformedEntity()); + entityToCache.setXml(transformedEntity.getEntity()); entityToCache.setVocabularyId(Optional.ofNullable(transformedEntity.getVocabulary()) .map(Vocabulary::getId).map(ObjectId::toString).orElse(null)); entityToCache.setResultStatus(transformedEntity.getResultStatus()); diff --git a/metis-dereference/metis-dereference-service/src/main/java/eu/europeana/metis/dereference/service/TransformedEntity.java b/metis-dereference/metis-dereference-service/src/main/java/eu/europeana/metis/dereference/service/TransformedEntity.java index 82a3f25c1..abe3b528e 100644 --- a/metis-dereference/metis-dereference-service/src/main/java/eu/europeana/metis/dereference/service/TransformedEntity.java +++ b/metis-dereference/metis-dereference-service/src/main/java/eu/europeana/metis/dereference/service/TransformedEntity.java @@ -9,13 +9,19 @@ public class TransformedEntity { private final Vocabulary vocabulary; - private final String transformedEntity; + private final String entity; private final DereferenceResultStatus resultStatus; - public TransformedEntity(Vocabulary vocabulary, String transformedEntity, + /** + * Constructor + * @param vocabulary The vocabulary according to which this entity was transformed (can be null). + * @param entity The result entity (can be null). + * @param resultStatus The result status (should not be null). + */ + public TransformedEntity(Vocabulary vocabulary, String entity, DereferenceResultStatus resultStatus) { this.vocabulary = vocabulary; - this.transformedEntity = transformedEntity; + this.entity = entity; this.resultStatus = resultStatus; } @@ -23,8 +29,8 @@ public Vocabulary getVocabulary() { return vocabulary; } - public String getTransformedEntity() { - return transformedEntity; + public String getEntity() { + return entity; } public DereferenceResultStatus getResultStatus() { diff --git a/metis-dereference/metis-dereference-service/src/test/java/eu/europeana/metis/dereference/service/MongoDereferenceServiceTest.java b/metis-dereference/metis-dereference-service/src/test/java/eu/europeana/metis/dereference/service/MongoDereferenceServiceTest.java index 4e8ceea81..2e4562459 100644 --- a/metis-dereference/metis-dereference-service/src/test/java/eu/europeana/metis/dereference/service/MongoDereferenceServiceTest.java +++ b/metis-dereference/metis-dereference-service/src/test/java/eu/europeana/metis/dereference/service/MongoDereferenceServiceTest.java @@ -54,9 +54,9 @@ class MongoDereferenceServiceTest { spy(new MongoDereferenceService(retriever, processedEntityDao, vocabularyDao)); private static final String GEONAMES_URI = "http://sws.geonames.org/"; - private static Vocabulary GEONAMES; + private static Vocabulary geonames; private static final String PLACE_ID = "http://sws.geonames.org/3020251/"; - private static String PLACE_SOURCE_ENTITY; + private static String placeSourceEntity; private static final Map CACHE = new HashMap<>(); @@ -64,16 +64,16 @@ class MongoDereferenceServiceTest { static void prepareData() throws IOException { // Create the vocabulary - GEONAMES = new Vocabulary(); - GEONAMES.setId(new ObjectId(new Date())); - GEONAMES.setUris(Collections.singleton(GEONAMES_URI)); - GEONAMES.setXslt(IOUtils.toString(Objects.requireNonNull(MongoDereferenceServiceTest.class + geonames = new Vocabulary(); + geonames.setId(new ObjectId(new Date())); + geonames.setUris(Collections.singleton(GEONAMES_URI)); + geonames.setXslt(IOUtils.toString(Objects.requireNonNull(MongoDereferenceServiceTest.class .getClassLoader().getResourceAsStream("geonames.xsl")), StandardCharsets.UTF_8)); - GEONAMES.setName("Geonames"); - GEONAMES.setIterations(0); + geonames.setName("Geonames"); + geonames.setIterations(0); // Create the place - PLACE_SOURCE_ENTITY = IOUtils.toString(Objects.requireNonNull(MongoDereferenceServiceTest.class + placeSourceEntity = IOUtils.toString(Objects.requireNonNull(MongoDereferenceServiceTest.class .getClassLoader().getResourceAsStream("place_entity.xsl")), StandardCharsets.UTF_8); } @@ -85,11 +85,11 @@ void resetMocks() throws IOException, URISyntaxException { // Add support for vocabulary in the mocks. final String searchString = new URI(GEONAMES_URI).getHost(); - doReturn(List.of(GEONAMES)).when(vocabularyDao).getByUriSearch(searchString); - doReturn(GEONAMES).when(vocabularyDao).get(GEONAMES.getId().toString()); + doReturn(List.of(geonames)).when(vocabularyDao).getByUriSearch(searchString); + doReturn(geonames).when(vocabularyDao).get(geonames.getId().toString()); // Add support for the place in the mocks. - doReturn(PLACE_SOURCE_ENTITY).when(retriever).retrieve(eq(PLACE_ID), anyString()); + doReturn(placeSourceEntity).when(retriever).retrieve(eq(PLACE_ID), anyString()); // Clear cache and build the cache functionality CACHE.clear();