From 9415c09ebe8eadb7ff465764f67455911585b08f Mon Sep 17 00:00:00 2001 From: Christian Gendreau Date: Tue, 3 Dec 2024 08:40:45 -0500 Subject: [PATCH] Added ManagedAttributeIdMapper --- .../seqdb/api/ResourceRepositoryConfig.java | 16 ++++++++++ .../api/util/ManagedAttributeIdMapper.java | 31 +++++++++++++++++++ .../SequenceManagedAttributeRepositoryIT.java | 16 ++++++++++ 3 files changed, 63 insertions(+) create mode 100644 src/main/java/ca/gc/aafc/seqdb/api/util/ManagedAttributeIdMapper.java diff --git a/src/main/java/ca/gc/aafc/seqdb/api/ResourceRepositoryConfig.java b/src/main/java/ca/gc/aafc/seqdb/api/ResourceRepositoryConfig.java index 4a6ebaca..f080ac23 100644 --- a/src/main/java/ca/gc/aafc/seqdb/api/ResourceRepositoryConfig.java +++ b/src/main/java/ca/gc/aafc/seqdb/api/ResourceRepositoryConfig.java @@ -1,6 +1,12 @@ package ca.gc.aafc.seqdb.api; +import io.crnk.core.engine.registry.ResourceRegistry; +import javax.inject.Inject; + import ca.gc.aafc.dina.DinaBaseApiAutoConfiguration; +import ca.gc.aafc.seqdb.api.dto.SequenceManagedAttributeDto; +import ca.gc.aafc.seqdb.api.util.ManagedAttributeIdMapper; + import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -11,4 +17,14 @@ @EntityScan("ca.gc.aafc.seqdb.api.entities") @ComponentScan(basePackageClasses = DinaBaseApiAutoConfiguration.class) public class ResourceRepositoryConfig { + + @Inject + @SuppressWarnings({"deprecation", "unchecked"}) + public void setupManagedAttributeLookup(ResourceRegistry resourceRegistry) { + var resourceInfo = resourceRegistry.getEntry(SequenceManagedAttributeDto.class) + .getResourceInformation(); + + resourceInfo.setIdStringMapper( + new ManagedAttributeIdMapper(resourceInfo.getIdStringMapper())); + } } diff --git a/src/main/java/ca/gc/aafc/seqdb/api/util/ManagedAttributeIdMapper.java b/src/main/java/ca/gc/aafc/seqdb/api/util/ManagedAttributeIdMapper.java new file mode 100644 index 00000000..97417783 --- /dev/null +++ b/src/main/java/ca/gc/aafc/seqdb/api/util/ManagedAttributeIdMapper.java @@ -0,0 +1,31 @@ +package ca.gc.aafc.seqdb.api.util; + +import io.crnk.core.engine.parser.StringMapper; +import java.util.UUID; +import lombok.RequiredArgsConstructor; + +/** + * Lets you use either the UUID or the component type + key as the ID. + * e.g. /managed-attribute/generic_molecular_analysis.attribute_name. + */ +@RequiredArgsConstructor +public class ManagedAttributeIdMapper implements StringMapper { + private final StringMapper stringMapper; + + @Override + public Object parse(String input) { + // If the input's not in UUID format then use the raw string as the ID: + try { + UUID.fromString(input); + } catch (IllegalArgumentException e) { + return input; + } + return stringMapper.parse(input); + } + + @Override + public String toString(Object input) { + return stringMapper.toString(input); + } + +} diff --git a/src/test/java/ca/gc/aafc/seqdb/api/repository/SequenceManagedAttributeRepositoryIT.java b/src/test/java/ca/gc/aafc/seqdb/api/repository/SequenceManagedAttributeRepositoryIT.java index 04cc5f02..983c1a66 100644 --- a/src/test/java/ca/gc/aafc/seqdb/api/repository/SequenceManagedAttributeRepositoryIT.java +++ b/src/test/java/ca/gc/aafc/seqdb/api/repository/SequenceManagedAttributeRepositoryIT.java @@ -51,4 +51,20 @@ void create_recordCreated() { SequenceManagedAttribute.ManagedAttributeComponent.GENERIC_MOLECULAR_ANALYSIS, result.getManagedAttributeComponent()); } + + @Test + @WithMockKeycloakUser(groupRole = SequenceManagedAttributeTestFixture.GROUP + ":SUPER_USER") + void findOneByKey_whenKeyProvided_managedAttributeFetched() { + SequenceManagedAttributeDto newAttribute = SequenceManagedAttributeTestFixture.newManagedAttribute(); + newAttribute.setName("Attribute 1"); + newAttribute.setVocabularyElementType(TypedVocabularyElement.VocabularyElementType.INTEGER); + newAttribute.setManagedAttributeComponent(SequenceManagedAttribute.ManagedAttributeComponent.GENERIC_MOLECULAR_ANALYSIS); + + UUID newAttributeUuid = repo.create(newAttribute).getUuid(); + + QuerySpec querySpec = new QuerySpec(SequenceManagedAttributeDto.class); + SequenceManagedAttributeDto fetchedAttribute = repo.findOne("generic_molecular_analysis.attribute_1", querySpec); + + assertEquals(newAttributeUuid, fetchedAttribute.getUuid()); + } }