Search in sources :

Example 1 with StructureField

use of com.cerner.bunsen.definitions.StructureField in project bunsen by cerner.

the class DefinitionToAvroVisitor method visitParentExtension.

@Override
public HapiConverter<Schema> visitParentExtension(String elementName, String extensionUrl, List<StructureField<HapiConverter<Schema>>> children) {
    // Ignore extension fields that don't have declared content for now.
    if (children.isEmpty()) {
        return null;
    }
    String recordNamespace = DefinitionVisitorsUtil.namespaceFor(basePackage, extensionUrl);
    String localPart = extensionUrl.substring(extensionUrl.lastIndexOf('/') + 1);
    String[] parts = localPart.split("[-|_]");
    String recordName = Arrays.stream(parts).map(part -> part.substring(0, 1).toUpperCase() + part.substring(1)).collect(Collectors.joining());
    String fullName = recordNamespace + "." + recordName;
    HapiConverter<Schema> converter = visitedConverters.get(fullName);
    if (converter == null) {
        List<Field> fields = children.stream().map(entry -> new Field(entry.fieldName(), nullable(entry.result().getDataType()), "Doc here", JsonProperties.NULL_VALUE)).collect(Collectors.toList());
        Schema schema = Schema.createRecord(recordName, "Reference type.", recordNamespace, false, fields);
        converter = new CompositeToAvroConverter(null, children, schema, fhirSupport, extensionUrl);
        visitedConverters.put(fullName, converter);
    }
    return converter;
}
Also used : PrimitiveConverter(com.cerner.bunsen.definitions.PrimitiveConverter) FhirConversionSupport(com.cerner.bunsen.definitions.FhirConversionSupport) Arrays(java.util.Arrays) IBase(org.hl7.fhir.instance.model.api.IBase) HapiObjectConverter(com.cerner.bunsen.definitions.HapiConverter.HapiObjectConverter) MultiValueConverter(com.cerner.bunsen.definitions.HapiConverter.MultiValueConverter) StructureField(com.cerner.bunsen.definitions.StructureField) StringUtils(org.apache.commons.lang3.StringUtils) HapiChoiceConverter(com.cerner.bunsen.definitions.HapiChoiceConverter) ArrayList(java.util.ArrayList) GenericData(org.apache.avro.generic.GenericData) BigDecimal(java.math.BigDecimal) HapiCompositeConverter(com.cerner.bunsen.definitions.HapiCompositeConverter) LogicalTypes(org.apache.avro.LogicalTypes) Map(java.util.Map) Type(org.apache.avro.Schema.Type) JsonProperties(org.apache.avro.JsonProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) Conversion(org.apache.avro.Conversion) SpecificData(org.apache.avro.specific.SpecificData) Conversions(org.apache.avro.Conversions) Schema(org.apache.avro.Schema) Field(org.apache.avro.Schema.Field) BaseRuntimeElementDefinition(ca.uhn.fhir.context.BaseRuntimeElementDefinition) EnumConverter(com.cerner.bunsen.definitions.EnumConverter) ImmutableMap(com.google.common.collect.ImmutableMap) LeafExtensionConverter(com.cerner.bunsen.definitions.LeafExtensionConverter) Collectors(java.util.stream.Collectors) HapiContainedConverter(com.cerner.bunsen.definitions.HapiContainedConverter) HapiConverter(com.cerner.bunsen.definitions.HapiConverter) StringConverter(com.cerner.bunsen.definitions.StringConverter) List(java.util.List) DefinitionVisitorsUtil(com.cerner.bunsen.definitions.DefinitionVisitorsUtil) BaseRuntimeChildDefinition(ca.uhn.fhir.context.BaseRuntimeChildDefinition) HapiFieldSetter(com.cerner.bunsen.definitions.HapiConverter.HapiFieldSetter) IPrimitiveType(org.hl7.fhir.instance.model.api.IPrimitiveType) DefinitionVisitor(com.cerner.bunsen.definitions.DefinitionVisitor) StructureField(com.cerner.bunsen.definitions.StructureField) Field(org.apache.avro.Schema.Field) Schema(org.apache.avro.Schema)

Example 2 with StructureField

use of com.cerner.bunsen.definitions.StructureField in project bunsen by cerner.

the class DefinitionToAvroVisitor method visitContained.

@Override
public HapiConverter<Schema> visitContained(String elementPath, String elementTypeUrl, Map<String, StructureField<HapiConverter<Schema>>> contained) {
    String recordName = DefinitionVisitorsUtil.recordNameFor(elementPath);
    String recordNamespace = DefinitionVisitorsUtil.namespaceFor(basePackage, elementTypeUrl);
    List<Field> fields = contained.values().stream().map(containedEntry -> {
        String doc = "Field for FHIR property " + containedEntry.propertyName();
        return new Field(containedEntry.fieldName(), nullable(containedEntry.result().getDataType()), doc, JsonProperties.NULL_VALUE);
    }).collect(Collectors.toList());
    Schema containerType = Schema.createArray(Schema.createRecord(recordName, "Structure for FHIR type contained", recordNamespace, false, fields));
    return new HapiContainedToAvroConverter(contained, containerType);
}
Also used : PrimitiveConverter(com.cerner.bunsen.definitions.PrimitiveConverter) FhirConversionSupport(com.cerner.bunsen.definitions.FhirConversionSupport) Arrays(java.util.Arrays) IBase(org.hl7.fhir.instance.model.api.IBase) HapiObjectConverter(com.cerner.bunsen.definitions.HapiConverter.HapiObjectConverter) MultiValueConverter(com.cerner.bunsen.definitions.HapiConverter.MultiValueConverter) StructureField(com.cerner.bunsen.definitions.StructureField) StringUtils(org.apache.commons.lang3.StringUtils) HapiChoiceConverter(com.cerner.bunsen.definitions.HapiChoiceConverter) ArrayList(java.util.ArrayList) GenericData(org.apache.avro.generic.GenericData) BigDecimal(java.math.BigDecimal) HapiCompositeConverter(com.cerner.bunsen.definitions.HapiCompositeConverter) LogicalTypes(org.apache.avro.LogicalTypes) Map(java.util.Map) Type(org.apache.avro.Schema.Type) JsonProperties(org.apache.avro.JsonProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) Conversion(org.apache.avro.Conversion) SpecificData(org.apache.avro.specific.SpecificData) Conversions(org.apache.avro.Conversions) Schema(org.apache.avro.Schema) Field(org.apache.avro.Schema.Field) BaseRuntimeElementDefinition(ca.uhn.fhir.context.BaseRuntimeElementDefinition) EnumConverter(com.cerner.bunsen.definitions.EnumConverter) ImmutableMap(com.google.common.collect.ImmutableMap) LeafExtensionConverter(com.cerner.bunsen.definitions.LeafExtensionConverter) Collectors(java.util.stream.Collectors) HapiContainedConverter(com.cerner.bunsen.definitions.HapiContainedConverter) HapiConverter(com.cerner.bunsen.definitions.HapiConverter) StringConverter(com.cerner.bunsen.definitions.StringConverter) List(java.util.List) DefinitionVisitorsUtil(com.cerner.bunsen.definitions.DefinitionVisitorsUtil) BaseRuntimeChildDefinition(ca.uhn.fhir.context.BaseRuntimeChildDefinition) HapiFieldSetter(com.cerner.bunsen.definitions.HapiConverter.HapiFieldSetter) IPrimitiveType(org.hl7.fhir.instance.model.api.IPrimitiveType) DefinitionVisitor(com.cerner.bunsen.definitions.DefinitionVisitor) StructureField(com.cerner.bunsen.definitions.StructureField) Field(org.apache.avro.Schema.Field) Schema(org.apache.avro.Schema)

Example 3 with StructureField

use of com.cerner.bunsen.definitions.StructureField in project bunsen by cerner.

the class Stu3StructureDefinitions method extensionElementToFields.

private <T> List<StructureField<T>> extensionElementToFields(DefinitionVisitor<T> visitor, StructureDefinition rootDefinition, ElementDefinition element, List<ElementDefinition> definitions, Deque<QualifiedPath> stack) {
    // FIXME: extension is a type rather than an external structure....
    StructureDefinition definition = element.getTypeFirstRep().getProfile() != null ? (StructureDefinition) validationSupport.fetchStructureDefinition(context, element.getTypeFirstRep().getProfile()) : null;
    List<StructureField<T>> extensions;
    if (definition != null) {
        if (shouldTerminateRecursive(visitor, new QualifiedPath(definition.getUrl(), element.getPath()), stack)) {
            return Collections.emptyList();
        } else {
            List<ElementDefinition> extensionDefinitions = definition.getSnapshot().getElement();
            ElementDefinition extensionRoot = extensionDefinitions.get(0);
            extensions = visitExtensionDefinition(visitor, rootDefinition, element.getSliceName(), stack, definition.getUrl(), extensionDefinitions, extensionRoot);
        }
    } else {
        if (element.getSliceName() == null) {
            return Collections.emptyList();
        }
        extensions = visitExtensionDefinition(visitor, rootDefinition, element.getSliceName(), stack, element.getTypeFirstRep().getProfile(), definitions, element);
    }
    if (!element.getMax().equals("1") && extensions.size() > 0) {
        // the nested extension element has max: *
        return Collections.singletonList(StructureField.extension(extensions.get(0).fieldName(), extensions.get(0).extensionUrl(), extensions.get(0).isModifier(), visitor.visitMultiValued(extensions.get(0).fieldName(), extensions.get(0).result())));
    } else {
        return extensions;
    }
}
Also used : QualifiedPath(com.cerner.bunsen.definitions.QualifiedPath) StructureField(com.cerner.bunsen.definitions.StructureField) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) ElementDefinition(org.hl7.fhir.dstu3.model.ElementDefinition)

Example 4 with StructureField

use of com.cerner.bunsen.definitions.StructureField in project bunsen by cerner.

the class Stu3StructureDefinitions method transformRoot.

private <T> T transformRoot(DefinitionVisitor<T> visitor, StructureDefinition definition, List<StructureDefinition> containedDefinitions, Deque<QualifiedPath> stack) {
    ElementDefinition definitionRootElement = definition.getSnapshot().getElementFirstRep();
    List<ElementDefinition> definitions = definition.getSnapshot().getElement();
    ElementDefinition root = definitions.get(0);
    stack.push(new QualifiedPath(definition.getUrl(), definitionRootElement.getPath()));
    List<StructureField<T>> childElements = transformChildren(visitor, definition, definitions, stack, root);
    // If there are contained definitions, create a Resource Container StructureField
    if (containedDefinitions.size() > 0) {
        StructureField<T> containedElement = transformContained(visitor, definition, containedDefinitions, stack, root);
        // Replace default StructureField with constructed Resource Container StructureField
        childElements.set(5, containedElement);
    }
    stack.pop();
    String rootName = elementName(root);
    return visitor.visitComposite(rootName, rootName, rootName, definition.getUrl(), childElements);
}
Also used : QualifiedPath(com.cerner.bunsen.definitions.QualifiedPath) StructureField(com.cerner.bunsen.definitions.StructureField) ElementDefinition(org.hl7.fhir.dstu3.model.ElementDefinition)

Example 5 with StructureField

use of com.cerner.bunsen.definitions.StructureField in project bunsen by cerner.

the class DefinitionToSparkVisitor method visitContained.

@Override
public HapiConverter visitContained(String elementPath, String elementTypeUrl, Map<String, StructureField<HapiConverter<DataType>>> contained) {
    StructField[] fields = contained.values().stream().map(containedEntry -> new StructField(containedEntry.fieldName(), containedEntry.result().getDataType(), true, Metadata.empty())).toArray(StructField[]::new);
    ArrayType container = new ArrayType(new StructType(fields), true);
    return new HapiContainedToSparkConverter(contained, container);
}
Also used : DataType(org.apache.spark.sql.types.DataType) PrimitiveConverter(com.cerner.bunsen.definitions.PrimitiveConverter) FhirConversionSupport(com.cerner.bunsen.definitions.FhirConversionSupport) Arrays(java.util.Arrays) IBase(org.hl7.fhir.instance.model.api.IBase) HapiObjectConverter(com.cerner.bunsen.definitions.HapiConverter.HapiObjectConverter) HashMap(java.util.HashMap) MultiValueConverter(com.cerner.bunsen.definitions.HapiConverter.MultiValueConverter) StructureField(com.cerner.bunsen.definitions.StructureField) StringUtils(org.apache.commons.lang3.StringUtils) HapiChoiceConverter(com.cerner.bunsen.definitions.HapiChoiceConverter) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) ArrayType(org.apache.spark.sql.types.ArrayType) HapiCompositeConverter(com.cerner.bunsen.definitions.HapiCompositeConverter) Map(java.util.Map) DataTypes(org.apache.spark.sql.types.DataTypes) Metadata(org.apache.spark.sql.types.Metadata) StructField(org.apache.spark.sql.types.StructField) StructType(org.apache.spark.sql.types.StructType) WrappedArray(scala.collection.mutable.WrappedArray) JavaConversions(scala.collection.JavaConversions) BaseRuntimeElementDefinition(ca.uhn.fhir.context.BaseRuntimeElementDefinition) EnumConverter(com.cerner.bunsen.definitions.EnumConverter) ImmutableMap(com.google.common.collect.ImmutableMap) RowFactory(org.apache.spark.sql.RowFactory) LeafExtensionConverter(com.cerner.bunsen.definitions.LeafExtensionConverter) Row(org.apache.spark.sql.Row) Collectors(java.util.stream.Collectors) HapiContainedConverter(com.cerner.bunsen.definitions.HapiContainedConverter) HapiConverter(com.cerner.bunsen.definitions.HapiConverter) StringConverter(com.cerner.bunsen.definitions.StringConverter) List(java.util.List) DefinitionVisitorsUtil(com.cerner.bunsen.definitions.DefinitionVisitorsUtil) GenericRowWithSchema(org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema) BaseRuntimeChildDefinition(ca.uhn.fhir.context.BaseRuntimeChildDefinition) HapiFieldSetter(com.cerner.bunsen.definitions.HapiConverter.HapiFieldSetter) IPrimitiveType(org.hl7.fhir.instance.model.api.IPrimitiveType) DefinitionVisitor(com.cerner.bunsen.definitions.DefinitionVisitor) ArrayType(org.apache.spark.sql.types.ArrayType) StructField(org.apache.spark.sql.types.StructField) StructType(org.apache.spark.sql.types.StructType)

Aggregations

StructureField (com.cerner.bunsen.definitions.StructureField)12 ArrayList (java.util.ArrayList)8 List (java.util.List)7 DefinitionVisitor (com.cerner.bunsen.definitions.DefinitionVisitor)6 FhirConversionSupport (com.cerner.bunsen.definitions.FhirConversionSupport)6 QualifiedPath (com.cerner.bunsen.definitions.QualifiedPath)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)6 HapiConverter (com.cerner.bunsen.definitions.HapiConverter)5 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)5 BaseRuntimeChildDefinition (ca.uhn.fhir.context.BaseRuntimeChildDefinition)4 BaseRuntimeElementDefinition (ca.uhn.fhir.context.BaseRuntimeElementDefinition)4 DefinitionVisitorsUtil (com.cerner.bunsen.definitions.DefinitionVisitorsUtil)4 EnumConverter (com.cerner.bunsen.definitions.EnumConverter)4 HapiChoiceConverter (com.cerner.bunsen.definitions.HapiChoiceConverter)4 HapiCompositeConverter (com.cerner.bunsen.definitions.HapiCompositeConverter)4 HapiContainedConverter (com.cerner.bunsen.definitions.HapiContainedConverter)4 HapiFieldSetter (com.cerner.bunsen.definitions.HapiConverter.HapiFieldSetter)4 HapiObjectConverter (com.cerner.bunsen.definitions.HapiConverter.HapiObjectConverter)4