Skip to content
Merged
Show file tree
Hide file tree
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 @@ -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<OpenFhirFhirConnectModelMapper> 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);
}

Expand Down
138 changes: 107 additions & 31 deletions src/main/java/com/medblocks/openfhir/tofhir/OpenEhrToFhir.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,57 @@ public Bundle compositionToFhir(final FhirConnectContext context,
final Set<String> createdAndAdded = new HashSet<>();
final Set<String> 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<String> archetypesAlreadyProcessed,
final Map<String, Boolean> isMultipleByResourceType,
final Map<String, Map<String, Object>> intermediateCaches,
final Set<String> createdAndAdded,
final Composition composition) {
final List<OpenFhirFhirConnectModelMapper> parentMappers = openFhirTemplateRepo.getMapperForArchetype(
templateId, composition.getArchetypeNodeId());
if (parentMappers != null) {
Expand All @@ -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<String> archetypesAlreadyProcessed,
final Map<String, Boolean> isMultipleByResourceType,
final Map<String, Map<String, Object>> intermediateCaches,
final Set<String> 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<String, Object> instantiatedIntermediateElements = new HashMap<>();

final String archetypeNodeId = archetypesWithinContent.getArchetypeNodeId();
if (archetypesAlreadyProcessed.contains(archetypeNodeId)) {
continue;
}

// get mapper by templateid (context) + archetype id (model)
final List<OpenFhirFhirConnectModelMapper> 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<String> archetypesAlreadyProcessed,
final Map<String, Boolean> isMultipleByResourceType,
final Map<String, Map<String, Object>> intermediateCaches,
final Set<String> 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<String, Object> 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<OpenFhirFhirConnectModelMapper> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ private Map<String, List<OpenFhirFhirConnectModelMapper>> loadMappings(final Fil
}
});

final String start = context.getContext().getStart();
if (start != null) {
final List<OpenFhirFhirConnectModelMapper> 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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/growth_chart/growth-chart.context.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ context:
- "OBSERVATION.height.v2"
- "OBSERVATION.body_mass_index.v2"
- "OBSERVATION.head_circumference.v1"
start: "OBSERVATION.body_weight.v2"
start: "openEHR-EHR-COMPOSITION.growth_chart.v0"
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ context:
- "OBSERVATION.pulse.v2"
- "OBSERVATION.respiration.v2"
- "OBSERVATION.pulse_oximetry.v1"
start: "OBSERVATION.news2.v1"
start: "openEHR-EHR-COMPOSITION.encounter.v1"
2 changes: 1 addition & 1 deletion src/test/resources/rso_poc_acp/acp-poc.context.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
start: "openEHR-EHR-COMPOSITION.report.v1"
Loading