Skip to content

Commit

Permalink
Addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chandrashekar-s committed May 16, 2024
1 parent 4a7bf43 commit 0c0f1bd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.CanonicalType;
import org.hl7.fhir.r4.model.ElementDefinition;
Expand Down Expand Up @@ -38,12 +39,16 @@ public FhirConversionSupport conversionSupport() {
}

@Override
protected IStructureDefinition getStructureDefinition(String resourceUrl) {
@Nonnull
protected IStructureDefinition getStructureDefinition(String resourceUrl)
throws IllegalArgumentException {
IBaseResource baseResource =
context.getValidationSupport().fetchStructureDefinition(resourceUrl);
return baseResource == null
? null
: new StructureDefinitionWrapper((StructureDefinition) baseResource);
if (baseResource == null) {
throw new IllegalArgumentException(
String.format("Unable to find definition for %s", resourceUrl));
}
return new StructureDefinitionWrapper((StructureDefinition) baseResource);
}

// FHIR version specific interface implementations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.cerner.bunsen.definitions.StructureDefinitions;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.hl7.fhir.dstu3.model.ElementDefinition;
import org.hl7.fhir.dstu3.model.StructureDefinition;
import org.hl7.fhir.instance.model.api.IBaseResource;
Expand All @@ -32,12 +33,16 @@ public FhirConversionSupport conversionSupport() {
}

@Override
protected IStructureDefinition getStructureDefinition(String resourceUrl) {
@Nonnull
protected IStructureDefinition getStructureDefinition(String resourceUrl)
throws IllegalArgumentException {
IBaseResource baseResource =
context.getValidationSupport().fetchStructureDefinition(resourceUrl);
return baseResource == null
? null
: new StructureDefinitionWrapper((StructureDefinition) baseResource);
if (baseResource == null) {
throw new IllegalArgumentException(
String.format("Unable to find definition for %s", resourceUrl));
}
return new StructureDefinitionWrapper((StructureDefinition) baseResource);
}

// FHIR version specific interface implementations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/** Abstract base class to visit FHIR structure definitions. */
Expand Down Expand Up @@ -158,10 +159,6 @@ private <T> List<StructureField<T>> extensionElementToFields(
String profileUrl = element.getFirstTypeProfile();
if (profileUrl != null) {
definition = getStructureDefinition(profileUrl);
if (definition == null) {
throw new IllegalArgumentException(
String.format("Unable to find definition for %s", profileUrl));
}
}

List<StructureField<T>> extensions;
Expand Down Expand Up @@ -318,10 +315,6 @@ private <T> List<StructureField<T>> elementToFields(
choiceTypes.put(typeCode, child);
} else {
IStructureDefinition structureDefinition = getStructureDefinition(typeCode);
if (structureDefinition == null) {
throw new IllegalArgumentException(
String.format("Unable to find definition for %s", typeCode));
}

// TODO document why we are resetting the stack here; it is not clear
// why this cannot lead to infinite recursion for choice types. If
Expand Down Expand Up @@ -502,24 +495,9 @@ public <T> T transform(

IStructureDefinition definition = getStructureDefinition(resourceTypeUrl);

if (definition == null) {
throw new IllegalArgumentException("Unable to find definition for " + resourceTypeUrl);
}

List<IStructureDefinition> containedDefinitions =
containedResourceTypeUrls.stream()
.map(
containedResourceTypeUrl -> {
IStructureDefinition containedDefinition =
getStructureDefinition(containedResourceTypeUrl);

if (containedDefinition == null) {
throw new IllegalArgumentException(
"Unable to find definition for " + containedResourceTypeUrl);
}

return containedDefinition;
})
.map(containedResourceTypeUrl -> getStructureDefinition(containedResourceTypeUrl))
.collect(Collectors.toList());

return transformRoot(visitor, definition, containedDefinitions);
Expand Down Expand Up @@ -553,15 +531,7 @@ private <T> T transform(
List<String> referenceProfiles = parentElement.getReferenceTargetProfiles();
List<String> referenceTypes =
referenceProfiles.stream()
.map(
profile -> {
IStructureDefinition structureDefinition = getStructureDefinition(profile);
if (structureDefinition == null) {
throw new IllegalArgumentException(
String.format("Unable to find definition for %s", profile));
}
return structureDefinition.getType();
})
.map(profile -> getStructureDefinition(profile).getType())
.sorted()
// Retrieve only the unique reference types
.distinct()
Expand Down Expand Up @@ -620,12 +590,16 @@ private <T> T transformRoot(
}

/**
* Returns the structure definition interface corresponding to the given URL.
* Returns the structure definition interface corresponding to the given resourceUrl.
*
* @param resourceUrl it can be a resource type like `Patient` or a profile URL.
* @return the {@link IStructureDefinition} corresponding to the `resourceUrl`.
* @return the {@link IStructureDefinition} corresponding to the `resourceUrl`
* @throws IllegalArgumentException if the structure definition cannot be found for the given
* resourceUrl.
*/
protected abstract IStructureDefinition getStructureDefinition(String resourceUrl);
@Nonnull
protected abstract IStructureDefinition getStructureDefinition(String resourceUrl)
throws IllegalArgumentException;

/**
* Returns the structure definition interface corresponding to the given element.
Expand Down

0 comments on commit 0c0f1bd

Please sign in to comment.