Class FHIRUtil


  • public class FHIRUtil
    extends Object
    Utility methods for working with the FHIR object model.
    • Field Detail

      • REFERENCE_PATTERN

        public static final 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 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 javax.json.JsonObjectBuilder toJsonObjectBuilder​(javax.json.JsonObject jsonObject)
      • 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​(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​(Exception exception,
                                                             IssueType issueType,
                                                             IssueSeverity severity,
                                                             boolean includeCausedByClauses)
        Build an OperationOutcome for the specified exception.
      • buildOperationOutcome

        public static OperationOutcome buildOperationOutcome​(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 URI buildLocationURI​(String type,
                                           Resource resource)
        Builds a relative "Location" header value for the specified resource. This will be a string of the form "//_history/". Note that the server will turn this into an absolute URL prior to returning it to the client.
        Parameters:
        resource - the resource for which the location header value should be returned
      • resolveReference

        public static Resource resolveReference​(Reference ref,
                                                DomainResource resource,
                                                Bundle bundle,
                                                Bundle.Entry entry)
                                         throws 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:
        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 Exception
        Resolve the reference ref to a bundle entry and return the corresponding resource container
        Throws:
        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​(Class<T> resourceType,
                                                                    Bundle bundle,
                                                                    Bundle.Entry sourceEntry,
                                                                    Reference ref)
                                                             throws 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:
        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
        ClassCastException - if the referenced resource cannot be cast to type resourceType
      • getExtensionStringValue

        public static String getExtensionStringValue​(Resource resource,
                                                     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 String getExtensionStringValue​(Element element,
                                                     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
      • getResourceTypeNames

        public static List<String> getResourceTypeNames()
        Returns:
        a list of all resource type names, including abstract supertypes
      • 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,
                                                    Map<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