Skip to content

Commit

Permalink
Adding check for existing institutions (gbif/registry#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahakanzn committed Jun 25, 2024
1 parent e87ef79 commit 7ea2531
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/gbif/collections/sync/SyncResult.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gbif.collections.sync;

import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.CollectionEntity;
import org.gbif.api.model.collections.Contact;
import org.gbif.api.model.collections.Institution;

Expand All @@ -10,6 +11,7 @@
import lombok.Builder;
import lombok.Data;
import lombok.Singular;
import org.gbif.api.model.collections.suggestions.ChangeSuggestion;

/**
* Holds the result of a sync.
Expand Down Expand Up @@ -72,6 +74,7 @@ public static class NoEntityMatch {
private Institution newInstitution;
private Collection newCollection;
private ContactMatch contactMatch;
private int newChangeSuggestion;
}

@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.gbif.api.model.collections.Contact;
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.MasterSourceMetadata;
import org.gbif.api.model.collections.suggestions.ChangeSuggestion;
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.api.model.registry.Identifier;
import org.gbif.api.model.registry.MachineTag;
Expand Down Expand Up @@ -121,6 +122,26 @@ public List<Institution> getIhInstitutions() {
return result;
}

public List<Institution> getInstitutionsByName(String name) {
List<Institution> result = new ArrayList<>();

boolean endRecords = false;
int offset = 0;
while (!endRecords) {
PagingResponse<Institution> response =
syncCall(api.listInstitutionsByName(name, 1000, offset));
endRecords = response.isEndOfRecords();
offset += response.getLimit();
result.addAll(response.getResults());
}

return result;
}

public int createChangeSuggestion(ChangeSuggestion changeSuggestion) {
return syncCall(api.createChangeSuggestion(changeSuggestion));
}

public Institution getInstitution(UUID key) {
return syncCall(api.getInstitution(key));
}
Expand Down Expand Up @@ -259,6 +280,12 @@ Call<PagingResponse<Institution>> listInstitutions(
@Query("limit") int limit,
@Query("offset") int offset);

@GET("institution")
Call<PagingResponse<Institution>> listInstitutionsByName(
@Query("name") String name,
@Query("limit") int limit,
@Query("offset") int offset);

@GET("institution/{key}")
Call<Institution> getInstitution(@Path("key") UUID key);

Expand Down Expand Up @@ -357,6 +384,9 @@ Call<Void> addMasterSourceMetadataToInstitution(
@POST("collection/{collectionKey}/masterSourceMetadata")
Call<Void> addMasterSourceMetadataToCollection(
@Path("collectionKey") UUID collectionKey, @Body MasterSourceMetadata masterSourceMetadata);

@POST("changeSuggestion")
Call<Integer> createChangeSuggestion(@Body ChangeSuggestion createSuggestion);
}

/** Adapter necessary for retrofit due to versioning. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.gbif.collections.sync.clients.proxy;

import java.util.List;
import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.Contact;
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.suggestions.ChangeSuggestion;
import org.gbif.collections.sync.clients.http.GrSciCollHttpClient;
import org.gbif.collections.sync.common.handler.CollectionHandler;
import org.gbif.collections.sync.common.handler.InstitutionHandler;
Expand Down Expand Up @@ -57,6 +59,16 @@ public void removeContactFromInstitution(UUID entityKey, int contactKey) {
institutionHandler.removeContactFromEntityCall(entityKey, contactKey);
}

@Override
public List<Institution> findInstitutionByName(String institutionName) {
return institutionHandler.listInstitutionsByName(institutionName);
}

@Override
public int createChangeSuggestion(ChangeSuggestion createSuggestion) {
return institutionHandler.createChangeSuggestion(createSuggestion);
}

public Integer addContactToCollection(UUID entityKey, Contact contact) {
return collectionHandler.addContactToEntityCall(entityKey, contact);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.gbif.collections.sync.clients.proxy;

import java.util.List;
import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.suggestions.ChangeSuggestion;

public interface GrSciCollProxyClient {

Expand All @@ -12,4 +14,8 @@ public interface GrSciCollProxyClient {
Collection createCollection(Collection newCollection);

boolean updateCollection(Collection oldCollection, Collection newCollection);

List<Institution> findInstitutionByName(String institutionName);

int createChangeSuggestion(ChangeSuggestion<Institution> createSuggestion);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.Contact;
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.suggestions.InstitutionChangeSuggestion;
import org.gbif.collections.sync.SyncResult.*;
import org.gbif.collections.sync.clients.proxy.GrSciCollProxyClient;
import org.gbif.collections.sync.common.converter.EntityConverter;
Expand Down Expand Up @@ -128,23 +129,32 @@ public InstitutionAndCollectionMatch handleInstAndCollMatch(MatchResult<S, R> ma
public NoEntityMatch handleNoMatch(MatchResult<S, R> matchResult) {
// create institution
Institution newInstitution = entityConverter.convertToInstitution(matchResult.getSource());

Institution createdInstitution = proxyClient.createInstitution(newInstitution);

// create collection
Collection createdCollection = createCollection(matchResult.getSource(), createdInstitution);

// same staff for both entities
ContactMatch contactMatchInstitution =
staffResultHandler.handleStaff(matchResult, createdInstitution);
ContactMatch contactMatchCollection =
staffResultHandler.handleStaff(matchResult, createdCollection);

return NoEntityMatch.builder()
.newCollection(createdCollection)
.newInstitution(createdInstitution)
.contactMatch(mergeContactMatches(contactMatchInstitution, contactMatchCollection))
.build();
if (proxyClient.findInstitutionByName(newInstitution.getName()).isEmpty()) {
Institution createdInstitution = proxyClient.createInstitution(newInstitution);
// create collection
Collection createdCollection = createCollection(matchResult.getSource(), createdInstitution);

// same staff for both entities
ContactMatch contactMatchInstitution =
staffResultHandler.handleStaff(matchResult, createdInstitution);
ContactMatch contactMatchCollection =
staffResultHandler.handleStaff(matchResult, createdCollection);

return NoEntityMatch.builder()
.newCollection(createdCollection)
.newInstitution(createdInstitution)
.contactMatch(mergeContactMatches(contactMatchInstitution, contactMatchCollection))
.build();
}
else {
// create change suggestion
InstitutionChangeSuggestion institutionChangeSuggestion = new InstitutionChangeSuggestion();
institutionChangeSuggestion.setSuggestedEntity(newInstitution);
int suggestionCreated = proxyClient.createChangeSuggestion(institutionChangeSuggestion);
return NoEntityMatch.builder()
.newChangeSuggestion(suggestionCreated)
.build();
}
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.gbif.collections.sync.common.handler;

import java.util.List;
import org.gbif.api.model.collections.Contact;
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.MasterSourceMetadata;
import org.gbif.api.model.collections.suggestions.ChangeSuggestion;
import org.gbif.api.model.registry.Identifier;
import org.gbif.api.model.registry.MachineTag;
import org.gbif.collections.sync.clients.http.GrSciCollHttpClient;
Expand Down Expand Up @@ -75,4 +77,12 @@ public void removeContactFromEntityCall(UUID entityKey, int contactKey) {
() -> grSciCollHttpClient.removeContactFromInstitution(entityKey, contactKey),
exceptionHandler(contactKey, "Failed to remove contact from institution " + entityKey));
}

public List<Institution> listInstitutionsByName(String name) {
return grSciCollHttpClient.getInstitutionsByName(name);
}

public int createChangeSuggestion(ChangeSuggestion changeSuggestion) {
return grSciCollHttpClient.createChangeSuggestion(changeSuggestion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public SyncResult sync() {
return;
}

IHMatchResult match = matcher.match(ihInstitution);
handleResult(match, resultBuilder);
IHMatchResult match = matcher.match(ihInstitution);handleResult(match, resultBuilder);
});

SyncResult result = resultBuilder.build();
Expand Down
20 changes: 19 additions & 1 deletion src/test/java/org/gbif/collections/sync/ih/BaseIHTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.gbif.api.vocabulary.Country;
import org.gbif.api.vocabulary.IdentifierType;
import org.gbif.api.vocabulary.collections.IdType;
import org.gbif.api.vocabulary.collections.InstitutionType;
import org.gbif.api.vocabulary.collections.MasterSourceType;
import org.gbif.api.vocabulary.collections.Source;
import org.gbif.collections.sync.SyncResult;
Expand Down Expand Up @@ -382,6 +381,25 @@ protected Map<String, Integer> convertCollectionSummary(IHInstitution.Collection
return map;
}

protected TestEntity<Institution,IHInstitution> createExistingInstitutionWithSameName() {
Institution i = new Institution();
i.setKey(UUID.randomUUID());
i.setCode("bar");
i.setName("bar");
i.setNumberSpecimens(1000);
i.setMasterSource(MasterSourceType.IH);
i.setMasterSourceMetadata(new MasterSourceMetadata(Source.IH_IRN, IRN_TEST));
i.getIdentifiers().add(new Identifier(IdentifierType.IH_IRN, IRN_TEST));
i.getIdentifiers().add(new Identifier(IdentifierType.CITES, CITES_TEST));

IHInstitution ih = new IHInstitution();
ih.setCode("bar");
ih.setOrganization("bar");
ih.setSpecimenTotal(1000);

return TestEntity.<Institution, IHInstitution>builder().entity(i).ih(ih).build();
}

private static IHConfig createConfig() {
IHConfig ihConfig = new IHConfig();
ihConfig.setSyncConfig(createTestSyncConfig());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public void noMatchTest() {
assertEmptyContactMatch(noEntityMatch.getContactMatch());
}

@Test
public void noMatchButNameExistsTest() {

}
@Test
public void institutionAndCollectionMatchTest() {
TestEntity<Collection, IHInstitution> collectionToUpdate = createCollectionToUpdate();
Expand Down

0 comments on commit 7ea2531

Please sign in to comment.