diff --git a/hub-prime/src/main/java/org/techbd/service/converters/shinny/ScreeningResponseObservationConverter.java b/hub-prime/src/main/java/org/techbd/service/converters/shinny/ScreeningResponseObservationConverter.java index 1e1255a714..a3f2b5e3cd 100644 --- a/hub-prime/src/main/java/org/techbd/service/converters/shinny/ScreeningResponseObservationConverter.java +++ b/hub-prime/src/main/java/org/techbd/service/converters/shinny/ScreeningResponseObservationConverter.java @@ -48,6 +48,7 @@ public class ScreeningResponseObservationConverter extends BaseConverter { "95617-7", "95616-9", "95615-1"); + private static final Set PHYSICAL_ACTIVITY_REFS = Set.of( "89555-7", "68516-4"); @@ -56,6 +57,11 @@ public class ScreeningResponseObservationConverter extends BaseConverter { "44250-9", "44255-8"); + private static final Map> QUESTION_CODE_REF_MAP = Map.of( + "95614-4", INTERPERSONAL_SAFETY_REFS, + "77594-0", PHYSICAL_ACTIVITY_REFS, + "71969-0", MENTAL_STATE_REFS); + @Override public ResourceType getResourceType() { return ResourceType.Observation; @@ -74,6 +80,7 @@ public List convert( LOG.info("ScreeningResponseObservationConverter::convert BEGIN for interaction id: {}", interactionId); Map questionAndAnswerCode = new HashMap<>(); List bundleEntryComponents = new ArrayList<>(); + Map> derivedFromMap = new HashMap<>(); for (ScreeningObservationData data : screeningObservationDataList) { Observation observation = new Observation(); @@ -144,7 +151,36 @@ public List convert( } observation.setIssued(DateUtil.convertStringToDate(data.getRecordedTime())); questionAndAnswerCode.put(data.getQuestionCode(), data.getAnswerCode()); - addScoreRefs(observation, data, screeningObservationDataList); + + QUESTION_CODE_REF_MAP.forEach((key, value) -> { + if (key.equals(data.getQuestionCode())) { + List derivedFromRefs = screeningObservationDataList.stream() + .filter(obs -> value.contains(obs.getQuestionCode())) + .map(obs -> { + String derivedFromId = CsvConversionUtil.sha256( + obs.getQuestionCodeDisplay() + .replace(" ", "") + + obs.getQuestionCode() + + obs.getEncounterId()); + + return new Reference("Observation/" + derivedFromId); + }) + .collect(Collectors.toList()); + derivedFromMap.put(observationId, derivedFromRefs); + } + + }); + + List derivedRefs = derivedFromMap.get(observationId); + + if (derivedRefs != null && !derivedRefs.isEmpty()) { + observation.setDerivedFrom(derivedRefs); + if (LOG.isDebugEnabled()) { + derivedRefs.forEach( + ref -> LOG.debug("Added reference {} for the observation {}", + ref.getReference(), observationId)); + } + } BundleEntryComponent entry = new BundleEntryComponent(); entry.setFullUrl(fullUrl); @@ -318,51 +354,4 @@ private CodeableConcept createCategory(String system, String code, String displa return category; } - private void addScoreRefs(Observation observation, ScreeningObservationData currentData, - List allObservations) { - - // Check for interpersonal safety, physical activity, or mental state codes - if (!"95614-4".equals(currentData.getQuestionCode()) && - !"77594-0".equals(currentData.getQuestionCode()) && - !"71969-0".equals(currentData.getQuestionCode())) { - return; - } - - LOG.info("Processing observation with code: {}", currentData.getQuestionCode()); - - Set relevantRefs; - String observationType; - - // Determine which reference set to use based on question code - if ("95614-4".equals(currentData.getQuestionCode())) { - relevantRefs = INTERPERSONAL_SAFETY_REFS; - observationType = "InterpersonalSafety"; - } else if ("77594-0".equals(currentData.getQuestionCode())) { - relevantRefs = PHYSICAL_ACTIVITY_REFS; - observationType = "PhysicalActivity"; - } else { - relevantRefs = MENTAL_STATE_REFS; - observationType = "MentalState"; - } - List derivedFromRefs = allObservations.stream() - .filter(obs -> relevantRefs.contains(obs.getQuestionCode())) - .map(obs -> { - String observationId = CsvConversionUtil.sha256( - obs.getQuestionCodeDisplay().replace(" ", "") + - obs.getQuestionCode() + obs.getEncounterId()); - return new Reference("Observation/" + observationId); - }) - .collect(Collectors.toList()); - - if (!derivedFromRefs.isEmpty()) { - observation.setDerivedFrom(derivedFromRefs); - LOG.info("Added {} derived references for question code {}", - derivedFromRefs.size(), currentData.getQuestionCode()); - - if (LOG.isDebugEnabled()) { - derivedFromRefs.forEach(ref -> LOG.debug("Added reference: {}", ref.getReference())); - } - } - } - }