diff --git a/src/main/java/com/medblocks/openfhir/ProdOpenFhirMappingContext.java b/src/main/java/com/medblocks/openfhir/ProdOpenFhirMappingContext.java index 71dd129..19c459f 100644 --- a/src/main/java/com/medblocks/openfhir/ProdOpenFhirMappingContext.java +++ b/src/main/java/com/medblocks/openfhir/ProdOpenFhirMappingContext.java @@ -89,6 +89,18 @@ public void initMappingCache(final FhirConnectContext context, fhirContextRepo.setMappers(mappers); fhirContextRepo.setSlotMappers(slotMappers); + final String start = context.getContext().getStart(); + if (start != null) { + final List archetypeMappers = mappers.get(start); + if (archetypeMappers == null) { + context.getContext().setStart(start); + } else { + final String overridenMapper = archetypeMappers.get(0).getOpenEhrConfig() + .getArchetype(); + context.getContext().setStart(overridenMapper); + } + } + repository.put(normalizedRepoId, fhirContextRepo); } diff --git a/src/main/java/com/medblocks/openfhir/tofhir/OpenEhrToFhir.java b/src/main/java/com/medblocks/openfhir/tofhir/OpenEhrToFhir.java index bbcea51..7aa06e7 100644 --- a/src/main/java/com/medblocks/openfhir/tofhir/OpenEhrToFhir.java +++ b/src/main/java/com/medblocks/openfhir/tofhir/OpenEhrToFhir.java @@ -164,8 +164,57 @@ public Bundle compositionToFhir(final FhirConnectContext context, final Set createdAndAdded = new HashSet<>(); final Set archetypesAlreadyProcessed = new HashSet<>(); - // first the parent itself - // get mapper by templateid (context) + archetype id (model) + if (StringUtils.isEmpty(context.getContext().getStart()) + || context.getContext().getStart().equals(composition.getArchetypeNodeId())) { + // fallback to the previous logic + // first the parent itself + // get mapper by templateid (context) + archetype id (model) + handleParentCompositionMapping(webTemplate, templateId, creatingBundle, flatJsonObject, + archetypesAlreadyProcessed, isMultipleByResourceType, + intermediateCaches, + createdAndAdded, composition); + + // all content + handleContentCompositionsMapping(webTemplate, templateId, creatingBundle, flatJsonObject, + archetypesAlreadyProcessed, isMultipleByResourceType, + intermediateCaches, + createdAndAdded, composition); + } else { + // find where to start + final ContentItem startingContentItem = composition.getContent().stream() + .filter(c -> c.getArchetypeNodeId().equals(context.getContext().getStart())) + .findAny().orElse(null); + if (startingContentItem == null) { + final String availableStarts = composition.getContent().stream().map(c -> c.getArchetypeNodeId()) + .collect(Collectors.joining(", ")); + log.error( + "context.start archetype '{}' not found within composition content. Available starts are: {}", + context.getContext().getStart(), availableStarts); + return creatingBundle; + } + handleContentItemMapping(webTemplate, + templateId, + creatingBundle, + flatJsonObject, + archetypesAlreadyProcessed, + isMultipleByResourceType, + intermediateCaches, + createdAndAdded, + startingContentItem); + } + + return creatingBundle; + } + + private void handleParentCompositionMapping(final WebTemplate webTemplate, + final String templateId, + final Bundle creatingBundle, + final JsonObject flatJsonObject, + final Set archetypesAlreadyProcessed, + final Map isMultipleByResourceType, + final Map> intermediateCaches, + final Set createdAndAdded, + final Composition composition) { final List parentMappers = openFhirTemplateRepo.getMapperForArchetype( templateId, composition.getArchetypeNodeId()); if (parentMappers != null) { @@ -181,41 +230,68 @@ public Bundle compositionToFhir(final FhirConnectContext context, composition.getArchetypeNodeId(), true); } + } + private void handleContentCompositionsMapping(final WebTemplate webTemplate, + final String templateId, + final Bundle creatingBundle, + final JsonObject flatJsonObject, + final Set archetypesAlreadyProcessed, + final Map isMultipleByResourceType, + final Map> intermediateCaches, + final Set createdAndAdded, + final Composition composition) { // loop through top level content/archetypes within the Composition for (final ContentItem archetypesWithinContent : composition.getContent()) { + handleContentItemMapping(webTemplate, + templateId, + creatingBundle, + flatJsonObject, + archetypesAlreadyProcessed, + isMultipleByResourceType, + intermediateCaches, + createdAndAdded, + archetypesWithinContent); + } + } - // elements instantiated throughout the mapping (FHIR dataelements instantiated, key'd by created object + fhir path + openehr path) - // instanced here so multiple archetypes can share them - final Map instantiatedIntermediateElements = new HashMap<>(); - - final String archetypeNodeId = archetypesWithinContent.getArchetypeNodeId(); - if (archetypesAlreadyProcessed.contains(archetypeNodeId)) { - continue; - } - - // get mapper by templateid (context) + archetype id (model) - final List theMappers = openFhirTemplateRepo.getMapperForArchetype( - templateId, archetypeNodeId); - if (theMappers == null) { - log.error("No mappers defined for archetype within this composition: {}. No mapping possible.", - archetypeNodeId); - continue; - } - handleMappings(theMappers, - createdAndAdded, - intermediateCaches, - isMultipleByResourceType, - flatJsonObject, - webTemplate, - instantiatedIntermediateElements, - creatingBundle, - archetypesAlreadyProcessed, - archetypeNodeId, - false); + private void handleContentItemMapping(final WebTemplate webTemplate, + final String templateId, + final Bundle creatingBundle, + final JsonObject flatJsonObject, + final Set archetypesAlreadyProcessed, + final Map isMultipleByResourceType, + final Map> intermediateCaches, + final Set createdAndAdded, + final ContentItem archetypesWithinContent) { + // elements instantiated throughout the mapping (FHIR dataelements instantiated, key'd by created object + fhir path + openehr path) + // instanced here so multiple archetypes can share them + final Map instantiatedIntermediateElements = new HashMap<>(); + + final String archetypeNodeId = archetypesWithinContent.getArchetypeNodeId(); + if (archetypesAlreadyProcessed.contains(archetypeNodeId)) { + return; } - return creatingBundle; + // get mapper by templateid (context) + archetype id (model) + final List theMappers = openFhirTemplateRepo.getMapperForArchetype( + templateId, archetypeNodeId); + if (theMappers == null) { + log.error("No mappers defined for archetype within this composition: {}. No mapping possible.", + archetypeNodeId); + return; + } + handleMappings(theMappers, + createdAndAdded, + intermediateCaches, + isMultipleByResourceType, + flatJsonObject, + webTemplate, + instantiatedIntermediateElements, + creatingBundle, + archetypesAlreadyProcessed, + archetypeNodeId, + false); } /** diff --git a/src/test/java/com/medblocks/openfhir/TestOpenFhirMappingContext.java b/src/test/java/com/medblocks/openfhir/TestOpenFhirMappingContext.java index fcb3263..7ead604 100644 --- a/src/test/java/com/medblocks/openfhir/TestOpenFhirMappingContext.java +++ b/src/test/java/com/medblocks/openfhir/TestOpenFhirMappingContext.java @@ -135,6 +135,19 @@ private Map> loadMappings(final Fil } }); + final String start = context.getContext().getStart(); + if (start != null) { + final List archetypeMappers = mappers.get(start); + if (archetypeMappers == null) { + context.getContext().setStart(start); + } else { + final String overridenMapper = archetypeMappers.get(0).getOpenEhrConfig() + .getArchetype(); + context.getContext().setStart(overridenMapper); + } + } + + return mappers; } diff --git a/src/test/resources/growth_chart/growth-chart.context.yml b/src/test/resources/growth_chart/growth-chart.context.yml index 13be239..63bc56a 100644 --- a/src/test/resources/growth_chart/growth-chart.context.yml +++ b/src/test/resources/growth_chart/growth-chart.context.yml @@ -17,4 +17,4 @@ context: - "OBSERVATION.height.v2" - "OBSERVATION.body_mass_index.v2" - "OBSERVATION.head_circumference.v1" - start: "OBSERVATION.body_weight.v2" \ No newline at end of file + start: "openEHR-EHR-COMPOSITION.growth_chart.v0" \ No newline at end of file diff --git a/src/test/resources/news2/NEWS2_Context_Mapping.context.yaml b/src/test/resources/news2/NEWS2_Context_Mapping.context.yaml index 1b23e86..433a046 100644 --- a/src/test/resources/news2/NEWS2_Context_Mapping.context.yaml +++ b/src/test/resources/news2/NEWS2_Context_Mapping.context.yaml @@ -20,4 +20,4 @@ context: - "OBSERVATION.pulse.v2" - "OBSERVATION.respiration.v2" - "OBSERVATION.pulse_oximetry.v1" - start: "OBSERVATION.news2.v1" \ No newline at end of file + start: "openEHR-EHR-COMPOSITION.encounter.v1" \ No newline at end of file diff --git a/src/test/resources/rso_poc_acp/acp-poc.context.yml b/src/test/resources/rso_poc_acp/acp-poc.context.yml index 714cc8d..48310ed 100644 --- a/src/test/resources/rso_poc_acp/acp-poc.context.yml +++ b/src/test/resources/rso_poc_acp/acp-poc.context.yml @@ -18,4 +18,4 @@ context: - "OBSERVATION.karnofsky_performance_status_scale.v1" - "EVALUATION.clinical_synopsis.v1" - "CLUSTER.media_file.v1" - start: "EVALUATION.advance_care_directive.v2" \ No newline at end of file + start: "openEHR-EHR-COMPOSITION.report.v1" \ No newline at end of file