Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upsert specimens #73

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public enum FdoProfile {
private final int index;


private FdoProfile(String attribute, int index) {
FdoProfile(String attribute, int index) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Way are these no longer private?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"If you do not specify an access modifier the enum constructor it will be implicitly private."

Source

Slowly going to remove the private constructors when I touch enum files

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find 💯

this.attribute = attribute;
this.index = index;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

public enum BaseTypeOfSpecimen {
@JsonProperty("Material entity") MATERIAL("Material entity"),
@JsonProperty("Information artefact") INFO("informationArtefact");
@JsonProperty("Information artefact") INFO("Information Artefact");

private final String state;


private BaseTypeOfSpecimen(String state) {
BaseTypeOfSpecimen(String state) {
this.state = state;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ public enum MaterialSampleType {
@JsonProperty("Whole organism specimen") WHOLE_ORG("Whole organism specimen"),
@JsonProperty("Organism part") ORG_PART("Organism part"),
@JsonProperty("Organism product") ORG_PRODUCT("Organism product"),
@JsonProperty("Biome aggegation") AGGR_BIOME("Biome aggegation"),
@JsonProperty("Biome aggregation") AGGR_BIOME("Biome aggregation"),
@JsonProperty("Bundle biome aggregation") BUNDLE_BIOME("Bundle biome aggregation"),
@JsonProperty("Fossil") FOSSIL("Fossil"),
@JsonProperty("Any biological specimen") ANY_BIO("Any biological specimen"),
@JsonProperty("Aggregation") AGGR("Aggregation"),
@JsonProperty("Slurry biome aggegation") SLURRY_BIOME("Slurry biome aggegation"),
@JsonProperty("Slurry biome aggregation") SLURRY_BIOME("Slurry biome aggregation"),
@JsonProperty("Other solid object") OTHER_SOLID("Other solid object"),
@JsonProperty("Fluid in container") FLUID("Fluid in container"),
@JsonProperty("Anthropogenic aggregation") ANTHRO_AGGR("Anthropogenic aggregation"),
Expand All @@ -22,7 +22,7 @@ public enum MaterialSampleType {

private final String state;

private MaterialSampleType(String state) {
MaterialSampleType(String state) {
this.state = state;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static eu.dissco.core.handlemanager.domain.FdoProfile.PID_RECORD_ISSUE_NUMBER;
import static eu.dissco.core.handlemanager.domain.FdoProfile.PID_STATUS;
import static eu.dissco.core.handlemanager.domain.FdoProfile.PRIMARY_SPECIMEN_OBJECT_ID;
import static org.jooq.impl.DSL.select;

import eu.dissco.core.handlemanager.domain.repsitoryobjects.HandleAttribute;
import eu.dissco.core.handlemanager.exceptions.PidCreationException;
Expand Down Expand Up @@ -73,8 +74,18 @@ public List<HandleAttribute> resolveHandleAttributes(List<byte[]> handles) {

public List<HandleAttribute> searchByNormalisedPhysicalIdentifier(
List<byte[]> normalisedPhysicalIdentifiers) {

return searchByNormalisedPhysicalIdentifierQuery(normalisedPhysicalIdentifiers)
return context.select(HANDLES.IDX, HANDLES.HANDLE, HANDLES.TYPE, HANDLES.DATA)
.from(HANDLES)
.where(HANDLES.HANDLE.in(select(HANDLES.HANDLE).from(HANDLES)
.where(HANDLES.TYPE.eq(PID_STATUS.get().getBytes(StandardCharsets.UTF_8)))
.and(HANDLES.DATA.notEqual("ARCHIVED".getBytes(StandardCharsets.UTF_8)))
.and(HANDLES.HANDLE.in(
select(HANDLES.HANDLE).from(HANDLES)
.where(HANDLES.TYPE.eq(NORMALISED_SPECIMEN_OBJECT_ID.get().getBytes(
StandardCharsets.UTF_8)))
.and(HANDLES.DATA.in(normalisedPhysicalIdentifiers))
))))
.and(HANDLES.TYPE.eq(NORMALISED_SPECIMEN_OBJECT_ID.get().getBytes(StandardCharsets.UTF_8)))
.fetch(this::mapToAttribute);
}

Expand Down
16 changes: 2 additions & 14 deletions src/main/java/eu/dissco/core/handlemanager/service/PidService.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,9 @@ private ProcessedDigitalSpecimenRequest processSpecimenRequests(
var registeredPhysicalIdentiferMap = registeredPhysicalIdentifiers.stream()
.collect(Collectors.toMap(row -> new String(row.getData(), StandardCharsets.UTF_8),
row -> new String(row.getHandle(), StandardCharsets.UTF_8)));
var writableHandles = pidRepository.checkHandlesWritable(
registeredPhysicalIdentiferMap.values().stream().map(s -> s.getBytes(
StandardCharsets.UTF_8)).toList()).stream()
.map(h -> new String(h, StandardCharsets.UTF_8)).collect(Collectors.toSet());
var updates = specimenRequests.stream()
.filter(request -> requestExistsAndIsWriteable(request, registeredPhysicalIdentiferMap,
writableHandles))
.filter(request -> registeredPhysicalIdentiferMap.containsKey(
request.getNormalisedPrimarySpecimenObjectId()))
.map(request -> new DigitalSpecimenUpdateWrapper(
registeredPhysicalIdentiferMap.get(request.getNormalisedPrimarySpecimenObjectId()),
request
Expand All @@ -366,14 +362,6 @@ private ProcessedDigitalSpecimenRequest processSpecimenRequests(
return new ProcessedDigitalSpecimenRequest(specimenRequests, updates);
}

private boolean requestExistsAndIsWriteable(DigitalSpecimenRequest request,
Map<String, String> registeredPhysicalIdentifiers, Set<String> writableHandles) {
return registeredPhysicalIdentifiers.containsKey(request.getNormalisedPrimarySpecimenObjectId())
&&
writableHandles.contains(
registeredPhysicalIdentifiers.get(request.getNormalisedPrimarySpecimenObjectId()));
}

protected List<HandleAttribute> createMediaObject(List<JsonNode> requestAttributes,
Iterator<byte[]> handleIterator)
throws InvalidRequestException, JsonProcessingException, PidResolutionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,24 @@ void testSearchByPhysicalSpecimenId() throws Exception {
assertThat(response).isEqualTo(expected);
}

@Test
void testSearchByPhysicalSpecimenIdIsArchived() {
//Given
var handle = HANDLE.getBytes(StandardCharsets.UTF_8);
var record = List.of(new HandleAttribute(NORMALISED_SPECIMEN_OBJECT_ID, handle,
NORMALISED_PRIMARY_SPECIMEN_OBJECT_ID_TESTVAL),
new HandleAttribute(PID_STATUS, handle, "ARCHIVED"));

postAttributes(record);

// When
var response = pidRepository.searchByNormalisedPhysicalIdentifier(
List.of(NORMALISED_PRIMARY_SPECIMEN_OBJECT_ID_TESTVAL.getBytes(StandardCharsets.UTF_8)));

// Then
assertThat(response).isEmpty();
}

@Test
void testUpdateRecord() throws Exception {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ void testCreateDigitalSpecimenSpecimenExists() throws Exception {
ObjectType.DIGITAL_SPECIMEN);

given(pidNameGeneratorService.genHandleList(1)).willReturn(new ArrayList<>(List.of(handle)));
given(pidRepository.checkHandlesWritable(anyList())).willReturn(List.of(handle));
given(profileProperties.getDomain()).willReturn(HANDLE_DOMAIN);
given(pidRepository.searchByNormalisedPhysicalIdentifier(anyList())).willReturn(List.of(
new HandleAttribute(FdoProfile.NORMALISED_SPECIMEN_OBJECT_ID, handle,
Expand All @@ -395,37 +394,6 @@ void testCreateDigitalSpecimenSpecimenExists() throws Exception {
assertThat(result).isEqualTo(responseExpected);
}

@Test
void testCreateDigitalSpecimenSpecimenExistsNotWritable() throws Exception {
// Given
byte[] handle = handles.get(0);
var digitalSpecimen = givenDigitalSpecimenRequestObjectNullOptionals();
var request = genCreateRecordRequest(digitalSpecimen,
RECORD_TYPE_DS);
List<HandleAttribute> digitalSpecimenAttributes = genDigitalSpecimenAttributes(handle);
var digitalSpecimenSublist = digitalSpecimenAttributes.stream()
.filter(row -> row.getType().equals(PRIMARY_SPECIMEN_OBJECT_ID.get())).toList();
var responseExpected = givenRecordResponseWriteSmallResponse(digitalSpecimenSublist,
List.of(handle),
ObjectType.DIGITAL_SPECIMEN);

given(pidNameGeneratorService.genHandleList(1)).willReturn(new ArrayList<>(List.of(handle)));
given(profileProperties.getDomain()).willReturn(HANDLE_DOMAIN);
given(pidRepository.searchByNormalisedPhysicalIdentifier(anyList())).willReturn(List.of(
new HandleAttribute(FdoProfile.NORMALISED_SPECIMEN_OBJECT_ID, handle,
digitalSpecimen.getNormalisedPrimarySpecimenObjectId())));
given(fdoRecordService.prepareDigitalSpecimenRecordAttributes(digitalSpecimen,
handle)).willReturn(digitalSpecimenAttributes);

// When
var result = service.createRecords(List.of(request));

// Then
then(pidRepository).should()
.postAttributesToDb(CREATED.getEpochSecond(), digitalSpecimenAttributes);
assertThat(result).isEqualTo(responseExpected);
}

@Test
void testCreateMediaObjectRecord() throws Exception {
// Given
Expand Down