Skip to content

Commit

Permalink
#255 #259 set modified and modifiedBy + moving collections when trans…
Browse files Browse the repository at this point in the history
…forming an institution to a collection
  • Loading branch information
marcos-lg committed Dec 3, 2020
1 parent 067c007 commit 4bc1d84
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
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.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -154,12 +155,16 @@ public void mergeTest() {

contactService.addContact(replacement.getKey(), p1.getKey());

mergeService.merge(toReplace.getKey(), replacement.getKey(), "test");
final String user = "test";
mergeService.merge(toReplace.getKey(), replacement.getKey(), user);

T replaced = crudService.get(toReplace.getKey());
T replacementUpdated = crudService.get(replacement.getKey());

assertEquals(0, 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(1, replacementUpdated.getMachineTags().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ public void convertToCollectionAndCreateNewInstitutionTest() {
om1.setDatasetKey(dataset.getKey());
occurrenceMappingService.addOccurrenceMapping(toConvert.getKey(), om1);

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

final String newInstitutionName = "new institution";
UUID newCollectionKey =
institutionMergeService.convertToCollection(
Expand All @@ -147,6 +154,9 @@ public void convertToCollectionAndCreateNewInstitutionTest() {
assertEquals(newCollection.getCode(), newInstitution.getCode());
assertEquals(newInstitutionName, newInstitution.getName());

Collection c1Updated = collectionService.get(c1.getKey());
assertEquals(newInstitution.getKey(), c1Updated.getInstitutionKey());

assertEquals(1, newCollection.getIdentifiers().size());
assertEquals(1, newCollection.getMachineTags().size());
assertEquals(1, newCollection.getOccurrenceMappings().size());
Expand Down Expand Up @@ -178,26 +188,6 @@ public void convertToCollectionWithExistingInstitutionTest() {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@ public void merge(UUID entityToReplaceKey, UUID replacementKey, String user) {
checkMergeExtraPreconditions(entityToReplace, replacement);

// delete and set the replacement
entityToReplace.setModifiedBy(user);
baseMapper.update(entityToReplace);
mergeableMapper.replace(entityToReplaceKey, replacementKey);

// merge entity fields
baseMapper.update(mergeEntityFields(entityToReplace, replacement));
T updatedEntityToReplace = mergeEntityFields(entityToReplace, replacement);
updatedEntityToReplace.setModifiedBy(user);
baseMapper.update(updatedEntityToReplace);

// copy the identifiers
entityToReplace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ public UUID convertToCollection(
checkArgument(
institutionToConvert.getDeleted() == null, "Cannot convert a deleted institution");

long count =
collectionMapper.count(
CollectionSearchParams.builder().institutionKey(institutionKey).build());
checkArgument(count == 0, "Cannot convert an institution that has collections");

Collection newCollection = new Collection();
newCollection.setKey(UUID.randomUUID());
newCollection.setCode(institutionToConvert.getCode());
Expand Down Expand Up @@ -132,6 +127,10 @@ public UUID convertToCollection(
collectionMapper.create(newCollection);
institutionMapper.convertToCollection(institutionKey, newCollection.getKey());

// move the collections
moveCollectionsToAnotherInstitution(
institutionToConvert.getKey(), newCollection.getInstitutionKey());

// move the identifiers
institutionToConvert
.getIdentifiers()
Expand Down Expand Up @@ -208,14 +207,18 @@ void additionalOperations(Institution entityToReplace, Institution replacement)
personMapper.update(p);
});

moveCollectionsToAnotherInstitution(entityToReplace.getKey(), replacement.getKey());
}

private void moveCollectionsToAnotherInstitution(
UUID sourceInstitutionKey, UUID targetInstitutionKey) {
// move the collections to the entity to keep
List<CollectionDto> collections =
collectionMapper.list(
CollectionSearchParams.builder().institutionKey(entityToReplace.getKey()).build(),
null);
CollectionSearchParams.builder().institutionKey(sourceInstitutionKey).build(), null);
collections.forEach(
c -> {
c.getCollection().setInstitutionKey(replacement.getKey());
c.getCollection().setInstitutionKey(targetInstitutionKey);
collectionMapper.update(c.getCollection());
});
}
Expand Down

0 comments on commit 4bc1d84

Please sign in to comment.