001/*-
002 * #%L
003 * HAPI FHIR JPA - Search Parameters
004 * %%
005 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.searchparam.util;
021
022import ca.uhn.fhir.context.RuntimeSearchParam;
023import ca.uhn.fhir.jpa.model.entity.StorageSettings;
024import jakarta.annotation.Nonnull;
025
026import static ca.uhn.fhir.rest.api.Constants.PARAM_CONTENT;
027import static ca.uhn.fhir.rest.api.Constants.PARAM_HAS;
028import static ca.uhn.fhir.rest.api.Constants.PARAM_ID;
029import static ca.uhn.fhir.rest.api.Constants.PARAM_IN;
030import static ca.uhn.fhir.rest.api.Constants.PARAM_LANGUAGE;
031import static ca.uhn.fhir.rest.api.Constants.PARAM_LASTUPDATED;
032import static ca.uhn.fhir.rest.api.Constants.PARAM_PROFILE;
033import static ca.uhn.fhir.rest.api.Constants.PARAM_SECURITY;
034import static ca.uhn.fhir.rest.api.Constants.PARAM_SOURCE;
035import static ca.uhn.fhir.rest.api.Constants.PARAM_TAG;
036import static ca.uhn.fhir.rest.api.Constants.PARAM_TEXT;
037import static org.apache.commons.lang3.StringUtils.defaultString;
038import static org.apache.commons.lang3.StringUtils.startsWith;
039
040public class RuntimeSearchParamHelper {
041
042        /**
043         * Helper function to determine if a RuntimeSearchParam is a resource level search param.
044         *
045         * @param theSearchParam the parameter to check
046         * @return return boolean
047         */
048        public static boolean isResourceLevel(RuntimeSearchParam theSearchParam) {
049                return startsWith(theSearchParam.getPath(), "Resource.");
050        }
051
052        /**
053         * Returns {@literal true} if the given Search Parameter is one that is handled by special
054         * handling as opposed to being simply indexed by FHIRPath expression. For example, parameters
055         * such as _tag, _id, _content, etc. are all handled in one-off routines as opposed to just
056         * looking up the value using the FHIRPath expression and indexing it.
057         */
058        @SuppressWarnings("DuplicateBranchesInSwitch")
059        public static boolean isSpeciallyHandledSearchParameter(
060                        @Nonnull RuntimeSearchParam theSearchParameter, StorageSettings theStorageSettings) {
061                return switch (defaultString(theSearchParameter.getName())) {
062                        case PARAM_CONTENT -> true;
063                        case PARAM_HAS -> true;
064                        case PARAM_ID -> true;
065                        case PARAM_IN -> true;
066                        case PARAM_LASTUPDATED -> true;
067                        case PARAM_LANGUAGE -> false;
068                        case PARAM_SOURCE -> true;
069                        case PARAM_TEXT -> true;
070                        case PARAM_PROFILE, PARAM_TAG, PARAM_SECURITY -> theStorageSettings.getTagStorageMode()
071                                        != StorageSettings.TagStorageModeEnum.INLINE;
072                        default -> false;
073                };
074        }
075}