Search in sources :

Example 1 with IBaseHasModifierExtensions

use of org.hl7.fhir.instance.model.api.IBaseHasModifierExtensions in project bunsen by cerner.

the class HapiCompositeConverter method fromHapi.

@Override
public Object fromHapi(Object input) {
    IBase composite = (IBase) input;
    Object[] values = new Object[children.size()];
    int valueIndex = 0;
    Iterator<StructureField<HapiConverter<T>>> schemaIterator = children.iterator();
    if (composite instanceof IAnyResource) {
        // Id element
        StructureField<HapiConverter<T>> schemaEntry = schemaIterator.next();
        values[0] = schemaEntry.result().fromHapi(((IAnyResource) composite).getIdElement());
        valueIndex++;
        // Meta element
        schemaEntry = schemaIterator.next();
        values[valueIndex] = schemaEntry.result().fromHapi(((IAnyResource) composite).getMeta());
        valueIndex++;
    }
    Map<String, List> properties = fhirSupport.compositeValues(composite);
    // Co-iterate with an index so we place the correct values into the corresponding locations.
    for (; valueIndex < children.size(); ++valueIndex) {
        StructureField<HapiConverter<T>> schemaEntry = schemaIterator.next();
        String propertyName = schemaEntry.propertyName();
        // Append the [x] suffix for choice properties.
        if (schemaEntry.isChoice()) {
            propertyName = propertyName + "[x]";
        }
        HapiConverter<T> converter = schemaEntry.result();
        List propertyValues = properties.get(propertyName);
        if (propertyValues != null && !propertyValues.isEmpty()) {
            if (isMultiValued(converter.getDataType())) {
                values[valueIndex] = schemaEntry.result().fromHapi(propertyValues);
            } else {
                values[valueIndex] = schemaEntry.result().fromHapi(propertyValues.get(0));
            }
        } else if (converter.extensionUrl() != null) {
            // No corresponding property for the name, so see if it is an Extension or ModifierExtention
            List<? extends IBaseExtension> extensions = schemaEntry.isModifier() ? ((IBaseHasModifierExtensions) composite).getModifierExtension() : ((IBaseHasExtensions) composite).getExtension();
            for (IBaseExtension extension : extensions) {
                if (extension.getUrl().equals(converter.extensionUrl())) {
                    values[valueIndex] = schemaEntry.result().fromHapi(extension);
                }
            }
        } else if (converter instanceof MultiValueConverter && ((MultiValueConverter) converter).getElementConverter().extensionUrl() != null) {
            final String extensionUrl = ((MultiValueConverter) converter).getElementConverter().extensionUrl();
            List<? extends IBaseExtension> extensions = schemaEntry.isModifier() ? ((IBaseHasModifierExtensions) composite).getModifierExtension() : ((IBaseHasExtensions) composite).getExtension();
            final List<? extends IBaseExtension> extensionList = extensions.stream().filter(extension -> extension.getUrl().equals(extensionUrl)).collect(Collectors.toList());
            if (extensionList.size() > 0) {
                values[valueIndex] = schemaEntry.result().fromHapi(extensionList);
            }
        }
    }
    return createComposite(values);
}
Also used : IBaseHasExtensions(org.hl7.fhir.instance.model.api.IBaseHasExtensions) IBaseHasModifierExtensions(org.hl7.fhir.instance.model.api.IBaseHasModifierExtensions) Iterator(java.util.Iterator) BaseRuntimeElementDefinition(ca.uhn.fhir.context.BaseRuntimeElementDefinition) IBase(org.hl7.fhir.instance.model.api.IBase) RuntimeElemContainedResourceList(ca.uhn.fhir.context.RuntimeElemContainedResourceList) IBaseExtension(org.hl7.fhir.instance.model.api.IBaseExtension) Collectors(java.util.stream.Collectors) IAnyResource(org.hl7.fhir.instance.model.api.IAnyResource) List(java.util.List) BaseRuntimeChildDefinition(ca.uhn.fhir.context.BaseRuntimeChildDefinition) Map(java.util.Map) BaseRuntimeElementCompositeDefinition(ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition) IBaseHasExtensions(org.hl7.fhir.instance.model.api.IBaseHasExtensions) IAnyResource(org.hl7.fhir.instance.model.api.IAnyResource) IBaseHasModifierExtensions(org.hl7.fhir.instance.model.api.IBaseHasModifierExtensions) RuntimeElemContainedResourceList(ca.uhn.fhir.context.RuntimeElemContainedResourceList) List(java.util.List) IBase(org.hl7.fhir.instance.model.api.IBase) IBaseExtension(org.hl7.fhir.instance.model.api.IBaseExtension)

Aggregations

BaseRuntimeChildDefinition (ca.uhn.fhir.context.BaseRuntimeChildDefinition)1 BaseRuntimeElementCompositeDefinition (ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition)1 BaseRuntimeElementDefinition (ca.uhn.fhir.context.BaseRuntimeElementDefinition)1 RuntimeElemContainedResourceList (ca.uhn.fhir.context.RuntimeElemContainedResourceList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 IAnyResource (org.hl7.fhir.instance.model.api.IAnyResource)1 IBase (org.hl7.fhir.instance.model.api.IBase)1 IBaseExtension (org.hl7.fhir.instance.model.api.IBaseExtension)1 IBaseHasExtensions (org.hl7.fhir.instance.model.api.IBaseHasExtensions)1 IBaseHasModifierExtensions (org.hl7.fhir.instance.model.api.IBaseHasModifierExtensions)1