Skip to content

Commit

Permalink
#534 added parentCode to occurrence mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Nov 16, 2023
1 parent c83e29e commit d3424b3
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<spring-cloud-sleuth.version>2.2.8.RELEASE</spring-cloud-sleuth.version>

<!-- GBIF -->
<gbif-api.version>1.12.9</gbif-api.version>
<gbif-api.version>1.12.10</gbif-api.version>
<gbif-common.version>0.59</gbif-common.version>
<gbif-common-mybatis.version>1.3</gbif-common-mybatis.version>
<gbif-common-ws.version>1.25</gbif-common-ws.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ public void explicitMappingsTest() {
params.setDatasetKey(d1.getKey());
params.setInstitutionCode(i1.getCode());
params.setCollectionCode(c5.getCode());
params.setDatasetKey(d1.getKey());

// When
LookupResult result = lookupService.lookup(params);
Expand Down Expand Up @@ -536,7 +535,6 @@ public void ambiguousExplicitMappingsTest() {
params.setInstitutionId(occMappingI22.getIdentifier());
params.setCollectionCode(occMappingC2.getCode());
params.setCollectionId(occMappingC22.getIdentifier());
params.setDatasetKey(d1.getKey());

// When
LookupResult result = lookupService.lookup(params);
Expand All @@ -553,6 +551,39 @@ public void ambiguousExplicitMappingsTest() {
assertEquals(Match.Status.AMBIGUOUS_EXPLICIT_MAPPINGS, collectionMatch.getStatus());
}

@Test
public void explicitMappingsWithParentCodeTest() {
// State
Dataset d1 = createDataset();

OccurrenceMapping occMappingC1 = new OccurrenceMapping();
occMappingC1.setDatasetKey(d1.getKey());
occMappingC1.setParentCode(i1.getCode());
collectionService.addOccurrenceMapping(c1.getKey(), occMappingC1);

LookupParams params = new LookupParams();
params.setDatasetKey(d1.getKey());
params.setInstitutionCode(i1.getCode());
params.setInstitutionId(i1.getKey().toString());

// When
LookupResult result = lookupService.lookup(params);

// Should
assertNotNull(result.getInstitutionMatch());
Match<InstitutionMatched> institutionMatch = result.getInstitutionMatch();
assertEquals(Match.MatchType.EXACT, institutionMatch.getMatchType());
assertEquals(i1.getKey(), institutionMatch.getEntityMatched().getKey());
assertEquals(2, institutionMatch.getReasons().size());
assertEquals(Match.Status.ACCEPTED, institutionMatch.getStatus());

assertNotNull(result.getCollectionMatch());
Match<CollectionMatched> collectionMatch = result.getCollectionMatch();
assertEquals(Match.MatchType.EXPLICIT_MAPPING, collectionMatch.getMatchType());
assertEquals(c1.getKey(), collectionMatch.getEntityMatched().getKey());
assertEquals(Match.Status.ACCEPTED, collectionMatch.getStatus());
}

@Test
public void countryMatchTest() {
// State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public interface LookupMapper<T extends EntityMatchedDto> {

List<T> lookup(
@Nullable @Param("code") String code,
@Nullable @Param("parentCode") String parentCode,
@Nullable @Param("identifier") String identifier,
@Nullable @Param("key") UUID key,
@Nullable @Param("datasetKey") UUID datasetKey);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">

<changeSet id="129" author="mlopez" runInTransaction="false">
<sql splitStatements="false" stripComments="false">
<![CDATA[
ALTER TABLE occurrence_mapping ADD COLUMN parent_code varchar CHECK (assert_min_length(code, 1));
]]>
</sql>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,5 @@
<include file="liquibase/126-wikidata-ncbi-identifier-types.xml" />
<include file="liquibase/127-organization-country-not-null.xml" />
<include file="liquibase/128-suggestions-country.xml" />
<include file="liquibase/129-parent-code-occurrence-mapping.xml" />
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@
INNER JOIN occurrence_mapping om ON om.key = com.occurrence_mapping_key
WHERE c.deleted is null AND om.dataset_key = #{datasetKey,jdbcType=OTHER}
<if test="code != null"> AND (om.code IS NULL OR om.code = #{code,jdbcType=VARCHAR})</if>
<if test="parentCode != null"> AND (om.parent_code IS NULL OR om.parent_code = #{parentCode,jdbcType=VARCHAR})</if>
<if test="identifier != null"> AND (om.identifier IS NULL OR om.identifier = #{identifier,jdbcType=VARCHAR})</if>
</if>
</trim>) AS matches
Expand Down
1 change: 1 addition & 0 deletions registry-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ when there are more than 1 possible combination within a dataset. This allows us
- All the occurrences from the dataset X have to be mapped to the institution I
- All the occurrences from the dataset X and code Y have to be mapped to the institution I
- All the occurrences from the dataset X, code Y and identifier Z have to be mapped to the institution I
- For collections only, all the occurrences from the dataset X and parentCode Y(this is the institution code) have to be mapped to the collection C

There can be as many combinations as needed.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,18 @@ private static String cleanString(String value) {
return !Strings.isNullOrEmpty(value) ? value.trim() : null;
}

protected List<T> getDbMatches(String codeParam, String identifierParam, UUID datasetKey) {
protected List<T> getDbMatches(String codeParam, String parentCodeParam, String identifierParam, UUID datasetKey) {
String code = cleanString(codeParam);
String parentCode = cleanString(parentCodeParam);
String identifier = cleanString(identifierParam);

if (code == null && identifier == null) {
if (code == null && identifier == null && datasetKey == null) {
return Collections.emptyList();
}

UUID key = parseUUID(identifier);

return getLookupMapper().lookup(code, identifier, key, datasetKey);
return getLookupMapper().lookup(code, parentCode, identifier, key, datasetKey);
}

protected Match<R> createMatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,13 @@
import org.gbif.registry.persistence.mapper.collections.LookupMapper;
import org.gbif.registry.persistence.mapper.collections.dto.CollectionMatchedDto;
import org.gbif.registry.service.collections.lookup.Matches;

import java.net.URI;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.util.*;

import static org.gbif.api.model.collections.lookup.Match.Reason.INST_COLL_MISMATCH;

@Component
Expand All @@ -53,7 +47,11 @@ public Matches<CollectionMatched> matchCollections(
Matches<CollectionMatched> matches = new Matches<>();

List<CollectionMatchedDto> dbMatches =
getDbMatches(params.getCollectionCode(), params.getCollectionId(), params.getDatasetKey());
getDbMatches(
params.getCollectionCode(),
params.getInstitutionCode(),
params.getCollectionId(),
params.getDatasetKey());

// the queries may return duplicates because a collection can match with several fields
Map<UUID, CollectionMatchedDto> dtosMap = new HashMap<>();
Expand Down Expand Up @@ -110,10 +108,19 @@ protected Match<CollectionMatched> createCollectionMatch(
if (institutionMatched.getEntityMatched().getKey().equals(dto.getInstitutionKey())
&& institutionMatched.getMatchType() == Match.MatchType.EXACT
|| institutionMatched.getMatchType() == Match.MatchType.EXPLICIT_MAPPING) {
Match<CollectionMatched> match = Match.exact(toEntityMatched(dto), getMatchReasons(dto));
match.addReason(Match.Reason.BELONGS_TO_INSTITUTION_MATCHED);
exactMatches.add(match);
return match;

if (dto.isExplicitMapping()) {
Match<CollectionMatched> match =
Match.explicitMapping(toEntityMatched(dto), getMatchReasons(dto));
match.addReason(Match.Reason.BELONGS_TO_INSTITUTION_MATCHED);
explicitMatches.add(match);
return match;
} else {
Match<CollectionMatched> match = Match.exact(toEntityMatched(dto), getMatchReasons(dto));
match.addReason(Match.Reason.BELONGS_TO_INSTITUTION_MATCHED);
exactMatches.add(match);
return match;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Matches<InstitutionMatched> matchInstitutions(LookupParams params) {

List<InstitutionMatchedDto> dbMatches =
getDbMatches(
params.getInstitutionCode(), params.getInstitutionId(), params.getDatasetKey());
params.getInstitutionCode(), null, params.getInstitutionId(), params.getDatasetKey());

// the queries may return duplicates because we retrieve the list of identifiers in the same
// query. Also, if an institution matches with several fields it will be duplicated
Expand Down

0 comments on commit d3424b3

Please sign in to comment.