Class FHIRUtil


  • public class FHIRUtil
    extends java.lang.Object
    Utility methods for working with the FHIR object model.
    • 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
    • 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)
      • 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.EXCEPTION
        severity - 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 name
        resource - 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 name
        id - the resource logical id value
        version - the resource version
        Returns:
      • resolveReference

        public static Resource resolveReference​(Reference ref,
                                                DomainResource resource,
                                                Bundle bundle,
                                                Bundle.Entry entry)
                                         throws java.lang.Exception
        Resolve reference ref to a bundle entry or a resource contained within resource and return the corresponding resource container. Resolving ref 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 reference ref 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 reference ref to an entry within bundle and return the corresponding resource
        Parameters:
        resourceType -
        bundle -
        sourceEntry - allowed to be null if and only if the reference is absolute
        ref -
        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 reference
        java.lang.ClassCastException - if the referenced resource cannot be cast to type resourceType
      • resolveBundleReference

        public static Bundle.Entry resolveBundleReference​(Bundle bundle,
                                                          Bundle.Entry sourceEntry,
                                                          Reference ref)
                                                   throws FHIRException,
                                                          java.net.URISyntaxException
        Resolve the reference ref to an entry within bundle
        Parameters:
        bundle -
        sourceEntry - allowed to be null if and only if the reference is absolute
        ref -
        Throws:
        FHIRException - if the resource could not be found or the reference has no value
        java.net.URISyntaxException - if the ref value is not a valid URI
        java.lang.IllegalArgumentException - if ref 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
      • hasTag

        public static boolean hasTag​(Resource resource,
                                     Coding tag)
      • addTag

        public static <T extends Resource> T addTag​(T resource,
                                                    Coding tag)
        Return a copy of resource resource with tag tag
        Type Parameters:
        T -
        Parameters:
        resource - the resource to add the tag too
        tag - 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 create
        resources - 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 reference reference based on the fullUrlString value.
        Parameters:
        ref -
        fullUrlString -
        Returns:
        An absolute fullUrl string for the literal value of Reference ref, or the passed literal value in Reference ref if it cannot be parsed, or null if ref has no literal value