001/*
002 * #%L
003 * HAPI FHIR Search Parameters
004 * %%
005 * Copyright (C) 2014 - 2023 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.extractor;
021
022
023import ca.uhn.fhir.context.RuntimeSearchParam;
024import ca.uhn.fhir.jpa.model.entity.*;
025import org.hl7.fhir.instance.model.api.IBase;
026import org.hl7.fhir.instance.model.api.IBaseResource;
027
028import java.util.ArrayList;
029import java.util.Collection;
030import java.util.Collections;
031import java.util.Date;
032import java.util.HashSet;
033import java.util.List;
034
035public interface ISearchParamExtractor {
036
037        /**
038         * Constant for the {@literal theSearchParamFilter} parameters on this interface
039         * indicating that all search parameters should be indexed.
040         */
041        ISearchParamFilter ALL_PARAMS = t -> t;
042
043        /**
044         * Constant for the {@literal theSearchParamFilter} parameters on this interface
045         * indicating that no search parameters should be indexed.
046         */
047        ISearchParamFilter NO_PARAMS = t -> Collections.emptyList();
048
049        default SearchParamSet<ResourceIndexedSearchParamDate> extractSearchParamDates(IBaseResource theResource) {
050                return extractSearchParamDates(theResource, ALL_PARAMS);
051        }
052
053        SearchParamSet<ResourceIndexedSearchParamDate> extractSearchParamDates(IBaseResource theResource, ISearchParamFilter theSearchParamFilter);
054
055        default SearchParamSet<ResourceIndexedSearchParamNumber> extractSearchParamNumber(IBaseResource theResource) {
056                return extractSearchParamNumber(theResource, ALL_PARAMS);
057        }
058
059        SearchParamSet<ResourceIndexedSearchParamNumber> extractSearchParamNumber(IBaseResource theResource, ISearchParamFilter theSearchParamFilter);
060
061        default SearchParamSet<ResourceIndexedSearchParamQuantity> extractSearchParamQuantity(IBaseResource theResource) {
062                return extractSearchParamQuantity(theResource, ALL_PARAMS);
063        }
064
065        SearchParamSet<ResourceIndexedSearchParamQuantity> extractSearchParamQuantity(IBaseResource theResource, ISearchParamFilter theSearchParamFilter);
066
067        default SearchParamSet<ResourceIndexedSearchParamQuantityNormalized> extractSearchParamQuantityNormalized(IBaseResource theResource) {
068                return extractSearchParamQuantityNormalized(theResource, ALL_PARAMS);
069        }
070
071        SearchParamSet<ResourceIndexedSearchParamQuantityNormalized> extractSearchParamQuantityNormalized(IBaseResource theResource, ISearchParamFilter theSearchParamFilter);
072
073        default SearchParamSet<ResourceIndexedSearchParamString> extractSearchParamStrings(IBaseResource theResource) {
074                return extractSearchParamStrings(theResource, ALL_PARAMS);
075        }
076
077        SearchParamSet<ResourceIndexedSearchParamString> extractSearchParamStrings(IBaseResource theResource, ISearchParamFilter theSearchParamFilter);
078
079        default SearchParamSet<ResourceIndexedSearchParamComposite> extractSearchParamComposites(IBaseResource theResource) {
080                return extractSearchParamComposites(theResource, ALL_PARAMS);
081        }
082
083        SearchParamSet<ResourceIndexedSearchParamComposite> extractSearchParamComposites(IBaseResource theResource, ISearchParamFilter theSearchParamFilter);
084
085        default SearchParamSet<BaseResourceIndexedSearchParam> extractSearchParamTokens(IBaseResource theResource) {
086                return extractSearchParamTokens(theResource, ALL_PARAMS);
087        }
088
089        SearchParamSet<BaseResourceIndexedSearchParam> extractSearchParamTokens(IBaseResource theResource, ISearchParamFilter theSearchParamFilter);
090
091        SearchParamSet<BaseResourceIndexedSearchParam> extractSearchParamTokens(IBaseResource theResource, RuntimeSearchParam theSearchParam);
092
093        SearchParamSet<BaseResourceIndexedSearchParam> extractSearchParamSpecial(IBaseResource theResource, ISearchParamFilter theSearchParamFilter);
094
095        SearchParamSet<ResourceIndexedComboStringUnique> extractSearchParamComboUnique(String theResourceType, ResourceIndexedSearchParams theParams);
096
097        SearchParamSet<ResourceIndexedComboTokenNonUnique> extractSearchParamComboNonUnique(String theResourceType, ResourceIndexedSearchParams theParams);
098
099        default SearchParamSet<ResourceIndexedSearchParamUri> extractSearchParamUri(IBaseResource theResource) {
100                return extractSearchParamUri(theResource, ALL_PARAMS);
101        }
102
103
104        SearchParamSet<ResourceIndexedSearchParamUri> extractSearchParamUri(IBaseResource theResource, ISearchParamFilter theSearchParamFilter);
105
106        SearchParamSet<PathAndRef> extractResourceLinks(IBaseResource theResource, boolean theWantLocalReferences);
107
108        String[] split(String theExpression);
109
110        List<String> extractParamValuesAsStrings(RuntimeSearchParam theActiveSearchParam, IBaseResource theResource);
111
112        List<IBase> extractValues(String thePaths, IBase theResource);
113
114        String toRootTypeName(IBase nextObject);
115
116        String toTypeName(IBase nextObject);
117
118        PathAndRef extractReferenceLinkFromResource(IBase theValue, String thePath);
119
120        Date extractDateFromResource(IBase theValue, String thePath);
121
122        ResourceIndexedSearchParamToken createSearchParamForCoding(String theResourceType, RuntimeSearchParam theSearchParam, IBase theValue);
123
124        String getDisplayTextForCoding(IBase theValue);
125
126        BaseSearchParamExtractor.IValueExtractor getPathValueExtractor(IBase theResource, String theSinglePath);
127
128        List<IBase> getCodingsFromCodeableConcept(IBase theValue);
129
130        String getDisplayTextFromCodeableConcept(IBase theValue);
131
132        @FunctionalInterface
133        interface ISearchParamFilter {
134
135                /**
136                 * Given the list of search parameters for indexing, an implementation of this
137                 * interface may selectively remove any that it wants to remove (or can add if desired).
138                 * <p>
139                 * Implementations must not modify the list that is passed in. If changes are
140                 * desired, a new list must be created and returned.
141                 */
142                Collection<RuntimeSearchParam> filterSearchParams(Collection<RuntimeSearchParam> theSearchParams);
143
144        }
145
146        class SearchParamSet<T> extends HashSet<T> {
147
148                private List<String> myWarnings;
149
150                public void addWarning(String theWarning) {
151                        if (myWarnings == null) {
152                                myWarnings = new ArrayList<>();
153                        }
154                        myWarnings.add(theWarning);
155                }
156
157                List<String> getWarnings() {
158                        if (myWarnings == null) {
159                                return Collections.emptyList();
160                        }
161                        return myWarnings;
162                }
163
164        }
165
166
167}