001package ca.uhn.fhir.jpa.searchparam.provider;
002
003/*-
004 * #%L
005 * HAPI FHIR Search Parameters
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
025import ca.uhn.fhir.jpa.searchparam.matcher.InMemoryMatchResult;
026import ca.uhn.fhir.jpa.searchparam.matcher.SearchParamMatcher;
027import ca.uhn.fhir.rest.api.server.RequestDetails;
028import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
029import ca.uhn.fhir.rest.server.provider.HashMapResourceProvider;
030import org.hl7.fhir.instance.model.api.IBaseResource;
031
032import java.util.ArrayList;
033import java.util.List;
034import java.util.function.Function;
035
036public class SearchableHashMapResourceProvider<T extends IBaseResource> extends HashMapResourceProvider<T> {
037        private final SearchParamMatcher mySearchParamMatcher;
038
039        /**
040         * Constructor
041         *
042         * @param theFhirContext  The FHIR context
043         * @param theResourceType The resource type to support
044         */
045        public SearchableHashMapResourceProvider(FhirContext theFhirContext, Class<T> theResourceType, SearchParamMatcher theSearchParamMatcher) {
046                super(theFhirContext, theResourceType);
047                mySearchParamMatcher = theSearchParamMatcher;
048        }
049
050        public List<IBaseResource> searchByCriteria(String theCriteria, RequestDetails theRequest) {
051                return searchBy(resource -> mySearchParamMatcher.match(theCriteria, resource, theRequest), theRequest);
052
053        }
054
055        public List<IBaseResource> searchByParams(SearchParameterMap theSearchParams, RequestDetails theRequest) {
056                return searchBy(resource -> mySearchParamMatcher.match(theSearchParams.toNormalizedQueryString(getFhirContext()), resource, theRequest), theRequest);
057        }
058
059        private List<IBaseResource> searchBy(Function<IBaseResource, InMemoryMatchResult> theMatcher, RequestDetails theRequest) {
060                mySearchCount.incrementAndGet();
061                List<T> allEResources = getAllResources();
062
063                List<T> matches = new ArrayList<>();
064                for (T resource : allEResources) {
065                        InMemoryMatchResult result = theMatcher.apply(resource);
066                        if (!result.supported()) {
067                                throw new InvalidRequestException("Search not supported by in-memory matcher: "+result.getUnsupportedReason());
068                        }
069                        if (result.matched()) {
070                                matches.add(resource);
071                        }
072                }
073                return fireInterceptorsAndFilterAsNeeded(matches, theRequest);
074        }
075}