Search in sources :

Example 1 with InputDataExtractor

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;
}
Also used : SegmentGroup(io.github.linuxforhealth.hl7.message.util.SegmentGroup) ResourceEvaluationResult(io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult) Constants(io.github.linuxforhealth.core.Constants) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) SimpleEvaluationResult(io.github.linuxforhealth.core.expression.SimpleEvaluationResult) FHIRResourceTemplate(io.github.linuxforhealth.api.FHIRResourceTemplate) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ExpressionUtility(io.github.linuxforhealth.hl7.util.ExpressionUtility) Map(java.util.Map) InputDataExtractor(io.github.linuxforhealth.api.InputDataExtractor) ResourceResult(io.github.linuxforhealth.core.resource.ResourceResult) Meta(org.hl7.fhir.r4.model.Meta) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult) ObjectMapperUtil(io.github.linuxforhealth.core.ObjectMapperUtil) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) ResourceValue(io.github.linuxforhealth.api.ResourceValue) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Structure(ca.uhn.hl7v2.model.Structure) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UUID(java.util.UUID) LocalDateTime(org.joda.time.LocalDateTime) Collectors(java.util.stream.Collectors) SimpleResourceValue(io.github.linuxforhealth.core.resource.SimpleResourceValue) SegmentExtractorUtil(io.github.linuxforhealth.hl7.message.util.SegmentExtractorUtil) List(java.util.List) MDC(org.slf4j.MDC) EvaluationResultFactory(io.github.linuxforhealth.core.expression.EvaluationResultFactory) Entry(java.util.Map.Entry) Bundle(org.hl7.fhir.r4.model.Bundle) Preconditions(com.google.common.base.Preconditions) ResourceModel(io.github.linuxforhealth.api.ResourceModel) BundleType(org.hl7.fhir.r4.model.Bundle.BundleType) MessageEngine(io.github.linuxforhealth.api.MessageEngine) RequiredConstraintFailureException(io.github.linuxforhealth.core.exception.RequiredConstraintFailureException) FHIRContext(io.github.linuxforhealth.fhir.FHIRContext) FHIRResourceMapper(io.github.linuxforhealth.fhir.FHIRResourceMapper) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleResourceValue(io.github.linuxforhealth.core.resource.SimpleResourceValue) ResourceValue(io.github.linuxforhealth.api.ResourceValue) SimpleResourceValue(io.github.linuxforhealth.core.resource.SimpleResourceValue) ResourceModel(io.github.linuxforhealth.api.ResourceModel) FHIRResourceTemplate(io.github.linuxforhealth.api.FHIRResourceTemplate) Bundle(org.hl7.fhir.r4.model.Bundle) ResourceEvaluationResult(io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult) SimpleEvaluationResult(io.github.linuxforhealth.core.expression.SimpleEvaluationResult) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult) ResourceResult(io.github.linuxforhealth.core.resource.ResourceResult) ResourceEvaluationResult(io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

Structure (ca.uhn.hl7v2.model.Structure)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 EvaluationResult (io.github.linuxforhealth.api.EvaluationResult)1 FHIRResourceTemplate (io.github.linuxforhealth.api.FHIRResourceTemplate)1 InputDataExtractor (io.github.linuxforhealth.api.InputDataExtractor)1 MessageEngine (io.github.linuxforhealth.api.MessageEngine)1 ResourceModel (io.github.linuxforhealth.api.ResourceModel)1 ResourceValue (io.github.linuxforhealth.api.ResourceValue)1 Constants (io.github.linuxforhealth.core.Constants)1 ObjectMapperUtil (io.github.linuxforhealth.core.ObjectMapperUtil)1 RequiredConstraintFailureException (io.github.linuxforhealth.core.exception.RequiredConstraintFailureException)1 EvaluationResultFactory (io.github.linuxforhealth.core.expression.EvaluationResultFactory)1 SimpleEvaluationResult (io.github.linuxforhealth.core.expression.SimpleEvaluationResult)1 ResourceResult (io.github.linuxforhealth.core.resource.ResourceResult)1 SimpleResourceValue (io.github.linuxforhealth.core.resource.SimpleResourceValue)1 FHIRContext (io.github.linuxforhealth.fhir.FHIRContext)1