use of ca.uhn.fhir.context.RuntimeResourceDefinition in project cqf-ruler by DBCG.
the class Reflections method getAccessor.
/**
* Gets the IAccessor for the given BaseType and child
*
* @param <BaseType> an IBase type
* @param theBaseTypeClass the class of a the IBase type
* @param theChildName the name of the child property of the
* BaseType to generate an accessor for
* @return an IAccessor for the given child and the BaseType
*/
public static <BaseType extends IBase> IAccessor getAccessor(final Class<? extends BaseType> theBaseTypeClass, String theChildName) {
checkNotNull(theBaseTypeClass);
checkNotNull(theChildName);
FhirContext fhirContext = FhirContext.forCached(FhirVersions.forClass(theBaseTypeClass));
if (theBaseTypeClass.isInstance(IBaseResource.class)) {
@SuppressWarnings("unchecked") RuntimeResourceDefinition resourceDefinition = fhirContext.getResourceDefinition((Class<? extends IBaseResource>) theBaseTypeClass);
return resourceDefinition.getChildByName(theChildName).getAccessor();
} else {
BaseRuntimeElementDefinition<?> elementDefinition = fhirContext.getElementDefinition(theBaseTypeClass);
return elementDefinition.getChildByName(theChildName).getAccessor();
}
}
use of ca.uhn.fhir.context.RuntimeResourceDefinition in project pathling by aehrc.
the class ManifestConverter method populateScope.
void populateScope(@Nonnull final PassportScope passportScope, @Nonnull final VisaManifest manifest) {
// Create a filter for the Patient resource.
final String patientIdCollection = manifest.getPatientIds().stream().map(id -> "'" + id + "'").collect(Collectors.joining(" combine "));
final String patientIdFilter = "identifier.where(system = '" + StringLiteralPath.escapeFhirPathString(patientIdSystem) + "').where(value in (" + patientIdCollection + "))" + ".empty().not()";
final Set<String> patientFilters = passportScope.get(ResourceType.PATIENT);
if (patientFilters == null) {
passportScope.put(ResourceType.PATIENT, new HashSet<>(List.of(patientIdFilter)));
} else {
patientFilters.add(patientIdFilter);
}
// See: https://www.hl7.org/fhir/r4/compartmentdefinition-patient.html
for (final ResourceType resourceType : ResourceType.values()) {
if (resourceType.equals(ResourceType.DOMAINRESOURCE) || resourceType.equals(ResourceType.RESOURCE) || resourceType.equals(ResourceType.NULL)) {
continue;
}
final RuntimeResourceDefinition definition = fhirContext.getResourceDefinition(resourceType.toCode());
final List<RuntimeSearchParam> searchParams = definition.getSearchParamsForCompartmentName("Patient");
for (final RuntimeSearchParam searchParam : searchParams) {
final String path = searchParam.getPath();
// Remove the leading "[resource type]." from the path.
final String pathTrimmed = path.replaceFirst("^" + resourceType.toCode() + "\\.", "");
// Paths that end with this resolve pattern are polymorphic references, and will need
// to be resolved using `ofType()` within our implementation.
final String resolvePattern = ".where(resolve() is Patient)";
final String filter;
if (pathTrimmed.endsWith(resolvePattern)) {
filter = pathTrimmed.replace(resolvePattern, ".resolve().ofType(Patient)." + patientIdFilter);
} else {
final Set<String> targets = searchParam.getTargets();
if (targets.size() > 0 && !targets.contains("Patient")) {
// Patient, we need to skip it altogether.
continue;
} else if (targets.size() == 1) {
// If the search parameter is monomorphic, we can resolve it without `ofType`.
filter = pathTrimmed + ".resolve()." + patientIdFilter;
} else {
// If the search parameter is polymorphic, we also need to resolve it to Patient. Note
// that polymorphic references with an "Any" type have zero targets.
filter = pathTrimmed + ".resolve().ofType(Patient)." + patientIdFilter;
}
}
// Add the filter to the map.
final Set<String> filters = passportScope.get(resourceType);
if (filters == null) {
passportScope.put(resourceType, new HashSet<>(List.of(filter)));
} else {
filters.add(filter);
}
}
}
}
use of ca.uhn.fhir.context.RuntimeResourceDefinition in project pathling by aehrc.
the class FindRecursiveTypesApp method findRecursiveTypes.
private void findRecursiveTypes() {
System.out.println(">>> Listing recursive types:");
for (final String resourceType : fhirContext.getResourceTypes()) {
final RuntimeResourceDefinition rd = fhirContext.getResourceDefinition(resourceType);
recursive.clear();
path.clear();
traverseDefinition(rd, "", 0);
if (!recursive.isEmpty()) {
System.out.println(resourceType);
recursive.forEach(s -> System.out.println(" " + s));
}
}
}
use of ca.uhn.fhir.context.RuntimeResourceDefinition in project pathling by aehrc.
the class FhirEncoders method of.
/**
* Returns an encoder for the given FHIR resource.
*
* @param type the type of the resource to encode.
* @param <T> the type of the resource to be encoded.
* @return an encoder for the resource.
*/
public final <T extends IBaseResource> ExpressionEncoder<T> of(final Class<T> type) {
final RuntimeResourceDefinition definition = context.getResourceDefinition(type);
final int key = type.getName().hashCode();
synchronized (encoderCache) {
// noinspection unchecked
return (ExpressionEncoder<T>) encoderCache.computeIfAbsent(key, k -> EncoderBuilder.of(definition, context, mappings, maxNestingLevel, JavaConverters.asScalaSet(openTypes).toSet(), enableExtensions));
}
}
use of ca.uhn.fhir.context.RuntimeResourceDefinition in project pathling by aehrc.
the class ResourcePath method build.
/**
* Build a new ResourcePath using the supplied {@link FhirContext} and {@link Database}.
*
* @param fhirContext the {@link FhirContext} to use for sourcing the resource definition
* @param database the {@link Database} to use for retrieving the Dataset
* @param resourceType the type of the resource
* @param expression the expression to use in the resulting path
* @param singular whether the resulting path should be flagged as a single item collection
* @param skipAliasing set to true to skip column aliasing
* @return A shiny new ResourcePath
*/
@Nonnull
public static ResourcePath build(@Nonnull final FhirContext fhirContext, @Nonnull final Database database, @Nonnull final ResourceType resourceType, @Nonnull final String expression, final boolean singular, final boolean skipAliasing) {
// Get the resource definition from HAPI.
final String resourceCode = resourceType.toCode();
final RuntimeResourceDefinition hapiDefinition = fhirContext.getResourceDefinition(resourceCode);
final ResourceDefinition definition = new ResourceDefinition(resourceType, hapiDefinition);
// Retrieve the dataset for the resource type using the supplied resource reader.
final Dataset<Row> dataset = database.read(resourceType);
final Column idColumn = col("id");
final Column finalIdColumn;
final Dataset<Row> finalDataset;
final Map<String, Column> elementsToColumns;
if (skipAliasing) {
// If aliasing is disabled, the dataset will contain columns with the original element names.
// This is used for contexts where we need the original column names for encoding (e.g.
// search).
finalDataset = dataset;
finalIdColumn = idColumn;
elementsToColumns = Stream.of(dataset.columns()).collect(Collectors.toMap(Function.identity(), functions::col, (a, b) -> null));
} else {
// If aliasing is enabled, all columns in the dataset will be aliased, and the original
// columns will be dropped. This is to avoid column name clashes when doing joins.
final DatasetWithColumnMap datasetWithColumnMap = aliasAllColumns(dataset);
finalDataset = datasetWithColumnMap.getDataset();
final Map<Column, Column> columnMap = datasetWithColumnMap.getColumnMap();
elementsToColumns = columnMap.keySet().stream().collect(Collectors.toMap(Column::toString, columnMap::get, (a, b) -> null));
finalIdColumn = elementsToColumns.get(idColumn.toString());
}
// We use the ID column as the value column for a ResourcePath.
return new ResourcePath(expression, finalDataset, finalIdColumn, Optional.empty(), finalIdColumn, singular, Optional.empty(), definition, elementsToColumns);
}
Aggregations