Skip to content

Commit

Permalink
Bug fix for CORRECT_CMO_PATIENT_ID workflow (#1017)
Browse files Browse the repository at this point in the history
Signed-off-by: Angelica Ochoa <[email protected]>
  • Loading branch information
ao508 authored Nov 16, 2023
1 parent e082832 commit 52c28b8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public interface CrdbMappingService {
String getCmoPatientIdbyDmpId(String dmpId);
String getCmoPatientIdByInputId(String inputId);
String getCmoPatientIdByInputId(String inputId) throws Exception;
CrdbMappingModel getCrdbMappingModelByInputId(String inputId) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.mskcc.smile.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.nats.client.Message;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -106,6 +105,8 @@ public void run() {
String oldCmoPtId = idCorrectionMap.get("oldId");
String newCmoPtId = idCorrectionMap.get("newId");

// grab samples by old cmo patient id and new cmo patient id that
// we are correcting or swapping to
List<SmileSample> samplesByOldCmoPatient =
sampleService.getSamplesByCmoPatientId(oldCmoPtId);
List<SmileSample> samplesByNewCmoPatient =
Expand Down Expand Up @@ -192,26 +193,25 @@ private void setupCorrectCmoPatientIdHandler(Gateway gateway,
public void onMessage(Message msg, Object message) {
LOG.info("Received message on topic: " + CORRECT_CMOPTID_TOPIC);
Map<String, String> incomingDataMap = new HashMap<>();
String oldCmoPatientId = null;
String newCmoPatientId = null;
try {
// do not log contents of incoming message
String incomingDataString = mapper.readValue(
new String(msg.getData(), StandardCharsets.UTF_8),
String.class);
incomingDataMap = mapper.readValue(incomingDataString, Map.class);
} catch (JsonProcessingException e) {
LOG.error("Error processing the incoming data map. "
+ "Refer to NATS logs for more details.");
oldCmoPatientId = crdbMappingService.getCmoPatientIdByInputId(
incomingDataMap.get("oldId"));
newCmoPatientId = crdbMappingService.getCmoPatientIdByInputId(
incomingDataMap.get("newId"));
} catch (Exception e) {
LOG.error("Error parsing the incoming data map: ", e);
}
if (incomingDataMap.isEmpty()) {
LOG.error("Was not able to deserialize incoming message as instance of Map.class - "
+ "please confirm manually that the expected message contents were published");
} else {
String oldCmoPatientId = crdbMappingService.getCmoPatientIdByInputId(
incomingDataMap.get("oldId"));
String newCmoPatientId = crdbMappingService.getCmoPatientIdByInputId(
incomingDataMap.get("newId"));
Boolean crdbMappingStatus = Boolean.TRUE;

// verify that old and new ids resolve to a valid cmo patient id in crdb service
if (oldCmoPatientId == null || oldCmoPatientId.isEmpty()) {
StringBuilder logMessage = new StringBuilder();
Expand Down Expand Up @@ -262,7 +262,6 @@ public void onMessage(Message msg, Object message) {
});
}


@Override
public void shutdown() throws Exception {
if (!initialized) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Object call() {
}

@Override
public String getCmoPatientIdByInputId(String inputId) {
public String getCmoPatientIdByInputId(String inputId) throws Exception {
Callable<Object> task = new Callable<Object>() {
@Override
public Object call() {
Expand All @@ -57,7 +57,9 @@ public Object call() {
};
Object result = runQueryWithForcedTimeout(task);
if (result != null) {
return result.toString();
ArrayList<Object> crdbValues = mapper.convertValue(result, ArrayList.class);
CrdbMappingModel r = new CrdbMappingModel(crdbValues);
return r.getCmoId().replace("\"", "");
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,29 @@ public SmileSample saveSmileSample(SmileSample
if (!existingSample.getSampleClass().equals(sampleMetadata.getTumorOrNormal())) {
existingSample.setSampleClass(sampleMetadata.getTumorOrNormal());
}
// determine where a patient swap is required also

// determine whether a patient swap is required also
if (!sample.getPatient().getSmilePatientId().equals(
existingSample.getPatient().getSmilePatientId())) {
LOG.info("Updating sample-to-patient relationship and removing connection to patient: "
+ existingSample.getPatient().getSmilePatientId());
sampleRepository.removeSamplePatientRelationship(existingSample.getSmileSampleId(),
existingSample.getPatient().getSmilePatientId());
existingSample.setPatient(sample.getPatient());
// merge aliases from existing patient to the patient we are swapping to
SmilePatient existingPatient = existingSample.getPatient();
SmilePatient patientToSwapTo = sample.getPatient();
Boolean updatedPatientAliases = Boolean.FALSE;
for (PatientAlias pa : existingPatient.getPatientAliases()) {
if (!patientToSwapTo.hasPatientAlias(pa)) {
patientToSwapTo.addPatientAlias(pa);
updatedPatientAliases = Boolean.TRUE;
}
}
// save updates to patient we are swapping to if applicable
if (updatedPatientAliases) {
patientService.savePatientMetadata(patientToSwapTo);
}
existingSample.setPatient(patientToSwapTo);
}
sampleRepository.save(existingSample);
toReturn = existingSample;
Expand Down Expand Up @@ -254,7 +269,7 @@ public Boolean updateSampleMetadata(SampleMetadata sampleMetadata, Boolean fromL
sampleRepository.updateRevisableBySampleId(existingSample.getSmileSampleId(), Boolean.TRUE);
return Boolean.TRUE;
}

// no updates to persist to sample, log and return false
LOG.info("There are no updates to persist for sample: "
+ sampleMetadata.getPrimaryId());
Expand Down Expand Up @@ -361,7 +376,8 @@ public List<PublishedSmileSample> getPublishedSmileSamplesByCmoPatientId(
@Override
public List<SmileSample> getSamplesByCmoPatientId(String cmoPatientId) throws Exception {
List<SmileSample> samples = new ArrayList<>();
for (SmileSample sample: sampleRepository.findAllSamplesByCmoPatientId(cmoPatientId)) {
List<SmileSample> samplesFound = sampleRepository.findAllSamplesByCmoPatientId(cmoPatientId);
for (SmileSample sample: samplesFound) {
samples.add(getDetailedSmileSample(sample));
}
return samples;
Expand Down

0 comments on commit 52c28b8

Please sign in to comment.