use of io.github.linuxforhealth.api.InputDataExtractor in project hl7v2-fhir-converter by LinuxForHealth.
the class HL7MessageEngine method transform.
/**
* Converts a HL7 message to a FHIR bundle with the list of resources specified
*
* @see io.github.linuxforhealth.api.MessageEngine#transform(io.github.linuxforhealth.api.InputDataExtractor,
* java.lang.Iterable, java.util.Map)
*/
@Override
public Bundle transform(final InputDataExtractor dataInput, final Iterable<FHIRResourceTemplate> resources, final Map<String, EvaluationResult> contextValues) {
Preconditions.checkArgument(dataInput != null, "dataInput cannot be null");
Preconditions.checkArgument(contextValues != null, "contextValues cannot be null");
Preconditions.checkArgument(resources != null, "resources cannot be null");
HL7MessageData hl7DataInput = (HL7MessageData) dataInput;
Bundle bundle = initBundle();
Map<String, EvaluationResult> localContextValues = new HashMap<>(contextValues);
// Add run-time properties to localContextVariables
for (Map.Entry<String, String> entry : getFHIRContext().getProperties().entrySet()) {
localContextValues.put(entry.getKey(), new SimpleEvaluationResult<String>(entry.getValue()));
}
// Add the ZoneId runtime property to localContextVariables
// If it is not passed in (null), use "" (empty) to satisfy parser null checks in .yml parsing
String zoneIdText = getFHIRContext().getZoneIdText() != null ? getFHIRContext().getZoneIdText() : "";
localContextValues.put("ZONEID", new SimpleEvaluationResult<String>(zoneIdText));
List<ResourceResult> resourceResultsWithEvalLater = new ArrayList<>();
for (FHIRResourceTemplate genericTemplate : resources) {
HL7FHIRResourceTemplate hl7ResourceTemplate = (HL7FHIRResourceTemplate) genericTemplate;
ResourceModel rs = genericTemplate.getResource();
List<ResourceResult> resourceResults = new ArrayList<>();
try {
MDC.put(RESOURCE, rs.getName());
List<ResourceResult> results = generateResources(hl7DataInput, hl7ResourceTemplate, localContextValues);
if (results != null) {
resourceResults.addAll(results);
results.stream().filter(r -> (r.getPendingExpressions() != null && !r.getPendingExpressions().isEmpty())).forEach(re -> resourceResultsWithEvalLater.add(re));
List<ResourceResult> resultsToAddToBundle = results.stream().filter(r -> (r.getPendingExpressions() == null || r.getPendingExpressions().isEmpty())).collect(Collectors.toList());
addResourceToBundle(bundle, resultsToAddToBundle);
}
resourceResults.removeIf(isEmpty());
Map<String, EvaluationResult> newContextValues = getContextValuesFromResource(hl7ResourceTemplate, resourceResults);
localContextValues.putAll(newContextValues);
} catch (IllegalArgumentException | IllegalStateException e) {
LOGGER.error("Exception during resource {} generation", rs.getName());
LOGGER.debug("Exception during resource {} generation", rs.getName(), e);
} finally {
MDC.remove(RESOURCE);
}
}
for (ResourceResult r : resourceResultsWithEvalLater) {
MDC.put(RESOURCE, "PendingExpressions");
try {
Map<String, EvaluationResult> primaryContextValues = new HashMap<>(localContextValues);
r.getPendingExpressions().getContextValues().entrySet().forEach(e -> primaryContextValues.putIfAbsent(e.getKey(), e.getValue()));
ResourceEvaluationResult res = ExpressionUtility.evaluate(hl7DataInput, primaryContextValues, r.getPendingExpressions().getExpressions());
Map<String, Object> resolvedValues = new HashMap<>();
resolvedValues.putAll(r.getValue().getResource());
resolvedValues.putAll(res.getResolveValues());
List<ResourceValue> additionalResources = new ArrayList<>();
additionalResources.addAll(r.getAdditionalResources());
additionalResources.addAll(res.getAdditionalResolveValues());
ResourceResult updatedResourceResult = new ResourceResult(new SimpleResourceValue(resolvedValues, r.getValue().getFHIRResourceType()), additionalResources, r.getGroupId());
addResourceToBundle(bundle, Lists.newArrayList(updatedResourceResult));
} catch (IllegalArgumentException | IllegalStateException e) {
LOGGER.error("Exception during resource PendingExpressions generation");
LOGGER.debug("Exception during resource PendingExpressions generation", e);
} finally {
MDC.remove(RESOURCE);
}
}
LOGGER.info("Successfully converted message");
LOGGER.debug("Successfully converted Message: {} , Message Control Id: {} to FHIR bundle resource with id {}", dataInput.getName(), dataInput.getId(), bundle.getId());
return bundle;
}
Aggregations