Skip to content

Commit

Permalink
MET-6053: Some code improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
jochen-vermeulen committed Jul 25, 2024
1 parent c1db018 commit 0f337b3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -127,8 +131,8 @@ private Pair<EnrichmentBase, DereferenceResultStatus> 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);
Expand Down Expand Up @@ -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, DereferenceResultStatus> 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<DereferenceResultStatus> statuses = new HashSet<>();
for (Vocabulary vocabulary : vocabularyCandidates.getVocabularies()) {
final Set<DereferenceResultStatus> statuses = EnumSet.noneOf(DereferenceResultStatus.class);
for (Vocabulary vocabulary : vocabularyCandidates.getLeft().getVocabularies()) {
final TransformedEntity transformedEntity = transformEntity(vocabulary,
originalEntity.getEntity(), resourceId);
if (transformedEntity.getResultStatus() == DereferenceResultStatus.SUCCESS) {
Expand All @@ -220,6 +214,29 @@ private TransformedEntity performDereferenceAlgorithmForSingleResource(String re
return new TransformedEntity(null, null, status);
}

private Pair<VocabularyCandidates, DereferenceResultStatus> 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 {
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,28 @@
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;
}

public Vocabulary getVocabulary() {
return vocabulary;
}

public String getTransformedEntity() {
return transformedEntity;
public String getEntity() {
return entity;
}

public DereferenceResultStatus getResultStatus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,26 @@ 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<String, ProcessedEntity> CACHE = new HashMap<>();

@BeforeAll
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);
}

Expand All @@ -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();
Expand Down

0 comments on commit 0f337b3

Please sign in to comment.