From 52c28b8775cdb4136ab7f247d96fb4017928de0d Mon Sep 17 00:00:00 2001 From: ao508 <15623749+ao508@users.noreply.github.com> Date: Thu, 16 Nov 2023 16:04:49 -0500 Subject: [PATCH] Bug fix for CORRECT_CMO_PATIENT_ID workflow (#1017) Signed-off-by: Angelica Ochoa <15623749+ao508@users.noreply.github.com> --- .../smile/service/CrdbMappingService.java | 2 +- .../CorrectCmoPatientHandlingServiceImpl.java | 21 ++++++++-------- .../service/impl/CrdbMappingServiceImpl.java | 6 +++-- .../smile/service/impl/SampleServiceImpl.java | 24 +++++++++++++++---- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/service/src/main/java/org/mskcc/smile/service/CrdbMappingService.java b/service/src/main/java/org/mskcc/smile/service/CrdbMappingService.java index f184a6d5..ec0be6a7 100644 --- a/service/src/main/java/org/mskcc/smile/service/CrdbMappingService.java +++ b/service/src/main/java/org/mskcc/smile/service/CrdbMappingService.java @@ -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; } diff --git a/service/src/main/java/org/mskcc/smile/service/impl/CorrectCmoPatientHandlingServiceImpl.java b/service/src/main/java/org/mskcc/smile/service/impl/CorrectCmoPatientHandlingServiceImpl.java index ffe3b403..2abfccde 100644 --- a/service/src/main/java/org/mskcc/smile/service/impl/CorrectCmoPatientHandlingServiceImpl.java +++ b/service/src/main/java/org/mskcc/smile/service/impl/CorrectCmoPatientHandlingServiceImpl.java @@ -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; @@ -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 samplesByOldCmoPatient = sampleService.getSamplesByCmoPatientId(oldCmoPtId); List samplesByNewCmoPatient = @@ -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 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(); @@ -262,7 +262,6 @@ public void onMessage(Message msg, Object message) { }); } - @Override public void shutdown() throws Exception { if (!initialized) { diff --git a/service/src/main/java/org/mskcc/smile/service/impl/CrdbMappingServiceImpl.java b/service/src/main/java/org/mskcc/smile/service/impl/CrdbMappingServiceImpl.java index afd76385..31f1b938 100644 --- a/service/src/main/java/org/mskcc/smile/service/impl/CrdbMappingServiceImpl.java +++ b/service/src/main/java/org/mskcc/smile/service/impl/CrdbMappingServiceImpl.java @@ -48,7 +48,7 @@ public Object call() { } @Override - public String getCmoPatientIdByInputId(String inputId) { + public String getCmoPatientIdByInputId(String inputId) throws Exception { Callable task = new Callable() { @Override public Object call() { @@ -57,7 +57,9 @@ public Object call() { }; Object result = runQueryWithForcedTimeout(task); if (result != null) { - return result.toString(); + ArrayList crdbValues = mapper.convertValue(result, ArrayList.class); + CrdbMappingModel r = new CrdbMappingModel(crdbValues); + return r.getCmoId().replace("\"", ""); } return null; } diff --git a/service/src/main/java/org/mskcc/smile/service/impl/SampleServiceImpl.java b/service/src/main/java/org/mskcc/smile/service/impl/SampleServiceImpl.java index 580cbba0..aeb58114 100644 --- a/service/src/main/java/org/mskcc/smile/service/impl/SampleServiceImpl.java +++ b/service/src/main/java/org/mskcc/smile/service/impl/SampleServiceImpl.java @@ -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; @@ -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()); @@ -361,7 +376,8 @@ public List getPublishedSmileSamplesByCmoPatientId( @Override public List getSamplesByCmoPatientId(String cmoPatientId) throws Exception { List samples = new ArrayList<>(); - for (SmileSample sample: sampleRepository.findAllSamplesByCmoPatientId(cmoPatientId)) { + List samplesFound = sampleRepository.findAllSamplesByCmoPatientId(cmoPatientId); + for (SmileSample sample: samplesFound) { samples.add(getDetailedSmileSample(sample)); } return samples;