Skip to content

Commit

Permalink
GRSciColl suggestions V1
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Apr 22, 2021
1 parent 763a879 commit 73a7c67
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.gbif.api.vocabulary.Country;
import org.gbif.registry.database.TestCaseDatabaseInitializer;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.service.collections.suggestions.InstitutionChangeSuggestionService;
import org.gbif.registry.ws.it.BaseItTest;
import org.gbif.ws.client.filter.SimplePrincipalProvider;

Expand All @@ -47,25 +46,24 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/** Tests the {@link InstitutionChangeSuggestionService}. */
/** Tests the {@link ChangeSuggestionService}. */
public abstract class BaseChangeSuggestionServiceIT<
T extends CollectionEntity & Contactable & LenientEquals<T>>
T extends CollectionEntity & Contactable & LenientEquals<T>, R extends ChangeSuggestion<T>>
extends BaseItTest {

protected static final String PROPOSER = "proposer";
protected static final Pageable DEFAULT_PAGE = new PagingRequest(0L, 5);

@RegisterExtension
protected TestCaseDatabaseInitializer databaseRule =
TestCaseDatabaseInitializer.builder().dataSource(database.getTestDatabase()).build();
protected TestCaseDatabaseInitializer databaseRule = new TestCaseDatabaseInitializer();

private final ChangeSuggestionService<T> changeSuggestionService;
private final ChangeSuggestionService<T, R> changeSuggestionService;
private final CrudService<T> crudService;

protected BaseChangeSuggestionServiceIT(
SimplePrincipalProvider simplePrincipalProvider,
EsManageServer esServer,
ChangeSuggestionService<T> changeSuggestionService,
ChangeSuggestionService<T, R> changeSuggestionService,
CrudService<T> crudService) {
super(simplePrincipalProvider, esServer);
this.changeSuggestionService = changeSuggestionService;
Expand All @@ -81,7 +79,7 @@ public void newEntitySuggestionTest() {
address.setCountry(Country.DENMARK);
entity.setAddress(address);

ChangeSuggestion<T> suggestion = createEmptyChangeSuggestion();
R suggestion = createEmptyChangeSuggestion();
suggestion.setSuggestedEntity(entity);
suggestion.setType(Type.CREATE);
suggestion.setProposedBy(PROPOSER);
Expand Down Expand Up @@ -141,7 +139,7 @@ public void changeInstitutionSuggestionTest() {
int numberChanges = updateEntity(entity);
address.setCity("city");

ChangeSuggestion<T> suggestion = createEmptyChangeSuggestion();
R suggestion = createEmptyChangeSuggestion();
suggestion.setSuggestedEntity(entity);
suggestion.setType(Type.UPDATE);
suggestion.setEntityKey(entityKey);
Expand Down Expand Up @@ -191,7 +189,7 @@ public void deleteInstitutionSuggestionTest() {
T entity = createEntity();
UUID entityKey = crudService.create(entity);

ChangeSuggestion<T> suggestion = createEmptyChangeSuggestion();
R suggestion = createEmptyChangeSuggestion();
suggestion.setSuggestedEntity(entity);
suggestion.setType(Type.DELETE);
suggestion.setEntityKey(entityKey);
Expand Down Expand Up @@ -227,7 +225,7 @@ public void mergeInstitutionSuggestionTest() {
T entity2 = createEntity();
UUID entity2Key = crudService.create(entity2);

ChangeSuggestion<T> suggestion = createEmptyChangeSuggestion();
R suggestion = createEmptyChangeSuggestion();
suggestion.setSuggestedEntity(entity);
suggestion.setType(Type.MERGE);
suggestion.setEntityKey(entityKey);
Expand Down Expand Up @@ -262,7 +260,7 @@ public void discardSuggestionTest() {
// State
T entity = createEntity();

ChangeSuggestion<T> suggestion = createEmptyChangeSuggestion();
R suggestion = createEmptyChangeSuggestion();
suggestion.setSuggestedEntity(entity);
suggestion.setType(Type.CREATE);
suggestion.setProposedBy(PROPOSER);
Expand Down Expand Up @@ -290,7 +288,7 @@ public void discardSuggestionTest() {
public void listTest() {
// State
T entity = createEntity();
ChangeSuggestion<T> suggestion = createEmptyChangeSuggestion();
R suggestion = createEmptyChangeSuggestion();
suggestion.setSuggestedEntity(entity);
suggestion.setType(Type.CREATE);
suggestion.setProposedBy(PROPOSER);
Expand All @@ -300,7 +298,7 @@ public void listTest() {

T entity2 = createEntity();
UUID entity2Key = crudService.create(entity2);
ChangeSuggestion<T> suggestion2 = createEmptyChangeSuggestion();
R suggestion2 = createEmptyChangeSuggestion();
suggestion2.setSuggestedEntity(entity2);
suggestion2.setEntityKey(entity2Key);
suggestion2.setType(Type.UPDATE);
Expand All @@ -310,7 +308,7 @@ public void listTest() {
int suggKey2 = changeSuggestionService.createChangeSuggestion(suggestion2);

// When
PagingResponse<ChangeSuggestion<T>> results =
PagingResponse<R> results =
changeSuggestionService.list(Status.APPLIED, null, null, null, null, DEFAULT_PAGE);
// Then
assertEquals(0, results.getResults().size());
Expand All @@ -337,7 +335,7 @@ public void listTest() {
assertEquals(0, results.getCount());
}

protected void assertCreatedSuggestion(ChangeSuggestion<T> created) {
protected void assertCreatedSuggestion(R created) {
assertEquals(Status.PENDING, created.getStatus());
assertNull(created.getApplied());
assertNull(created.getDiscarded());
Expand All @@ -362,5 +360,5 @@ protected UUID getReplacedByValue(T entity) {

abstract int reviewEntity(T entity);

abstract ChangeSuggestion<T> createEmptyChangeSuggestion();
abstract R createEmptyChangeSuggestion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
import org.springframework.beans.factory.annotation.Autowired;

/** Tests the {@link CollectionChangeSuggestionService}. */
public class CollectionChangeSuggestionServiceIT extends BaseChangeSuggestionServiceIT<Collection> {
public class CollectionChangeSuggestionServiceIT
extends BaseChangeSuggestionServiceIT<Collection, CollectionChangeSuggestion> {

@Autowired
public CollectionChangeSuggestionServiceIT(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

/** Tests the {@link InstitutionChangeSuggestionService}. */
public class InstitutionChangeSuggestionServiceIT
extends BaseChangeSuggestionServiceIT<Institution> {
extends BaseChangeSuggestionServiceIT<Institution, InstitutionChangeSuggestion> {

private final InstitutionChangeSuggestionService institutionChangeSuggestionService;
private final InstitutionService institutionService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

<sql id="SUGGESTION_PARAMS_UPDATE">
suggested_entity = #{suggestedEntity,jdbcType=OTHER}::jsonb,
status = #{status,jdbcType=OTHER},
comments = #{comments,jdbcType=OTHER,typeHandler=StringArrayTypeHandler},
changes = #{changes,jdbcType=OTHER,typeHandler=SuggestedChangesTypeHandler}::jsonb,
modified = now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public UUID create(T entity) {
return entity.getKey();
}

@Override
public void update(T entity) {
preUpdate(entity);
T entityOld = get(entity.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@
public abstract class BaseChangeSuggestionService<
T extends
CollectionEntity & Taggable & Identifiable & MachineTaggable & Commentable & Contactable
& OccurrenceMappeable>
implements ChangeSuggestionService<T> {
& OccurrenceMappeable,
R extends ChangeSuggestion<T>>
implements ChangeSuggestionService<T, R> {

private static final Logger LOG = LoggerFactory.getLogger(BaseChangeSuggestionService.class);

Expand Down Expand Up @@ -105,7 +106,7 @@ protected BaseChangeSuggestionService(
}

@Override
public int createChangeSuggestion(ChangeSuggestion<T> changeSuggestion) {
public int createChangeSuggestion(R changeSuggestion) {
checkArgument(!changeSuggestion.getComments().isEmpty(), "A comment is required");

if (changeSuggestion.getType() == Type.CREATE) {
Expand All @@ -127,7 +128,7 @@ public int createChangeSuggestion(ChangeSuggestion<T> changeSuggestion) {
throw new IllegalArgumentException("Invalid suggestion type: " + changeSuggestion.getType());
}

protected int createUpdateSuggestion(ChangeSuggestion<T> changeSuggestion) {
protected int createUpdateSuggestion(R changeSuggestion) {
checkArgument(changeSuggestion.getEntityKey() != null);
checkArgument(changeSuggestion.getSuggestedEntity() != null);

Expand All @@ -143,7 +144,7 @@ protected int createUpdateSuggestion(ChangeSuggestion<T> changeSuggestion) {
return dto.getKey();
}

protected int createNewEntitySuggestion(ChangeSuggestion<T> changeSuggestion) {
protected int createNewEntitySuggestion(R changeSuggestion) {
checkArgument(changeSuggestion.getSuggestedEntity() != null);

ChangeSuggestionDto dto = createBaseChangeSuggestionDto(changeSuggestion);
Expand All @@ -154,7 +155,7 @@ protected int createNewEntitySuggestion(ChangeSuggestion<T> changeSuggestion) {
return dto.getKey();
}

protected int createDeleteSuggestion(ChangeSuggestion<T> changeSuggestion) {
protected int createDeleteSuggestion(R changeSuggestion) {
checkArgument(changeSuggestion.getEntityKey() != null);

ChangeSuggestionDto dto = createBaseChangeSuggestionDto(changeSuggestion);
Expand All @@ -166,7 +167,7 @@ protected int createDeleteSuggestion(ChangeSuggestion<T> changeSuggestion) {
return dto.getKey();
}

protected int createMergeSuggestion(ChangeSuggestion<T> changeSuggestion) {
protected int createMergeSuggestion(R changeSuggestion) {
checkArgument(changeSuggestion.getEntityKey() != null);
checkArgument(changeSuggestion.getMergeTargetKey() != null);

Expand All @@ -181,7 +182,7 @@ protected int createMergeSuggestion(ChangeSuggestion<T> changeSuggestion) {
}

@Override
public void updateChangeSuggestion(ChangeSuggestion<T> updatedChangeSuggestion) {
public void updateChangeSuggestion(R updatedChangeSuggestion) {
ChangeSuggestionDto dto = changeSuggestionMapper.get(updatedChangeSuggestion.getKey());

checkArgument(
Expand All @@ -191,7 +192,7 @@ public void updateChangeSuggestion(ChangeSuggestion<T> updatedChangeSuggestion)
if (dto.getType() == Type.CREATE || dto.getType() == Type.UPDATE) {
// we do this to update the suggested entity with the current state and minimize the risk of
// having race conditions
ChangeSuggestion<T> changeSuggestion = dtoToChangeSuggestion(dto);
R changeSuggestion = dtoToChangeSuggestion(dto);

Set<ChangeDto> newChanges =
extractChanges(
Expand All @@ -215,11 +216,13 @@ public void discardChangeSuggestion(int key) {
changeSuggestionMapper.update(dto);
}

public void applyChangeSuggestion(int suggestionKey) {
@Override
public UUID applyChangeSuggestion(int suggestionKey) {
ChangeSuggestionDto dto = changeSuggestionMapper.get(suggestionKey);
ChangeSuggestion<T> changeSuggestion = dtoToChangeSuggestion(dto);
R changeSuggestion = dtoToChangeSuggestion(dto);
UUID createdEntity = null;
if (dto.getType() == Type.CREATE) {
UUID createdEntity = extendedCollectionService.create(changeSuggestion.getSuggestedEntity());
createdEntity = extendedCollectionService.create(changeSuggestion.getSuggestedEntity());
dto.setEntityKey(createdEntity);
} else if (dto.getType() == Type.UPDATE) {
extendedCollectionService.update(changeSuggestion.getSuggestedEntity());
Expand All @@ -228,18 +231,20 @@ public void applyChangeSuggestion(int suggestionKey) {
} else if (dto.getType() == Type.MERGE) {
mergeService.merge(changeSuggestion.getEntityKey(), changeSuggestion.getMergeTargetKey());
} else if (dto.getType() == Type.CONVERSION_TO_COLLECTION) {
applyConversionToCollection(dto);
createdEntity = applyConversionToCollection(dto);
}

dto.setStatus(Status.APPLIED);
dto.setModifiedBy(getUsername());
dto.setApplied(new Date());
dto.setAppliedBy(getUsername());
changeSuggestionMapper.update(dto);

return createdEntity;
}

@Override
public PagingResponse<ChangeSuggestion<T>> list(
public PagingResponse<R> list(
@Nullable Status status,
@Nullable Type type,
@Nullable Country country,
Expand Down Expand Up @@ -267,7 +272,7 @@ public PagingResponse<ChangeSuggestion<T>> list(
newEmptyChangeSuggestion().getProposedBy(),
entityKey);

List<ChangeSuggestion<T>> changeSuggestions =
List<R> changeSuggestions =
dtos.stream().map(this::dtoToChangeSuggestion).collect(Collectors.toList());

return new PagingResponse<>(page, count, changeSuggestions);
Expand Down Expand Up @@ -321,8 +326,7 @@ private Set<ChangeDto> extractChanges(T suggestedEntity, T currentEntity) {
return changes;
}

protected ChangeSuggestionDto createBaseChangeSuggestionDto(
ChangeSuggestion<T> changeSuggestion) {
protected ChangeSuggestionDto createBaseChangeSuggestionDto(R changeSuggestion) {
ChangeSuggestionDto dto = new ChangeSuggestionDto();
dto.setEntityKey(changeSuggestion.getEntityKey());
dto.setStatus(Status.PENDING);
Expand Down Expand Up @@ -356,8 +360,8 @@ protected Country getCountry(T entity) {
}
}

protected ChangeSuggestion<T> dtoToChangeSuggestion(ChangeSuggestionDto dto) {
ChangeSuggestion<T> suggestion = newEmptyChangeSuggestion();
protected R dtoToChangeSuggestion(ChangeSuggestionDto dto) {
R suggestion = newEmptyChangeSuggestion();
suggestion.setKey(dto.getKey());
suggestion.setStatus(dto.getStatus());
suggestion.setType(dto.getType());
Expand Down Expand Up @@ -435,9 +439,9 @@ protected String toJson(T entity) {
}
}

protected abstract ChangeSuggestion<T> newEmptyChangeSuggestion();
protected abstract R newEmptyChangeSuggestion();

protected abstract int createConvertToCollectionSuggestion(ChangeSuggestion<T> changeSuggestion);
protected abstract int createConvertToCollectionSuggestion(R changeSuggestion);

protected abstract void applyConversionToCollection(ChangeSuggestionDto dto);
protected abstract UUID applyConversionToCollection(ChangeSuggestionDto dto);
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
package org.gbif.registry.service.collections.suggestions;

import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.EntityType;
import org.gbif.api.model.collections.suggestions.ChangeSuggestion;
import org.gbif.api.model.collections.suggestions.CollectionChangeSuggestion;
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.model.common.paging.PagingRequest;
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.api.vocabulary.Country;
import org.gbif.registry.persistence.mapper.collections.ChangeSuggestionMapper;
import org.gbif.registry.persistence.mapper.collections.CollectionMapper;
import org.gbif.registry.persistence.mapper.collections.dto.ChangeSuggestionDto;
import org.gbif.registry.service.collections.DefaultCollectionService;
import org.gbif.registry.service.collections.merge.CollectionMergeService;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;

@Service
public class CollectionChangeSuggestionService extends BaseChangeSuggestionService<Collection> {
public class CollectionChangeSuggestionService
extends BaseChangeSuggestionService<Collection, CollectionChangeSuggestion> {

private final ChangeSuggestionMapper changeSuggestionMapper;

Expand All @@ -50,7 +40,7 @@ public CollectionChangeSuggestionService(

@Override
public CollectionChangeSuggestion getChangeSuggestion(int key) {
return (CollectionChangeSuggestion) dtoToChangeSuggestion(changeSuggestionMapper.get(key));
return dtoToChangeSuggestion(changeSuggestionMapper.get(key));
}

@Override
Expand All @@ -59,12 +49,12 @@ protected CollectionChangeSuggestion newEmptyChangeSuggestion() {
}

@Override
protected int createConvertToCollectionSuggestion(ChangeSuggestion<Collection> changeSuggestion) {
protected int createConvertToCollectionSuggestion(CollectionChangeSuggestion changeSuggestion) {
throw new UnsupportedOperationException();
}

@Override
protected void applyConversionToCollection(ChangeSuggestionDto dto) {
protected UUID applyConversionToCollection(ChangeSuggestionDto dto) {
throw new UnsupportedOperationException();
}
}
Loading

0 comments on commit 73a7c67

Please sign in to comment.