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

fix: Mapping for scores code optimization #745 #983

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Changes from all 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 @@ -48,6 +48,7 @@ public class ScreeningResponseObservationConverter extends BaseConverter {
"95617-7",
"95616-9",
"95615-1");

private static final Set<String> PHYSICAL_ACTIVITY_REFS = Set.of(
"89555-7",
"68516-4");
Expand All @@ -56,6 +57,11 @@ public class ScreeningResponseObservationConverter extends BaseConverter {
"44250-9",
"44255-8");

private static final Map<String, Set<String>> 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;
Expand All @@ -74,6 +80,7 @@ public List<BundleEntryComponent> convert(
LOG.info("ScreeningResponseObservationConverter::convert BEGIN for interaction id: {}", interactionId);
Map<String, String> questionAndAnswerCode = new HashMap<>();
List<BundleEntryComponent> bundleEntryComponents = new ArrayList<>();
Map<String, List<Reference>> derivedFromMap = new HashMap<>();

for (ScreeningObservationData data : screeningObservationDataList) {
Observation observation = new Observation();
Expand Down Expand Up @@ -144,7 +151,36 @@ public List<BundleEntryComponent> 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<Reference> 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<Reference> 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);
Expand Down Expand Up @@ -318,51 +354,4 @@ private CodeableConcept createCategory(String system, String code, String displa
return category;
}

private void addScoreRefs(Observation observation, ScreeningObservationData currentData,
List<ScreeningObservationData> 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<String> 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<Reference> 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()));
}
}
}

}
Loading