Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Nov 21, 2023
2 parents 4ac72a8 + d3424b3 commit 1caa78b
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 42 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.8</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 @@ -528,7 +528,7 @@ public void listChangeSuggestionTest() {
UUID entityKey = UUID.randomUUID();
Pageable page = new PagingRequest();

when(getMockChangeSuggestionService().list(status, type, proposerEmail, entityKey, page))
when(getMockChangeSuggestionService().list(status, type, proposerEmail, entityKey, null, page))
.thenReturn(
new PagingResponse<>(
new PagingRequest(), 1L, Collections.singletonList(changeSuggestion)));
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 @@ -433,6 +433,11 @@ public void discardSuggestionTest() {
public void listTest() {
// State
T entity = createEntity();

Address address = new Address();
address.setCountry(Country.DENMARK);
entity.setAddress(address);

R suggestion = createEmptyChangeSuggestion();
suggestion.setSuggestedEntity(entity);
suggestion.setType(Type.CREATE);
Expand All @@ -442,8 +447,17 @@ public void listTest() {
int suggKey1 = changeSuggestionService.createChangeSuggestion(suggestion);

T entity2 = createEntity();
Address address2 = new Address();
address2.setCountry(Country.DENMARK);
entity2.setAddress(address2);

UUID entity2Key = collectionEntityService.create(entity2);
R suggestion2 = createEmptyChangeSuggestion();

Address addressSugg = new Address();
addressSugg.setCountry(Country.SPAIN);
entity2.setAddress(addressSugg);

suggestion2.setSuggestedEntity(entity2);
suggestion2.setEntityKey(entity2Key);
suggestion2.setType(Type.UPDATE);
Expand All @@ -454,26 +468,38 @@ public void listTest() {

// When
PagingResponse<R> results =
changeSuggestionService.list(Status.APPLIED, null, null, null, DEFAULT_PAGE);
changeSuggestionService.list(Status.APPLIED, null, null, null, null, DEFAULT_PAGE);
// Then
assertEquals(0, results.getResults().size());
assertEquals(0, results.getCount());

// When
results = changeSuggestionService.list(null, Type.CREATE, null, null, DEFAULT_PAGE);
results = changeSuggestionService.list(null, Type.CREATE, null, null, null, DEFAULT_PAGE);
// Then
assertEquals(1, results.getResults().size());
assertEquals(1, results.getCount());

// When
results = changeSuggestionService.list(null, null, null, entity2Key, DEFAULT_PAGE);
results = changeSuggestionService.list(null, null, null, entity2Key, null, DEFAULT_PAGE);
// Then
assertEquals(1, results.getResults().size());
assertEquals(1, results.getCount());

// When
results = changeSuggestionService.list(null, null, null, null, Country.DENMARK, DEFAULT_PAGE);
// Then
assertEquals(2, results.getResults().size());
assertEquals(2, results.getCount());

// When
results = changeSuggestionService.list(null, null, null, null, Country.SPAIN, DEFAULT_PAGE);
// Then
assertEquals(0, results.getResults().size());
assertEquals(0, results.getCount());

// When - user with no rights can't see the proposer email
resetSecurityContext("user", UserRole.USER);
results = changeSuggestionService.list(null, null, null, entity2Key, DEFAULT_PAGE);
results = changeSuggestionService.list(null, null, null, entity2Key, null, DEFAULT_PAGE);
// Then
assertTrue(results.getResults().stream().allMatch(v -> v.getProposerEmail() == null));
}
Expand Down Expand Up @@ -587,15 +613,14 @@ public void updateContactsTest() {
assertTrue(applied.getContactPersons().stream().anyMatch(c -> c.getFirstName().equals("11")));
assertTrue(applied.getContactPersons().stream().anyMatch(c -> c.getFirstName().equals("22")));


entity = collectionEntityService.get(entityKey);

// suggestion to change contact1
entity.getContactPersons().stream()
.filter(c -> c.getKey().equals(contact1.getKey()))
.findFirst()
.get()
.setModifiedBy("11");
.filter(c -> c.getKey().equals(contact1.getKey()))
.findFirst()
.get()
.setModifiedBy("11");

R suggestion3 = createEmptyChangeSuggestion();
suggestion3.setSuggestedEntity(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.gbif.api.model.collections.suggestions.Status;
import org.gbif.api.model.collections.suggestions.Type;
import org.gbif.api.model.common.paging.Pageable;
import org.gbif.api.vocabulary.Country;
import org.gbif.registry.persistence.mapper.collections.dto.ChangeSuggestionDto;

import java.util.List;
Expand All @@ -43,14 +44,16 @@ List<ChangeSuggestionDto> list(
@Param("entityType") CollectionEntityType entityType,
@Param("proposerEmail") String proposerEmail,
@Param("entityKey") UUID entityKey,
@Param("country") Country country,
@Nullable @Param("page") Pageable page);

long count(
@Param("status") Status status,
@Param("type") Type type,
@Param("entityType") CollectionEntityType entityType,
@Param("proposerEmail") String proposerEmail,
@Param("entityKey") UUID entityKey);
@Param("entityKey") UUID entityKey,
@Param("country") Country country);

void update(ChangeSuggestionDto suggestion);
}
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
Expand Up @@ -50,8 +50,5 @@ public class ChangeSuggestionDto {
private String nameNewInstitutionConvertedCollection;
private Date modified;
private String modifiedBy;

// this field is not persisted in the DB. It stores the country that has to be taken into account
// to check the user permissions
private Country countryScope;
}
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="128" author="mlopez" runInTransaction="false">
<sql splitStatements="false" stripComments="false">
<![CDATA[
ALTER TABLE change_suggestion ADD COLUMN entity_country CHAR(2) CHECK (assert_min_length(entity_country, 2));
]]>
</sql>
</changeSet>
</databaseChangeLog>
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>
2 changes: 2 additions & 0 deletions registry-persistence/src/main/resources/liquibase/master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,6 @@
<include file="liquibase/125-occurrence-counts-grscicoll.xml" />
<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 @@ -7,19 +7,20 @@
<id property="key" column="key"/>
<result property="comments" column="comments" typeHandler="StringArrayTypeHandler"/>
<result property="changes" column="changes" typeHandler="SuggestedChangesTypeHandler"/>
<result property="countryScope" column="entity_country"/>
</resultMap>

<sql id="SUGGESTION_WRITE_FIELDS">
entity_type, entity_key, type, status, proposed, proposed_by, proposer_email, changes, comments,
suggested_entity, merge_target_key, institution_converted_collection, name_new_institution_converted_collection,
modified, modified_by
modified, modified_by, entity_country
</sql>

<sql id="SUGGESTION_READ_FIELDS">
cs.key, cs.entity_type, cs.entity_key, cs.type, cs.status, cs.proposed, cs.proposed_by, cs.proposer_email, cs. applied,
cs.applied_by, cs.discarded_by, cs.discarded, cs.suggested_entity, cs.comments,
cs.merge_target_key, cs.changes, cs.institution_converted_collection, cs.name_new_institution_converted_collection,
cs.modified, cs.modified_by
cs.modified, cs.modified_by, cs.entity_country
</sql>

<sql id="SUGGESTION_PARAMS_CREATE">
Expand All @@ -37,7 +38,8 @@
#{institutionConvertedCollection,jdbcType=OTHER},
#{nameNewInstitutionConvertedCollection,jdbcType=VARCHAR},
now(), <!-- modified -->
#{modifiedBy,jdbcType=VARCHAR}
#{modifiedBy,jdbcType=VARCHAR},
#{countryScope,jdbcType=VARCHAR}
</sql>

<sql id="SUGGESTION_PARAMS_UPDATE">
Expand All @@ -51,7 +53,8 @@
discarded_by = #{discardedBy,jdbcType=INTEGER},
applied = #{applied,jdbcType=OTHER},
applied_by = #{appliedBy,jdbcType=INTEGER},
entity_key = #{entityKey,jdbcType=INTEGER}
entity_key = #{entityKey,jdbcType=INTEGER},
entity_country = #{countryScope,jdbcType=VARCHAR}
</sql>

<insert id="create" parameterType="ChangeSuggestionDto" useGeneratedKeys="true" keyProperty="key">
Expand Down Expand Up @@ -94,6 +97,9 @@
<if test="entityKey != null">
AND cs.entity_key = #{entityKey,jdbcType=OTHER}
</if>
<if test="country != null">
AND cs.entity_country = #{country,jdbcType=VARCHAR}
</if>
</where>
ORDER BY cs.proposed DESC
<if test="page != null">
Expand All @@ -120,6 +126,9 @@
<if test="entityKey != null">
AND cs.entity_key = #{entityKey,jdbcType=OTHER}
</if>
<if test="country != null">
AND cs.entity_country = #{country,jdbcType=VARCHAR}
</if>
</where>
</select>

Expand Down
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
Loading

0 comments on commit 1caa78b

Please sign in to comment.