Skip to content

Commit

Permalink
Added ManagedAttributeIdMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
cgendreau committed Dec 3, 2024
1 parent f33fb3f commit 9415c09
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/ca/gc/aafc/seqdb/api/ResourceRepositoryConfig.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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<Object> {
private final StringMapper<Object> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 9415c09

Please sign in to comment.