Search in sources :

Example 1 with DefinitionToAvroVisitor

use of com.cerner.bunsen.avro.converters.DefinitionToAvroVisitor in project bunsen by cerner.

the class AvroConverter method visitResource.

private static AvroConverter visitResource(FhirContext context, StructureDefinitions structureDefinitions, String resourceTypeUrl, List<String> containedResourceTypeUrls, Map<String, HapiConverter<Schema>> compositeConverters) {
    FhirVersionEnum fhirVersion = context.getVersion().getVersion();
    String basePackage;
    if (FhirVersionEnum.DSTU3.equals(fhirVersion)) {
        basePackage = "com.cerner.bunsen.stu3.avro";
    } else if (FhirVersionEnum.R4.equals(fhirVersion)) {
        basePackage = "com.cerner.bunsen.r4.avro";
    } else {
        throw new IllegalArgumentException("Unsupported FHIR version " + fhirVersion.toString());
    }
    DefinitionToAvroVisitor visitor = new DefinitionToAvroVisitor(structureDefinitions.conversionSupport(), basePackage, compositeConverters);
    HapiConverter<Schema> converter = structureDefinitions.transform(visitor, resourceTypeUrl, containedResourceTypeUrls);
    RuntimeResourceDefinition[] resources = new RuntimeResourceDefinition[1 + containedResourceTypeUrls.size()];
    resources[0] = context.getResourceDefinition(converter.getElementType());
    for (int i = 0; i < containedResourceTypeUrls.size(); i++) {
        // Retrieve the name of the contained resources from the Resource Container's schema
        Field containedField = converter.getDataType().getField("contained").schema().getTypes().get(// Get non-null element of the Union
        1).getElementType().getFields().get(i);
        // Shift array by 1, since the 0 index holds the parent definition
        resources[i + 1] = context.getResourceDefinition(containedField.name());
    }
    return new AvroConverter(converter, resources);
}
Also used : Field(org.apache.avro.Schema.Field) RuntimeResourceDefinition(ca.uhn.fhir.context.RuntimeResourceDefinition) Schema(org.apache.avro.Schema) FhirVersionEnum(ca.uhn.fhir.context.FhirVersionEnum) DefinitionToAvroVisitor(com.cerner.bunsen.avro.converters.DefinitionToAvroVisitor)

Aggregations

FhirVersionEnum (ca.uhn.fhir.context.FhirVersionEnum)1 RuntimeResourceDefinition (ca.uhn.fhir.context.RuntimeResourceDefinition)1 DefinitionToAvroVisitor (com.cerner.bunsen.avro.converters.DefinitionToAvroVisitor)1 Schema (org.apache.avro.Schema)1 Field (org.apache.avro.Schema.Field)1