Class FHIRUtil
- java.lang.Object
-
- org.linuxforhealth.fhir.model.util.FHIRUtil
-
public class FHIRUtil extends java.lang.Object
Utility methods for working with the FHIR object model.
-
-
Field Summary
Fields Modifier and Type Field Description static OperationOutcome
ALL_OK
static Extension
DATA_ABSENT_REASON_UNKNOWN
static java.util.regex.Pattern
REFERENCE_PATTERN
static String
STRING_DATA_ABSENT_REASON_UNKNOWN
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Resource>
TaddTag(T resource, Coding tag)
Return a copy of resourceresource
with tagtag
static boolean
anyFailureInIssues(java.util.List<OperationOutcome.Issue> issues)
Determine if any of the issues in the list of issues are failure issuesstatic java.lang.String
buildBundleReference(Reference ref, java.lang.String fullUrlString)
Build the referencereference
based on thefullUrlString
value.static java.net.URI
buildLocationURI(java.lang.String type, java.lang.String id, int version)
Builds a relative "Location" header value for the specified resource type/id/version.static java.net.URI
buildLocationURI(java.lang.String type, Resource resource)
Builds a relative "Location" header value for the specified resource.static OperationOutcome
buildOperationOutcome(java.lang.Exception exception, boolean includeCausedByClauses)
Build an OperationOutcome for the specified exception with a single issue of type 'exception' and severity 'fatal'.static OperationOutcome
buildOperationOutcome(java.lang.Exception exception, IssueType issueType, IssueSeverity severity, boolean includeCausedByClauses)
Build an OperationOutcome for the specified exception.static OperationOutcome
buildOperationOutcome(java.lang.String message, IssueType issueType, IssueSeverity severity)
Build an OperationOutcome for the specified exception.static OperationOutcome
buildOperationOutcome(java.util.Collection<OperationOutcome.Issue> issues)
Build an OperationOutcome that contains the specified list of operation outcome issues.static OperationOutcome
buildOperationOutcome(FHIRException e, boolean includeCausedByClauses)
Build an OperationOutcome with an id from exception e and a single issue of type 'exception' and severity 'fatal'.static OperationOutcome
buildOperationOutcome(FHIROperationException e, boolean includeCausedByClauses)
Build an OperationOutcome with an id and a list of issues from exception e.static OperationOutcome.Issue
buildOperationOutcomeIssue(java.lang.String msg, IssueType code)
static OperationOutcome.Issue
buildOperationOutcomeIssue(IssueSeverity severity, IssueType code, java.lang.String details)
static OperationOutcome.Issue
buildOperationOutcomeIssue(IssueSeverity severity, IssueType code, java.lang.String details, java.lang.String expression)
static Bundle
createStandaloneBundle(BundleType bundleType, java.util.Map<java.lang.String,Resource> resources)
Create a self-contained bundle from the passed map of resources, replacing Resource.id values and references with a generated UUID.static java.lang.String
getExtensionStringValue(Resource resource, java.lang.String extensionUrl)
Returns the string value of the specified extension element within the specified resource.static java.lang.String
getExtensionStringValue(Element element, java.lang.String extensionUrl)
Returns the string value of the specified extension element within the specified element.static boolean
hasTag(Resource resource, Coding tag)
static void
init()
Loads the class in the classloader in order to initialize static members.static boolean
isFailure(IssueSeverity severity)
Determine if the given severity should be treated as a failurestatic <T extends Resource>
TresolveBundleReference(java.lang.Class<T> resourceType, Bundle bundle, Bundle.Entry sourceEntry, Reference ref)
Resolve the referenceref
to an entry withinbundle
and return the corresponding resourcestatic Bundle.Entry
resolveBundleReference(Bundle bundle, Bundle.Entry sourceEntry, Reference ref)
Resolve the referenceref
to an entry withinbundle
static Resource
resolveContainedReference(DomainResource resource, Reference ref)
Resolve the referenceref
to a bundle entry and return the corresponding resource containerstatic Resource
resolveReference(Reference ref, DomainResource resource, Bundle bundle, Bundle.Entry entry)
Resolve referenceref
to a bundle entry or a resource contained withinresource
and return the corresponding resource container.static jakarta.json.JsonObjectBuilder
toJsonObjectBuilder(jakarta.json.JsonObject jsonObject)
static java.lang.String
toString(Visitable visitable)
Converts a Visitable (Element or Resource) instance to a string using a FHIRGenerator.
-
-
-
Field Detail
-
REFERENCE_PATTERN
public static final java.util.regex.Pattern REFERENCE_PATTERN
-
DATA_ABSENT_REASON_UNKNOWN
public static final Extension DATA_ABSENT_REASON_UNKNOWN
-
STRING_DATA_ABSENT_REASON_UNKNOWN
public static final String STRING_DATA_ABSENT_REASON_UNKNOWN
-
ALL_OK
public static final OperationOutcome ALL_OK
-
-
Method Detail
-
init
public static void init()
Loads the class in the classloader in order to initialize static members. Call this before using the class in order to avoid a slight performance hit on first use.
-
toString
public static java.lang.String toString(Visitable visitable)
Converts a Visitable (Element or Resource) instance to a string using a FHIRGenerator.The toString format (JSON or XML) can be specified through
FHIRModelConfig.setToStringFormat(Format)
.- Parameters:
visitable
- the Element or Resource instance to be converted- Returns:
- the String version of the element or resource
-
toJsonObjectBuilder
public static jakarta.json.JsonObjectBuilder toJsonObjectBuilder(jakarta.json.JsonObject jsonObject)
-
buildOperationOutcomeIssue
public static OperationOutcome.Issue buildOperationOutcomeIssue(java.lang.String msg, IssueType code)
-
buildOperationOutcomeIssue
public static OperationOutcome.Issue buildOperationOutcomeIssue(IssueSeverity severity, IssueType code, java.lang.String details)
-
buildOperationOutcomeIssue
public static OperationOutcome.Issue buildOperationOutcomeIssue(IssueSeverity severity, IssueType code, java.lang.String details, java.lang.String expression)
-
buildOperationOutcome
public static OperationOutcome buildOperationOutcome(java.util.Collection<OperationOutcome.Issue> issues)
Build an OperationOutcome that contains the specified list of operation outcome issues.
-
buildOperationOutcome
public static OperationOutcome buildOperationOutcome(FHIROperationException e, boolean includeCausedByClauses)
Build an OperationOutcome with an id and a list of issues from exception e.
-
buildOperationOutcome
public static OperationOutcome buildOperationOutcome(FHIRException e, boolean includeCausedByClauses)
Build an OperationOutcome with an id from exception e and a single issue of type 'exception' and severity 'fatal'.
-
buildOperationOutcome
public static OperationOutcome buildOperationOutcome(java.lang.Exception exception, boolean includeCausedByClauses)
Build an OperationOutcome for the specified exception with a single issue of type 'exception' and severity 'fatal'.
-
buildOperationOutcome
public static OperationOutcome buildOperationOutcome(java.lang.Exception exception, IssueType issueType, IssueSeverity severity, boolean includeCausedByClauses)
Build an OperationOutcome for the specified exception.
-
buildOperationOutcome
public static OperationOutcome buildOperationOutcome(java.lang.String message, IssueType issueType, IssueSeverity severity)
Build an OperationOutcome for the specified exception.- Parameters:
issueType
- defaults to IssueTypeList.EXCEPTIONseverity
- defaults to IssueSeverityList.FATAL
-
buildLocationURI
public static java.net.URI buildLocationURI(java.lang.String type, Resource resource)
Builds a relative "Location" header value for the specified resource. This will be a string of the form"[resource-type]/[id]/_history/[version]"
. Note that the server will turn this into an absolute URL prior to returning it to the client.- Parameters:
type
- the resource type nameresource
- the resource for which the location header value should be returned
-
buildLocationURI
public static java.net.URI buildLocationURI(java.lang.String type, java.lang.String id, int version)
Builds a relative "Location" header value for the specified resource type/id/version. This will be a string of the form"[resource-type]/[id]/_history/[version]"
. Note that the server will turn this into an absolute URL prior to returning it to the client.- Parameters:
type
- the resource type nameid
- the resource logical id valueversion
- the resource version- Returns:
-
resolveReference
public static Resource resolveReference(Reference ref, DomainResource resource, Bundle bundle, Bundle.Entry entry) throws java.lang.Exception
Resolve referenceref
to a bundle entry or a resource contained withinresource
and return the corresponding resource container. Resolvingref
to a resource that exists outside of the bundle is not yet supported, but this support may be added in the future.- Throws:
java.lang.Exception
- if the resource could not be found, the reference has no value, or the value does not match the expected format for a reference
-
resolveContainedReference
public static Resource resolveContainedReference(DomainResource resource, Reference ref) throws java.lang.Exception
Resolve the referenceref
to a bundle entry and return the corresponding resource container- Throws:
java.lang.Exception
- if the resource could not be found, the reference has no value, or the value does not match the expected format for a contained reference
-
resolveBundleReference
public static <T extends Resource> T resolveBundleReference(java.lang.Class<T> resourceType, Bundle bundle, Bundle.Entry sourceEntry, Reference ref) throws java.lang.Exception
Resolve the referenceref
to an entry withinbundle
and return the corresponding resource- Parameters:
resourceType
-bundle
-sourceEntry
- allowed to be null if and only if the reference is absoluteref
-- Throws:
java.lang.Exception
- if the resource could not be found, the reference has no value, or the value does not match the expected format for a bundle referencejava.lang.ClassCastException
- if the referenced resource cannot be cast to typeresourceType
-
resolveBundleReference
public static Bundle.Entry resolveBundleReference(Bundle bundle, Bundle.Entry sourceEntry, Reference ref) throws FHIRException, java.net.URISyntaxException
Resolve the referenceref
to an entry withinbundle
- Parameters:
bundle
-sourceEntry
- allowed to be null if and only if the reference is absoluteref
-- Throws:
FHIRException
- if the resource could not be found or the reference has no valuejava.net.URISyntaxException
- if theref
value is not a valid URIjava.lang.IllegalArgumentException
- ifref
contains a fragment reference
-
getExtensionStringValue
public static java.lang.String getExtensionStringValue(Resource resource, java.lang.String extensionUrl)
Returns the string value of the specified extension element within the specified resource.- Parameters:
resource
-extensionUrl
-- Returns:
- the value of the first such extension with a valueString or null if the resource has no such extensions
-
getExtensionStringValue
public static java.lang.String getExtensionStringValue(Element element, java.lang.String extensionUrl)
Returns the string value of the specified extension element within the specified element.- Parameters:
element
-extensionUrl
-- Returns:
- the value of the first such extension with a valueString or null if the resource has no such extensions
-
addTag
public static <T extends Resource> T addTag(T resource, Coding tag)
Return a copy of resourceresource
with tagtag
- Type Parameters:
T
-- Parameters:
resource
- the resource to add the tag tootag
- the tag to add- Returns:
- a copy of the resource with the new tag (if it didn't already exist), or the resource passed in if the tag is already present
-
anyFailureInIssues
public static boolean anyFailureInIssues(java.util.List<OperationOutcome.Issue> issues)
Determine if any of the issues in the list of issues are failure issues- Parameters:
issues
-- Returns:
-
isFailure
public static boolean isFailure(IssueSeverity severity)
Determine if the given severity should be treated as a failure- Parameters:
severity
-- Returns:
-
createStandaloneBundle
public static Bundle createStandaloneBundle(BundleType bundleType, java.util.Map<java.lang.String,Resource> resources)
Create a self-contained bundle from the passed map of resources, replacing Resource.id values and references with a generated UUID.- Parameters:
bundleType
- The type of bundle to createresources
- A mapping from String identifiers to Resources. For resources with no logical id, the key can be any string- Returns:
- a Bundle with the passed resources with ids and references replaced by UUIDs
-
buildBundleReference
public static java.lang.String buildBundleReference(Reference ref, java.lang.String fullUrlString)
Build the referencereference
based on thefullUrlString
value.- Parameters:
ref
-fullUrlString
-- Returns:
- An absolute fullUrl string for the literal value of Reference
ref
, or the passed literal value in Referenceref
if it cannot be parsed, or null ifref
has no literal value
-
-