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;
021
022import ca.uhn.fhir.svcs.ISearchLimiterSvc;
023import com.google.common.collect.HashMultimap;
024import com.google.common.collect.Multimap;
025import jakarta.annotation.Nonnull;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import java.util.Collection;
030import java.util.HashMap;
031import java.util.Map;
032import java.util.Set;
033
034public class SearchLimiterSvc implements ISearchLimiterSvc {
035        private static final Logger ourLog = LoggerFactory.getLogger(ISearchLimiterSvc.class);
036
037        private final Multimap<String, String> myOperationToOmittedResourceTypes = HashMultimap.create();
038
039        private final Map<String, Set<String>> myOperationToOmittedResourceTypeCacheMap = new HashMap<>();
040
041        public void addOmittedResourceType(@Nonnull String theOperationName, @Nonnull String theResourceType) {
042                if (ourLog.isDebugEnabled()) {
043                        ourLog.debug(
044                                        "Filtering operation {} to prevent including resources of type {}",
045                                        theOperationName,
046                                        theResourceType);
047                }
048                myOperationToOmittedResourceTypes.put(theOperationName, theResourceType);
049        }
050
051        @Override
052        public Collection<String> getResourcesToOmitForOperationSearches(@Nonnull String theOperationName) {
053                return myOperationToOmittedResourceTypes.get(theOperationName);
054        }
055
056        @Override
057        public void removeOmittedResourceType(@Nonnull String theOperationName, @Nonnull String theResourceType) {
058                ourLog.debug("Removing any filtering of {} for {} operation", theResourceType, theOperationName);
059                myOperationToOmittedResourceTypes.remove(theOperationName, theResourceType);
060        }
061
062        @Override
063        public void removeAllResourcesForOperation(String theOperationName) {
064                ourLog.debug("Removing any filtering of resources for {} operations", theOperationName);
065                myOperationToOmittedResourceTypes.removeAll(theOperationName);
066        }
067}