Search in sources :

Example 1 with SourceElementComponent

use of org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent in project bunsen by cerner.

the class ConceptMaps method expandMappingsIterator.

private static Iterator<Mapping> expandMappingsIterator(ConceptMap map) {
    List<Mapping> mappings = new ArrayList<>();
    for (ConceptMapGroupComponent group : map.getGroup()) {
        for (SourceElementComponent element : group.getElement()) {
            for (TargetElementComponent target : element.getTarget()) {
                Mapping mapping = new Mapping();
                mapping.setConceptMapUri(map.getUrl());
                mapping.setConceptMapVersion(map.getVersion());
                try {
                    String sourceValue = map.getSource() instanceof UriType ? map.getSourceUriType().getValue() : map.getSourceReference().getReference();
                    mapping.setSourceValueSet(sourceValue);
                    String targetValue = map.getTarget() instanceof UriType ? map.getTargetUriType().getValue() : map.getTargetReference().getReference();
                    mapping.setTargetValueSet(targetValue);
                } catch (FHIRException fhirException) {
                    // an exception.
                    throw new RuntimeException(fhirException);
                }
                mapping.setSourceSystem(group.getSource());
                mapping.setSourceValue(element.getCode());
                mapping.setTargetSystem(group.getTarget());
                mapping.setTargetValue(target.getCode());
                if (target.getEquivalence() != null) {
                    mapping.setEquivalence(target.getEquivalence().toCode());
                }
                mappings.add(mapping);
            }
        }
    }
    return mappings.iterator();
}
Also used : ArrayList(java.util.ArrayList) TargetElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.TargetElementComponent) FHIRException(org.hl7.fhir.exceptions.FHIRException) ConceptMapGroupComponent(org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent) UriType(org.hl7.fhir.dstu3.model.UriType)

Example 2 with SourceElementComponent

use of org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent in project bunsen by cerner.

the class ConceptMaps method addToConceptMap.

/**
 * Adds the given mappings to the concept map.
 *
 * @param map the concept map
 * @param mappings the mappings to add
 */
private static void addToConceptMap(ConceptMap map, Dataset<Mapping> mappings) {
    // Sort the items so they are grouped together optimally, and so
    // we consistently produce the same ordering, therefore making
    // inspection and comparison of the concept maps easier.
    List<Mapping> sortedMappings = mappings.sort("sourceSystem", "targetSystem", "sourceValue", "targetValue").collectAsList();
    ConceptMapGroupComponent currentGroup = null;
    SourceElementComponent element = null;
    // Workaround for the decoder producing an immutable array by
    // replacing it with a mutable one.
    map.setGroup(new ArrayList<>(map.getGroup()));
    for (Mapping mapping : sortedMappings) {
        // Add a new group if we don't match the previous one.
        if (currentGroup == null || !mapping.getSourceSystem().equals(currentGroup.getSource()) || !mapping.getTargetSystem().equals(currentGroup.getTarget())) {
            currentGroup = null;
            // Find a matching group.
            for (ConceptMapGroupComponent candidate : map.getGroup()) {
                if (mapping.getSourceSystem().equals(candidate.getSource()) && mapping.getTargetSystem().equals(candidate.getTarget())) {
                    currentGroup = candidate;
                    // Workaround for the decoder producing an immutable array by
                    // replacing it with a mutable one.
                    currentGroup.setElement(new ArrayList<>(currentGroup.getElement()));
                    break;
                }
            }
            // No matching group found, so add it.
            if (currentGroup == null) {
                currentGroup = map.addGroup();
                currentGroup.setSource(mapping.getSourceSystem());
                currentGroup.setTarget(mapping.getTargetSystem());
                // Ensure a new element is created for the newly created group.
                element = null;
            }
        }
        // so add one if it does not match the previous.
        if (element == null || !mapping.getSourceValue().equals(element.getCode())) {
            element = currentGroup.addElement();
            element.setCode(mapping.getSourceValue());
        }
        element.addTarget().setCode(mapping.getTargetValue());
    }
}
Also used : ConceptMapGroupComponent(org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent)

Aggregations

ConceptMapGroupComponent (org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent)2 SourceElementComponent (org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent)2 ArrayList (java.util.ArrayList)1 TargetElementComponent (org.hl7.fhir.dstu3.model.ConceptMap.TargetElementComponent)1 UriType (org.hl7.fhir.dstu3.model.UriType)1 FHIRException (org.hl7.fhir.exceptions.FHIRException)1