Skip to content

Commit

Permalink
endpoints for sourceable fields
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Nov 30, 2021
1 parent a1da893 commit bc4c688
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyInt;
Expand Down Expand Up @@ -319,6 +320,11 @@ public void discardChangeSuggestionTest() {
assertDoesNotThrow(() -> getPrimaryCollectionEntityClient().discardChangeSuggestion(1));
}

@Test
public void getSourceableFieldsTest() {
assertFalse(getPrimaryCollectionEntityClient().getSourceableFields().isEmpty());
}

protected abstract PrimaryCollectionEntityService<T> getMockPrimaryEntityService();

protected abstract DuplicatesService getMockDuplicatesService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
import org.gbif.api.model.collections.MasterSourceType;
import org.gbif.api.model.collections.PrimaryCollectionEntity;
import org.gbif.api.model.collections.Sourceable;
import org.gbif.api.model.registry.MachineTaggable;
import org.gbif.api.model.collections.SourceableField;
import org.gbif.api.model.collections.Sourceables;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -40,6 +43,9 @@ public class MasterSourceUtils {
public static final Map<MasterSourceType, List<LockableField>> COLLECTION_LOCKABLE_FIELDS =
new EnumMap<>(MasterSourceType.class);

public static final List<SourceableField> INSTITUTION_SOURCEABLE_FIELDS = new ArrayList<>();
public static final List<SourceableField> COLLECTION_SOURCEABLE_FIELDS = new ArrayList<>();

static {
// create lockable fields for Institution
Arrays.stream(Institution.class.getDeclaredFields())
Expand All @@ -55,6 +61,12 @@ public class MasterSourceUtils {
Arrays.stream(f.getDeclaredAnnotationsByType(Sourceable.class))
.anyMatch(a -> !a.overridable() && a.sourceableParts().length == 0))
.forEach(f -> createLockableField(f, Collection.class, COLLECTION_LOCKABLE_FIELDS));

// create sourceable fields
Arrays.stream(Institution.class.getDeclaredFields())
.forEach(f -> createSourceableField(f).ifPresent(INSTITUTION_SOURCEABLE_FIELDS::add));
Arrays.stream(Collection.class.getDeclaredFields())
.forEach(f -> createSourceableField(f).ifPresent(COLLECTION_SOURCEABLE_FIELDS::add));
}

public static <T extends PrimaryCollectionEntity> boolean hasExternalMasterSource(T entity) {
Expand All @@ -76,16 +88,51 @@ private static void createLockableField(
Method getter = clazz.getDeclaredMethod(getterPrefix + methodName);
Method setter = clazz.getDeclaredMethod("set" + methodName, f.getType());

Optional.ofNullable(f.getDeclaredAnnotation(Sourceable.class))
.map(Sourceable::masterSources)
.ifPresent(
sources ->
Arrays.stream(sources)
.forEach(
ms ->
fieldsMap
.computeIfAbsent(ms, v -> new ArrayList<>())
.add(new LockableField(getter, setter))));
Consumer<Sourceable> processAnnotation =
annotation ->
Arrays.stream(annotation.masterSources())
.forEach(
ms ->
fieldsMap
.computeIfAbsent(ms, v -> new ArrayList<>())
.add(new LockableField(getter, setter)));

Optional.ofNullable(f.getDeclaredAnnotation(Sourceable.class)).ifPresent(processAnnotation);
Optional.ofNullable(f.getDeclaredAnnotation(Sourceables.class))
.map(Sourceables::value)
.ifPresent(annotations -> Arrays.stream(annotations).forEach(processAnnotation));
}

private static Optional<SourceableField> createSourceableField(Field f) {
Optional<Sourceable> sourceable =
Optional.ofNullable(f.getDeclaredAnnotation(Sourceable.class));
Optional<Sourceables> sourceables =
Optional.ofNullable(f.getDeclaredAnnotation(Sourceables.class));

if (sourceable.isPresent() || sourceables.isPresent()) {
SourceableField sourceableField = new SourceableField();
sourceableField.setFieldName(f.getName());

List<Sourceable> sourceablesList = new ArrayList<>();
sourceable.ifPresent(sourceablesList::add);
sourceables.ifPresent(value -> Collections.addAll(sourceablesList, value.value()));

// there shouldn't be more than annotation with the same master source
sourceablesList.forEach(
s ->
Arrays.stream(s.masterSources())
.forEach(
ms -> {
SourceableField.Source source = new SourceableField.Source();
source.setMasterSource(ms);
source.setOverridable(s.overridable());
source.setSourceableParts(Arrays.asList(s.sourceableParts()));
sourceableField.getSources().add(source);
}));

return Optional.of(sourceableField);
}
return Optional.empty();
}

@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.gbif.api.model.collections.Contactable;
import org.gbif.api.model.collections.OccurrenceMapping;
import org.gbif.api.model.collections.Person;
import org.gbif.api.model.collections.SourceableField;
import org.gbif.api.model.collections.duplicates.DuplicatesRequest;
import org.gbif.api.model.collections.duplicates.DuplicatesResult;
import org.gbif.api.model.collections.merge.MergeParams;
Expand Down Expand Up @@ -168,4 +169,10 @@ PagingResponse<R> listChangeSuggestion(
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
ApplySuggestionResult applyChangeSuggestion(@PathVariable("key") int key);

@RequestMapping(
method = RequestMethod.GET,
value = "sourceableFields",
produces = MediaType.APPLICATION_JSON_VALUE)
List<SourceableField> getSourceableFields();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.gbif.api.annotation.Trim;
import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.CollectionImportParams;
import org.gbif.api.model.collections.SourceableField;
import org.gbif.api.model.collections.request.CollectionSearchRequest;
import org.gbif.api.model.collections.suggestions.CollectionChangeSuggestion;
import org.gbif.api.model.collections.view.CollectionView;
Expand All @@ -29,6 +30,7 @@
import org.gbif.registry.service.collections.duplicates.CollectionDuplicatesService;
import org.gbif.registry.service.collections.merge.CollectionMergeService;
import org.gbif.registry.service.collections.suggestions.CollectionChangeSuggestionService;
import org.gbif.registry.service.collections.utils.MasterSourceUtils;
import org.gbif.registry.ws.export.CsvWriter;

import java.io.BufferedWriter;
Expand Down Expand Up @@ -159,4 +161,9 @@ public UUID createFromDataset(@RequestBody @Trim CollectionImportParams importPa
return collectionService.createFromDataset(
importParams.getDatasetKey(), importParams.getCollectionCode());
}

@GetMapping("sourceableFields")
public List<SourceableField> getSourceableFields() {
return MasterSourceUtils.COLLECTION_SOURCEABLE_FIELDS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.gbif.api.annotation.Trim;
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.InstitutionImportParams;
import org.gbif.api.model.collections.SourceableField;
import org.gbif.api.model.collections.merge.ConvertToCollectionParams;
import org.gbif.api.model.collections.request.InstitutionSearchRequest;
import org.gbif.api.model.collections.suggestions.InstitutionChangeSuggestion;
Expand All @@ -29,6 +30,7 @@
import org.gbif.registry.service.collections.duplicates.InstitutionDuplicatesService;
import org.gbif.registry.service.collections.merge.InstitutionMergeService;
import org.gbif.registry.service.collections.suggestions.InstitutionChangeSuggestionService;
import org.gbif.registry.service.collections.utils.MasterSourceUtils;
import org.gbif.registry.ws.export.CsvWriter;

import java.io.BufferedWriter;
Expand Down Expand Up @@ -165,4 +167,9 @@ public UUID createFromOrganization(@RequestBody @Trim InstitutionImportParams im
return institutionService.createFromOrganization(
importParams.getOrganizationKey(), importParams.getInstitutionCode());
}

@GetMapping("sourceableFields")
public List<SourceableField> getSourceableFields() {
return MasterSourceUtils.INSTITUTION_SOURCEABLE_FIELDS;
}
}

0 comments on commit bc4c688

Please sign in to comment.