Skip to content

Commit

Permalink
#255 keeping identifiers, machine tags and occurrence mappings in the…
Browse files Browse the repository at this point in the history
… replaced entity
  • Loading branch information
marcos-lg committed Dec 10, 2020
1 parent 393ff3a commit db02361
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,19 @@ public void mergeTest() {
T replaced = crudService.get(toReplace.getKey());
T replacementUpdated = crudService.get(replacement.getKey());

assertEquals(0, replaced.getIdentifiers().size());
assertEquals(1, replaced.getIdentifiers().size());
assertNotEquals(toReplace.getModified(), replaced.getModified());
assertEquals(user, replaced.getModifiedBy());
assertEquals(user, replacementUpdated.getModifiedBy());
assertEquals(2, replacementUpdated.getIdentifiers().size());
assertEquals(1, replaced.getMachineTags().size());
assertEquals(2, replaced.getMachineTags().size());
assertEquals(1, replacementUpdated.getMachineTags().size());
assertEquals(2, replacementUpdated.getContacts().size());
assertEquals(a2, replacementUpdated.getAddress());
assertEquals(ma1, replacementUpdated.getMailingAddress());
assertEquals(replacement.getCreatedBy(), replacementUpdated.getCreatedBy());
assertNull(replacementUpdated.getDeleted());
assertEquals(1, replaced.getOccurrenceMappings().size());
assertEquals(1, replacementUpdated.getOccurrenceMappings().size());

extraAsserts(replaced, replacement, replacementUpdated);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,4 @@ public interface MergeableMapper {

void replace(
@Param("targetEntityKey") UUID targetEntityKey, @Param("replacementKey") UUID replacementKey);

void moveIdentifier(
@Param("sourceEntityKey") UUID sourceEntityKey,
@Param("targetEntityKey") UUID targetEntityKey,
@Param("identifierKey") int identifierKey);

void moveMachineTag(
@Param("sourceEntityKey") UUID sourceEntityKey,
@Param("targetEntityKey") UUID targetEntityKey,
@Param("machineTagKey") int machineTagKey);

void moveOccurrenceMapping(
@Param("sourceEntityKey") UUID sourceEntityKey,
@Param("targetEntityKey") UUID targetEntityKey,
@Param("occurrenceMappingKey") int occurrenceMappingKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -572,26 +572,5 @@
replaced_by = #{replacementKey,jdbcType=OTHER}
WHERE key = #{targetEntityKey,jdbcType=OTHER} AND deleted IS NULL
</update>

<update id="moveIdentifier">
UPDATE collection_identifier
SET collection_key = #{targetEntityKey,jdbcType=OTHER}
WHERE collection_key = #{sourceEntityKey,jdbcType=OTHER}
AND identifier_key = #{identifierKey,jdbcType=INTEGER}
</update>

<update id="moveMachineTag">
UPDATE collection_machine_tag
SET collection_key = #{targetEntityKey,jdbcType=OTHER}
WHERE collection_key = #{sourceEntityKey,jdbcType=OTHER}
AND machine_tag_key = #{machineTagKey,jdbcType=INTEGER}
</update>

<update id="moveOccurrenceMapping">
UPDATE collection_occurrence_mapping
SET collection_key = #{targetEntityKey,jdbcType=OTHER}
WHERE collection_key = #{sourceEntityKey,jdbcType=OTHER}
AND occurrence_mapping_key = #{occurrenceMappingKey,jdbcType=INTEGER}
</update>
<!-- END MERGE -->
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -551,27 +551,6 @@
WHERE key = #{targetEntityKey,jdbcType=OTHER} AND deleted IS NULL
</update>

<update id="moveIdentifier">
UPDATE institution_identifier
SET institution_key = #{targetEntityKey,jdbcType=OTHER}
WHERE institution_key = #{sourceEntityKey,jdbcType=OTHER}
AND identifier_key = #{identifierKey,jdbcType=INTEGER}
</update>

<update id="moveMachineTag">
UPDATE institution_machine_tag
SET institution_key = #{targetEntityKey,jdbcType=OTHER}
WHERE institution_key = #{sourceEntityKey,jdbcType=OTHER}
AND machine_tag_key = #{machineTagKey,jdbcType=INTEGER}
</update>

<update id="moveOccurrenceMapping">
UPDATE institution_occurrence_mapping
SET institution_key = #{targetEntityKey,jdbcType=OTHER}
WHERE institution_key = #{sourceEntityKey,jdbcType=OTHER}
AND occurrence_mapping_key = #{occurrenceMappingKey,jdbcType=INTEGER}
</update>

<update id="convertToCollection">
UPDATE institution
SET deleted = now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import org.gbif.api.vocabulary.IdentifierType;
import org.gbif.registry.persistence.ContactableMapper;
import org.gbif.registry.persistence.mapper.IdentifierMapper;
import org.gbif.registry.persistence.mapper.MachineTagMapper;
import org.gbif.registry.persistence.mapper.collections.BaseMapper;
import org.gbif.registry.persistence.mapper.collections.MergeableMapper;
import org.gbif.registry.persistence.mapper.collections.OccurrenceMappeableMapper;
import org.gbif.registry.persistence.mapper.collections.OccurrenceMappingMapper;
import org.gbif.registry.persistence.mapper.collections.PersonMapper;

import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -58,20 +60,26 @@ public abstract class BaseMergeService<
protected final IdentifierMapper identifierMapper;
protected final OccurrenceMappeableMapper occurrenceMappeableMapper;
protected final PersonMapper personMapper;
private final MachineTagMapper machineTagMapper;
private final OccurrenceMappingMapper occurrenceMappingMapper;

protected BaseMergeService(
BaseMapper<T> baseMapper,
MergeableMapper mergeableMapper,
ContactableMapper contactableMapper,
IdentifierMapper identifierMapper,
OccurrenceMappeableMapper occurrenceMappeableMapper,
PersonMapper personMapper) {
PersonMapper personMapper,
MachineTagMapper machineTagMapper,
OccurrenceMappingMapper occurrenceMappingMapper) {
this.baseMapper = baseMapper;
this.mergeableMapper = mergeableMapper;
this.contactableMapper = contactableMapper;
this.identifierMapper = identifierMapper;
this.occurrenceMappeableMapper = occurrenceMappeableMapper;
this.personMapper = personMapper;
this.machineTagMapper = machineTagMapper;
this.occurrenceMappingMapper = occurrenceMappingMapper;
}

@Override
Expand Down Expand Up @@ -118,13 +126,20 @@ public void merge(UUID entityToReplaceKey, UUID replacementKey, String user) {
entityToReplace
.getIdentifiers()
.forEach(
i -> mergeableMapper.moveIdentifier(entityToReplaceKey, replacementKey, i.getKey()));
i -> {
identifierMapper.createIdentifier(i);
baseMapper.addIdentifier(replacementKey, i.getKey());
});

// copy iDigBio machine tags
// TODO: what if the replacement has idigbio machine tags already?
entityToReplace.getMachineTags().stream()
.filter(mt -> mt.getNamespace().equals("iDigBio.org"))
.forEach(
mt -> mergeableMapper.moveMachineTag(entityToReplaceKey, replacementKey, mt.getKey()));
mt -> {
machineTagMapper.createMachineTag(mt);
baseMapper.addMachineTag(replacementKey, mt.getKey());
});

// merge contacts
Objects.requireNonNull(entityToReplace.getContacts()).stream()
Expand All @@ -141,9 +156,10 @@ public void merge(UUID entityToReplaceKey, UUID replacementKey, String user) {
// update occurrence mappings
List<OccurrenceMapping> occMappings =
occurrenceMappeableMapper.listOccurrenceMappings(entityToReplaceKey);
occMappings.forEach(
om ->
mergeableMapper.moveOccurrenceMapping(entityToReplaceKey, replacementKey, om.getKey()));
occMappings.forEach(om -> {
occurrenceMappingMapper.createOccurrenceMapping(om);
occurrenceMappeableMapper.addOccurrenceMapping(replacementKey, om.getKey());
});

additionalOperations(entityToReplace, replacement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.Person;
import org.gbif.registry.persistence.mapper.IdentifierMapper;
import org.gbif.registry.persistence.mapper.MachineTagMapper;
import org.gbif.registry.persistence.mapper.collections.CollectionMapper;
import org.gbif.registry.persistence.mapper.collections.OccurrenceMappingMapper;
import org.gbif.registry.persistence.mapper.collections.PersonMapper;

import java.util.List;
Expand All @@ -35,14 +37,18 @@ public class CollectionMergeService extends BaseMergeService<Collection> {
protected CollectionMergeService(
CollectionMapper collectionMapper,
IdentifierMapper identifierMapper,
PersonMapper personMapper) {
PersonMapper personMapper,
MachineTagMapper machineTagMapper,
OccurrenceMappingMapper occurrenceMappingMapper) {
super(
collectionMapper,
collectionMapper,
collectionMapper,
identifierMapper,
collectionMapper,
personMapper);
personMapper,
machineTagMapper,
occurrenceMappingMapper);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public InstitutionMergeService(
institutionMapper,
identifierMapper,
institutionMapper,
personMapper);
personMapper,
machineTagMapper,
occurrenceMappingMapper);
this.institutionMapper = institutionMapper;
this.collectionMapper = collectionMapper;
this.machineTagMapper = machineTagMapper;
Expand Down Expand Up @@ -136,7 +138,6 @@ public UUID convertToCollection(
.getIdentifiers()
.forEach(
i -> {
institutionMapper.deleteIdentifier(institutionKey, i.getKey());
identifierMapper.createIdentifier(i);
collectionMapper.addIdentifier(newCollection.getKey(), i.getKey());
});
Expand All @@ -146,7 +147,6 @@ public UUID convertToCollection(
.getMachineTags()
.forEach(
mt -> {
institutionMapper.deleteMachineTag(institutionKey, mt.getKey());
machineTagMapper.createMachineTag(mt);
collectionMapper.addMachineTag(newCollection.getKey(), mt.getKey());
});
Expand All @@ -156,7 +156,6 @@ public UUID convertToCollection(
.getOccurrenceMappings()
.forEach(
om -> {
institutionMapper.deleteOccurrenceMapping(institutionKey, om.getKey());
occurrenceMappingMapper.createOccurrenceMapping(om);
collectionMapper.addOccurrenceMapping(newCollection.getKey(), om.getKey());
});
Expand Down

0 comments on commit db02361

Please sign in to comment.