Class SearchHelper
- java.lang.Object
-
- org.linuxforhealth.fhir.search.util.SearchHelper
-
public class SearchHelper extends java.lang.Object
A helper class with methods for working with HL7 FHIR search.
-
-
Constructor Summary
Constructors Constructor Description SearchHelper()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description QueryParameter
buildInclusionCriteria(java.lang.String compartmentName, java.util.Set<java.lang.String> compartmentLogicalIds, java.lang.String resourceType)
Build a query parameter to encapsulate the inclusion criteria for a compartment querystatic java.lang.String
buildSearchSelfUri(java.lang.String requestUriString, FHIRSearchContext context)
Build the self link from the search parameters actually used by the serverstatic void
checkInclusionIterateParameters(java.lang.String resourceType, FHIRSearchContext context, boolean lenient)
Check if _include or _revinclude parameters with the :iterate modifier reference invalid resource types.static boolean
containsInclusionParameter(java.util.Set<java.lang.String> searchParameterCodes)
Check if the list of search parameters contains either _include or _revinclude.java.util.Map<SearchParameter,java.util.List<FHIRPathNode>>
extractParameterValues(Resource resource)
skips the empty extracted search parametersjava.util.Map<SearchParameter,java.util.List<FHIRPathNode>>
extractParameterValues(Resource resource, boolean skipEmpty)
extract parameter values.static java.lang.String
extractReferenceValue(java.lang.String valueString)
Convert the string to a reference value usable by the persistence layer.static java.lang.String
findImplicitSystem(java.util.List<Extension> extensions)
Look up the http://fhir.linuxforhealth.org/extension/implicit-system extension in the given list of ExtensionsSearchParameter
getSearchParameter(java.lang.Class<?> resourceType, java.lang.String code)
SearchParameter
getSearchParameter(java.lang.Class<?> resourceType, Canonical uri)
SearchParameter
getSearchParameter(java.lang.String resourceType, java.lang.String code)
SearchParameter
getSearchParameter(java.lang.String resourceType, Canonical uri)
java.util.Map<java.lang.String,SearchParameter>
getSearchParameters(java.lang.String resourceType)
Returns a map of code to SearchParameter that consist of those associated with the "Resource" base resource type, as well as those associated with the specified resource type.static java.util.Set<java.lang.String>
getSummaryTextElementNames(java.lang.Class<?> resourceType)
Return only the "text" element, the 'id' element, the 'meta' element, and only top-level mandatory elements.protected static boolean
isAllowed(SearchConstants.Type type, SearchConstants.Modifier modifier)
static boolean
isChainedParameter(java.lang.String name)
static boolean
isCompartmentSearch(FHIRSearchContext searchContext)
Inspect the searchContext to see if the parameters define a compartment-based search.boolean
isCompartmentType(java.lang.String resourceType)
static boolean
isGeneralParameter(java.lang.String name)
static boolean
isReverseChainedParameter(java.lang.String code)
static boolean
isSearchResultParameter(java.lang.String name)
Determine if the parameter is a search result parameter.static boolean
isSearchSingletonParameter(java.lang.String name)
static java.lang.String
makeCompositeSubCode(java.lang.String compositeCode, java.lang.String subParameterCode)
Build a parameter name (code) which can be used to uniquely represent the stored composite sub-parameter (the values get added to the parameter_names table).static java.lang.String
normalizeForSearch(java.lang.String value)
Normalizes a string to be used as a search parameter value.static QueryParameter
parseChainedInclusionCriteria(QueryParameter inclusionCriteriaParm)
Transforms the passed QueryParameter representing chained inclusion criteria, into an actual chain of QueryParameter objects.FHIRSearchContext
parseCompartmentQueryParameters(java.lang.String compartmentName, java.lang.String compartmentLogicalId, java.lang.Class<?> resourceType, java.util.Map<java.lang.String,java.util.List<java.lang.String>> queryParameters)
FHIRSearchContext
parseCompartmentQueryParameters(java.lang.String compartmentName, java.lang.String compartmentLogicalId, java.lang.Class<?> resourceType, java.util.Map<java.lang.String,java.util.List<java.lang.String>> queryParameters, boolean lenient, boolean includeResource, FHIRVersionParam fhirVersion)
FHIRSearchContext
parseQueryParameters(java.lang.Class<?> resourceType, java.util.Map<java.lang.String,java.util.List<java.lang.String>> queryParameters)
FHIRSearchContext
parseQueryParameters(java.lang.Class<?> resourceType, java.util.Map<java.lang.String,java.util.List<java.lang.String>> queryParameters, boolean lenient, boolean includeResource)
FHIRSearchContext
parseQueryParameters(java.lang.Class<?> resourceType, java.util.Map<java.lang.String,java.util.List<java.lang.String>> queryParameters, boolean lenient, boolean includeResource, FHIRVersionParam fhirVersion)
Parse the passed query parameters into a FHIRSeachContext according to the given optionsFHIRSearchContext
parseReadQueryParameters(java.lang.Class<?> resourceType, java.util.Map<java.lang.String,java.util.List<java.lang.String>> queryParameters, java.lang.String interaction, boolean lenient, FHIRVersionParam fhirVersion)
Parse query parameters for read and vread.
-
-
-
Method Detail
-
getSearchParameter
public SearchParameter getSearchParameter(java.lang.Class<?> resourceType, java.lang.String code) throws java.lang.Exception
- Parameters:
resourceType
-code
-- Returns:
- the SearchParameter for type
resourceType
with codecode
or null if it doesn't exist - Throws:
java.lang.Exception
-
getSearchParameter
public SearchParameter getSearchParameter(java.lang.String resourceType, java.lang.String code) throws java.lang.Exception
- Parameters:
resourceType
-code
-- Returns:
- the SearchParameter for type
resourceType
with codecode
or null if it doesn't exist - Throws:
java.lang.Exception
-
getSearchParameter
public SearchParameter getSearchParameter(java.lang.Class<?> resourceType, Canonical uri) throws java.lang.Exception
- Parameters:
resourceType
-uri
-- Returns:
- the SearchParameter for type
resourceType
with urluri
or null if it doesn't exist - Throws:
java.lang.Exception
-
getSearchParameter
public SearchParameter getSearchParameter(java.lang.String resourceType, Canonical uri) throws java.lang.Exception
- Parameters:
resourceType
-uri
-- Returns:
- the SearchParameter for type
resourceType
with canonical urluri
or null if it doesn't exist - Throws:
java.lang.Exception
-
extractParameterValues
public java.util.Map<SearchParameter,java.util.List<FHIRPathNode>> extractParameterValues(Resource resource) throws java.lang.Exception
skips the empty extracted search parameters- Parameters:
resource
-- Returns:
- Throws:
java.lang.Exception
-
extractParameterValues
public java.util.Map<SearchParameter,java.util.List<FHIRPathNode>> extractParameterValues(Resource resource, boolean skipEmpty) throws java.lang.Exception
extract parameter values.- Parameters:
resource
-skipEmpty
-- Returns:
- Throws:
java.lang.Exception
-
parseQueryParameters
public FHIRSearchContext parseQueryParameters(java.lang.Class<?> resourceType, java.util.Map<java.lang.String,java.util.List<java.lang.String>> queryParameters) throws java.lang.Exception
- Throws:
java.lang.Exception
-
parseQueryParameters
public FHIRSearchContext parseQueryParameters(java.lang.Class<?> resourceType, java.util.Map<java.lang.String,java.util.List<java.lang.String>> queryParameters, boolean lenient, boolean includeResource) throws java.lang.Exception
- Throws:
java.lang.Exception
-
parseQueryParameters
public FHIRSearchContext parseQueryParameters(java.lang.Class<?> resourceType, java.util.Map<java.lang.String,java.util.List<java.lang.String>> queryParameters, boolean lenient, boolean includeResource, FHIRVersionParam fhirVersion) throws java.lang.Exception
Parse the passed query parameters into a FHIRSeachContext according to the given options- Parameters:
resourceType
-queryParameters
-lenient
-includeResource
-fhirVersion
-- Returns:
- Throws:
java.lang.Exception
-
findImplicitSystem
public static java.lang.String findImplicitSystem(java.util.List<Extension> extensions)
Look up the http://fhir.linuxforhealth.org/extension/implicit-system extension in the given list of Extensions- Parameters:
extensions
-- Returns:
- the implicit system value, or null if not found
-
extractReferenceValue
public static java.lang.String extractReferenceValue(java.lang.String valueString) throws FHIRSearchException
Convert the string to a reference value usable by the persistence layer. This simply involves removing the URL prefix if it matches the originalUri in the request context- Parameters:
valueString
-- Returns:
- Throws:
FHIRSearchException
-
isAllowed
protected static boolean isAllowed(SearchConstants.Type type, SearchConstants.Modifier modifier)
- Parameters:
type
-modifier
-- Returns:
-
getSearchParameters
public java.util.Map<java.lang.String,SearchParameter> getSearchParameters(java.lang.String resourceType) throws java.lang.Exception
Returns a map of code to SearchParameter that consist of those associated with the "Resource" base resource type, as well as those associated with the specified resource type.- Returns:
- the applicable search parameters for the current request context, indexed by code; never null
- Throws:
java.lang.Exception
-
parseReadQueryParameters
public FHIRSearchContext parseReadQueryParameters(java.lang.Class<?> resourceType, java.util.Map<java.lang.String,java.util.List<java.lang.String>> queryParameters, java.lang.String interaction, boolean lenient, FHIRVersionParam fhirVersion) throws java.lang.Exception
Parse query parameters for read and vread.- Parameters:
resourceType
- the resource typequeryParameters
- the query parametersinteraction
- read or vreadlenient
- true if lenient, false if strictfhirVersion
-- Returns:
- the FHIR search context
- Throws:
java.lang.Exception
- an exception
-
parseCompartmentQueryParameters
public FHIRSearchContext parseCompartmentQueryParameters(java.lang.String compartmentName, java.lang.String compartmentLogicalId, java.lang.Class<?> resourceType, java.util.Map<java.lang.String,java.util.List<java.lang.String>> queryParameters) throws java.lang.Exception
- Throws:
java.lang.Exception
-
parseCompartmentQueryParameters
public FHIRSearchContext parseCompartmentQueryParameters(java.lang.String compartmentName, java.lang.String compartmentLogicalId, java.lang.Class<?> resourceType, java.util.Map<java.lang.String,java.util.List<java.lang.String>> queryParameters, boolean lenient, boolean includeResource, FHIRVersionParam fhirVersion) throws java.lang.Exception
- Parameters:
compartmentName
-compartmentLogicalId
-resourceType
-queryParameters
-lenient
- Whether to ignore unknown or unsupported parameterincludeResource
- Whether to include the resource from the result (return handling prefer != minimal)fhirVersion
-- Returns:
- Throws:
java.lang.Exception
-
buildInclusionCriteria
public QueryParameter buildInclusionCriteria(java.lang.String compartmentName, java.util.Set<java.lang.String> compartmentLogicalIds, java.lang.String resourceType) throws FHIRSearchException
Build a query parameter to encapsulate the inclusion criteria for a compartment query- Parameters:
compartmentName
-compartmentLogicalIds
-resourceType
-- Returns:
- Throws:
FHIRSearchException
-
isSearchResultParameter
public static boolean isSearchResultParameter(java.lang.String name)
Determine if the parameter is a search result parameter.- Parameters:
name
- - the parameter name- Returns:
- true if the parameter is a search result parameter, false otherwise
-
isSearchSingletonParameter
public static boolean isSearchSingletonParameter(java.lang.String name)
-
isGeneralParameter
public static boolean isGeneralParameter(java.lang.String name)
-
isChainedParameter
public static boolean isChainedParameter(java.lang.String name)
-
isReverseChainedParameter
public static boolean isReverseChainedParameter(java.lang.String code)
-
parseChainedInclusionCriteria
public static QueryParameter parseChainedInclusionCriteria(QueryParameter inclusionCriteriaParm)
Transforms the passed QueryParameter representing chained inclusion criteria, into an actual chain of QueryParameter objects. This method consumes QueryParameters with names of this form:"{attribute1}.{attribute2}:{resourceType}"
For specific examples of chained inclusion criteria, see the FHIR spec for the Patient compartment- Parameters:
inclusionCriteriaParm
-- Returns:
- QueryParameter - The root of a parameter chain for chained inclusion criteria.
-
normalizeForSearch
public static java.lang.String normalizeForSearch(java.lang.String value)
Normalizes a string to be used as a search parameter value. All accents and diacritics are removed. Consecutive whitespace characters are replaced with a single space. And then the string is transformed to lower case.- Parameters:
value
- the string to normalize- Returns:
- the normalized string
-
buildSearchSelfUri
public static java.lang.String buildSearchSelfUri(java.lang.String requestUriString, FHIRSearchContext context) throws java.net.URISyntaxException
Build the self link from the search parameters actually used by the server- Throws:
java.net.URISyntaxException
-
getSummaryTextElementNames
public static java.util.Set<java.lang.String> getSummaryTextElementNames(java.lang.Class<?> resourceType)
Return only the "text" element, the 'id' element, the 'meta' element, and only top-level mandatory elements. The id, meta and the top-level mandatory elements will be added by the ElementFilter automatically.- Parameters:
resourceType
-- Returns:
-
makeCompositeSubCode
public static java.lang.String makeCompositeSubCode(java.lang.String compositeCode, java.lang.String subParameterCode)
Build a parameter name (code) which can be used to uniquely represent the stored composite sub-parameter (the values get added to the parameter_names table). We use a prefix just to avoid any (albeit already remote) possibility of collision. Also makes these more visible as something added by the implementation code, not from a configuration file.- Parameters:
compositeCode
-subParameterCode
-- Returns:
-
containsInclusionParameter
public static boolean containsInclusionParameter(java.util.Set<java.lang.String> searchParameterCodes)
Check if the list of search parameters contains either _include or _revinclude.- Parameters:
searchParameterCodes
- the set of search parameters to check- Returns:
- true if either _include or _revinclude found, false otherwise
-
checkInclusionIterateParameters
public static void checkInclusionIterateParameters(java.lang.String resourceType, FHIRSearchContext context, boolean lenient) throws FHIRSearchException
Check if _include or _revinclude parameters with the :iterate modifier reference invalid resource types. If in strict mode, throw a FHIRSearchException. If in lenient mode, log the invalid parameters and remove those parameters from the search context.- Parameters:
resourceType
- the search resource typecontext
- the search contextlenient
- flag indicating lenient or strict mode- Throws:
FHIRSearchException
-
isCompartmentType
public boolean isCompartmentType(java.lang.String resourceType)
- Parameters:
resourceType
-- Returns:
- whether the passed resourceType has its own CompartmentDefinition
-
isCompartmentSearch
public static boolean isCompartmentSearch(FHIRSearchContext searchContext)
Inspect the searchContext to see if the parameters define a compartment-based search. This is useful to know because it allows an implementation to enable optimizations specific to compartment-based searches.- Parameters:
searchContext
-- Returns:
-
-