Skip to content

Commit

Permalink
#259 service to convert an institution to a collection
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Dec 15, 2020
1 parent 511f045 commit 4290cb2
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public void beforeEach(ExtensionContext extensionContext) throws Exception {
connection.prepareStatement("DELETE FROM institution_collection_person").execute();
connection.prepareStatement("DELETE FROM institution_identifier").execute();
connection.prepareStatement("DELETE FROM institution_tag").execute();
connection.prepareStatement("DELETE FROM institution_occurrence_mapping").executeUpdate();
connection.prepareStatement("DELETE FROM collection_occurrence_mapping").executeUpdate();
connection.prepareStatement("DELETE FROM collection_person").execute();
connection.prepareStatement("DELETE FROM collection").execute();
connection.prepareStatement("DELETE FROM institution").execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public abstract class BaseMergeServiceIT<
& Taggable & Commentable>
extends BaseItTest {

private final MergeService mergeService;
private final CrudService<T> crudService;
private final IdentifierService identifierService;
private final ContactService contactService;
private final MachineTagService machineTagService;
private final OccurrenceMappingService occurrenceMappingService;
private final PersonService personService;
protected final MergeService mergeService;
protected final CrudService<T> crudService;
protected final IdentifierService identifierService;
protected final ContactService contactService;
protected final MachineTagService machineTagService;
protected final OccurrenceMappingService occurrenceMappingService;
protected final PersonService personService;

@Autowired private DatasetService datasetService;
@Autowired private NodeService nodeService;
Expand Down Expand Up @@ -198,7 +198,7 @@ public void preconditionsTest() {
IllegalArgumentException.class, () -> mergeService.merge(e1.getKey(), e2.getKey(), null));
}

private Dataset createDataset() {
protected Dataset createDataset() {
Node node = new Node();
node.setTitle("node");
node.setType(NodeType.COUNTRY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,28 @@

import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.OccurrenceMapping;
import org.gbif.api.model.collections.Person;
import org.gbif.api.model.registry.Dataset;
import org.gbif.api.model.registry.Identifier;
import org.gbif.api.model.registry.MachineTag;
import org.gbif.api.service.collections.CollectionService;
import org.gbif.api.service.collections.InstitutionService;
import org.gbif.api.service.collections.PersonService;
import org.gbif.api.vocabulary.IdentifierType;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.service.collections.merge.InstitutionMergeService;
import org.gbif.ws.client.filter.SimplePrincipalProvider;

import java.util.UUID;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/** Tests the {@link InstitutionMergeService}. */
public class InstitutionMergeServiceIT extends BaseMergeServiceIT<Institution> {
Expand Down Expand Up @@ -92,6 +102,109 @@ public void mergeWithCollectionsAndPrimaryInstitutionInContactsTest() {
assertEquals(replacement.getKey(), c1Updated.getInstitutionKey());
}

@Test
public void convertToCollectionAndCreateNewInstitutionTest() {
Institution toConvert = new Institution();
toConvert.setCode("tco");
toConvert.setName("to convert");
toConvert.setDescription("desc");
institutionService.create(toConvert);

// identifiers
identifierService.addIdentifier(
toConvert.getKey(), new Identifier(IdentifierType.LSID, "test"));

// contacts
Person p1 = new Person();
p1.setFirstName("p1");
personService.create(p1);
contactService.addContact(toConvert.getKey(), p1.getKey());

// machine tags
machineTagService.addMachineTag(toConvert.getKey(), new MachineTag("test", "test", "test"));

// occurrence mappings
Dataset dataset = createDataset();
OccurrenceMapping om1 = new OccurrenceMapping();
om1.setDatasetKey(dataset.getKey());
occurrenceMappingService.addOccurrenceMapping(toConvert.getKey(), om1);

final String newInstitutionName = "new institution";
UUID newCollectionKey =
institutionMergeService.convertToCollection(
toConvert.getKey(), null, newInstitutionName, "user");

Institution converted = institutionService.get(toConvert.getKey());
assertNotNull(converted.getDeleted());
assertEquals(newCollectionKey, converted.getConvertedToCollection());

Collection newCollection = collectionService.get(newCollectionKey);
assertNotNull(newCollection.getInstitutionKey());
assertEquals(toConvert.getDescription(), newCollection.getDescription());
assertNotEquals(toConvert.getKey(), newCollection.getInstitutionKey());

Institution newInstitution = institutionService.get(newCollection.getInstitutionKey());
assertEquals(newCollection.getCode(), newInstitution.getCode());
assertEquals(newInstitutionName, newInstitution.getName());

assertEquals(1, newCollection.getIdentifiers().size());
assertEquals(1, newCollection.getMachineTags().size());
assertEquals(1, newCollection.getOccurrenceMappings().size());
assertEquals(1, newCollection.getContacts().size());
}

@Test
public void convertToCollectionWithExistingInstitutionTest() {
Institution toConvert = new Institution();
toConvert.setCode("tco");
toConvert.setName("to convert");
toConvert.setDescription("desc");
institutionService.create(toConvert);

Institution another = new Institution();
another.setCode("a");
another.setName("another");
institutionService.create(another);

UUID newCollectionKey =
institutionMergeService.convertToCollection(
toConvert.getKey(), another.getKey(), null, "user");

Institution converted = institutionService.get(toConvert.getKey());
assertNotNull(converted.getDeleted());
assertEquals(newCollectionKey, converted.getConvertedToCollection());

Collection newCollection = collectionService.get(newCollectionKey);
assertEquals(another.getKey(), newCollection.getInstitutionKey());
}

@Test
public void convertToCollectionWithCollectionsTest() {
Institution toConvert = new Institution();
toConvert.setCode("tco");
toConvert.setName("to convert");
toConvert.setDescription("desc");
institutionService.create(toConvert);

Collection coll = new Collection();
coll.setCode("c");
coll.setName("coll");
coll.setInstitutionKey(toConvert.getKey());
collectionService.create(coll);

assertThrows(
IllegalArgumentException.class,
() ->
institutionMergeService.convertToCollection(toConvert.getKey(), null, "test", "user"));
}

@Test
public void convertToCollectionMissingArgsTest() {
assertThrows(
IllegalArgumentException.class,
() -> institutionMergeService.convertToCollection(UUID.randomUUID(), null, null, "user"));
}

@Override
Institution createEntityToReplace() {
Institution toReplace = new Institution();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ List<Institution> list(
* @return the keys of the institutions
*/
List<UUID> findByIdentifier(@Nullable @Param("identifier") String identifier);

void convertToCollection(
@Param("institutionKey") UUID institutionKey, @Param("collectionKey") UUID collectionKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<changeSet id="86" author="mlopez">
<sql splitStatements="false" stripComments="false">
<![CDATA[
ALTER TABLE institution ADD COLUMN replaced_by uuid;
ALTER TABLE collection ADD COLUMN replaced_by uuid;
ALTER TABLE institution ADD COLUMN replaced_by uuid REFERENCES institution(key) ON DELETE CASCADE;
ALTER TABLE institution ADD COLUMN converted_to_collection uuid REFERENCES collection(key) ON DELETE CASCADE;
ALTER TABLE collection ADD COLUMN replaced_by uuid REFERENCES collection(key) ON DELETE CASCADE;
ALTER TABLE institution_identifier ADD UNIQUE (identifier_key);
ALTER TABLE institution_occurrence_mapping ADD UNIQUE (occurrence_mapping_key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
i.institutional_governance, i.discipline, i.latitude, i.longitude, i.mailing_address_key, i.address_key,
i.additional_names, i.founding_date, i.geographic_description, i.taxonomic_description, i.number_specimens,
i.index_herbariorum_record, i.logo_url, i.cites_permit_number, i.created_by, i.modified_by,
i.created, i.modified, i.deleted, i.alternative_codes, i.replaced_by
i.created, i.modified, i.deleted, i.alternative_codes, i.replaced_by, i.converted_to_collection
</sql>

<sql id="INSTITUTION_PARAMS_CREATE">
Expand Down Expand Up @@ -571,5 +571,12 @@
WHERE institution_key = #{sourceEntityKey,jdbcType=OTHER}
AND occurrence_mapping_key = #{occurrenceMappingKey,jdbcType=INTEGER}
</update>

<update id="convertToCollection">
UPDATE institution
SET deleted = now(),
converted_to_collection = #{collectionKey,jdbcType=OTHER}
WHERE key = #{institutionKey,jdbcType=OTHER} AND deleted IS NULL
</update>
<!-- END MERGE -->
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public abstract class BaseMergeService<
& Taggable & Commentable>
implements MergeService {

private final BaseMapper<T> baseMapper;
private final MergeableMapper mergeableMapper;
private final ContactableMapper contactableMapper;
private final IdentifierMapper identifierMapper;
private final OccurrenceMappeableMapper occurrenceMappeableMapper;
protected final BaseMapper<T> baseMapper;
protected final MergeableMapper mergeableMapper;
protected final ContactableMapper contactableMapper;
protected final IdentifierMapper identifierMapper;
protected final OccurrenceMappeableMapper occurrenceMappeableMapper;
protected final PersonMapper personMapper;

protected BaseMergeService(
Expand Down Expand Up @@ -99,11 +99,14 @@ public void merge(UUID entityToReplaceKey, UUID replacementKey, String user) {
"Cannot do the replacement because both entities have an IH IRN identifier");
}

checkExtraPreconditions(entityToReplace, replacement);
checkMergeExtraPreconditions(entityToReplace, replacement);

// delete and set the replacement
mergeableMapper.replace(entityToReplaceKey, replacementKey);

// merge entity fields
baseMapper.update(mergeEntityFields(entityToReplace, replacement));

// copy the identifiers
entityToReplace
.getIdentifiers()
Expand All @@ -128,9 +131,6 @@ public void merge(UUID entityToReplaceKey, UUID replacementKey, String user) {
identifierMapper.createIdentifier(keyIdentifier);
baseMapper.addIdentifier(replacementKey, keyIdentifier.getKey());

// merge entity fields
baseMapper.update(mergeEntityFields(entityToReplace, replacement));

// update occurrence mappings
List<OccurrenceMapping> occMappings =
occurrenceMappeableMapper.listOccurrenceMappings(entityToReplaceKey);
Expand Down Expand Up @@ -201,7 +201,7 @@ protected <R> List<R> mergeLists(List<R> l1, List<R> l2) {
return new ArrayList<>(uniqueValues);
}

abstract void checkExtraPreconditions(T entityToReplace, T replacement);
abstract void checkMergeExtraPreconditions(T entityToReplace, T replacement);

abstract T mergeEntityFields(T entityToReplace, T replacement);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected CollectionMergeService(
}

@Override
void checkExtraPreconditions(Collection entityToReplace, Collection replacement) {
void checkMergeExtraPreconditions(Collection entityToReplace, Collection replacement) {
if (entityToReplace.getInstitutionKey() != null
&& !entityToReplace.getInstitutionKey().equals(replacement.getInstitutionKey())) {
throw new IllegalArgumentException(
Expand Down
Loading

0 comments on commit 4290cb2

Please sign in to comment.