Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple FHIR profile extensions #986

Merged
merged 8 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
import com.cerner.bunsen.definitions.HapiConverter;
import com.cerner.bunsen.definitions.HapiConverter.HapiObjectConverter;
import com.cerner.bunsen.definitions.StructureDefinitions;
import com.cerner.bunsen.exception.ProfileException;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.apache.avro.Schema;
import org.apache.avro.Schema.Field;
import org.apache.avro.generic.IndexedRecord;
import org.apache.commons.collections.CollectionUtils;
import org.hl7.fhir.instance.model.api.IBaseResource;

/** Converter to change HAPI objects into Avro structures and vice versa. */
Expand Down Expand Up @@ -133,6 +138,28 @@ public static AvroConverter forResource(FhirContext context, String resourceType
return forResource(context, resourceTypeUrl, Collections.emptyList());
}

/**
* Similar to {@link #forResource(FhirContext, String)} this method returns an Avro converter, but
* the returned Avro converter is a union of all the converters for the given resourceTypeUrls,
* the union is formed by merging all the fields in each converter.
*
* @param context the FHIR context
* @param resourceTypeUrls the list of resource type profile urls. The resourceTypeUrl can either
* be a relative URL for a base resource (e.g., "Condition" or "Observation"), or a URL
* identifying the structure definition for a given profile, such as
* "http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient".
* @return the merged Avro converter
*/
public static AvroConverter forResources(FhirContext context, List<String> resourceTypeUrls)
throws ProfileException {
List<AvroConverter> avroConverters = new ArrayList<>();
for (String resourceTypeUrl : resourceTypeUrls) {
AvroConverter avroConverter = forResource(context, resourceTypeUrl, Collections.emptyList());
avroConverters.add(avroConverter);
}
return mergeAvroConverters(avroConverters, context);
}

/**
* Returns an Avro converter for the given resource type. The resource type can either be a
* relative URL for a base resource (e.g. "Condition" or "Observation"), or a URL identifying the
Expand Down Expand Up @@ -196,4 +223,29 @@ public Schema getSchema() {
public String getResourceType() {
return hapiToAvroConverter.getElementType();
}

/**
* Merges all the given list of avroConverters to create a single AvroConverter which is a union
* of all the fields in the list of avroConverters
*/
private static AvroConverter mergeAvroConverters(
List<AvroConverter> avroConverters, FhirContext context) throws ProfileException {
Preconditions.checkArgument(
!CollectionUtils.isEmpty(avroConverters), "AvroConverter list cannot be empty for merging");
Iterator<AvroConverter> iterator = avroConverters.iterator();
AvroConverter mergedConverter = iterator.next();
while (iterator.hasNext()) {
mergedConverter = mergeAvroConverters(mergedConverter, iterator.next(), context);
}
return mergedConverter;
}

private static AvroConverter mergeAvroConverters(
AvroConverter left, AvroConverter right, FhirContext context) throws ProfileException {
HapiConverter<Schema> mergedConverter =
left.hapiToAvroConverter.merge(right.hapiToAvroConverter);
RuntimeResourceDefinition[] resources = new RuntimeResourceDefinition[1];
resources[0] = context.getResourceDefinition(mergedConverter.getElementType());
bashir2 marked this conversation as resolved.
Show resolved Hide resolved
return new AvroConverter(mergedConverter, resources);
bashir2 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading
Loading