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.registry;
021
022import ca.uhn.fhir.context.RuntimeSearchParam;
023import ca.uhn.fhir.rest.server.util.ResourceSearchParams;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027import java.util.Map;
028import java.util.Set;
029
030import static org.apache.commons.lang3.StringUtils.isNotBlank;
031
032public class RuntimeSearchParamCache extends ReadOnlySearchParamCache {
033        private static final Logger ourLog = LoggerFactory.getLogger(RuntimeSearchParamCache.class);
034
035        protected RuntimeSearchParamCache() {
036        }
037
038        public void add(String theResourceName, String theName, RuntimeSearchParam theSearchParam) {
039                ResourceSearchParams resourceSearchParams = getSearchParamMap(theResourceName);
040                resourceSearchParams.put(theName, theSearchParam);
041                String uri = theSearchParam.getUri();
042                if (isNotBlank(uri)) {
043                        RuntimeSearchParam existingForUrl = myUrlToParam.get(uri);
044                        if (existingForUrl == theSearchParam) {
045                                // This is expected, since the same SP can span multiple resource types
046                                // so it may get added more than once by this method
047                                ourLog.trace("Search param was previously registered for url: {}", uri);
048                        } else if (existingForUrl != null) {
049                                ourLog.debug("Multiple search parameters have URL: {}", uri);
050                        } else {
051                                myUrlToParam.put(uri, theSearchParam);
052                        }
053                }
054                if (theSearchParam.getId() != null && theSearchParam.getId().hasIdPart()) {
055                        String value = theSearchParam.getId().toUnqualifiedVersionless().getValue();
056                        myUrlToParam.put(value, theSearchParam);
057                }
058        }
059
060        public void remove(String theResourceName, String theName) {
061                if (!myResourceNameToSpNameToSp.containsKey(theResourceName)) {
062                        return;
063                }
064                myResourceNameToSpNameToSp.get(theResourceName).remove(theName);
065        }
066
067        private void putAll(ReadOnlySearchParamCache theReadOnlySearchParamCache) {
068                Set<Map.Entry<String, ResourceSearchParams>> builtInSps = theReadOnlySearchParamCache.myResourceNameToSpNameToSp.entrySet();
069                for (Map.Entry<String, ResourceSearchParams> nextBuiltInEntry : builtInSps) {
070                        for (RuntimeSearchParam nextParam : nextBuiltInEntry.getValue().values()) {
071                                String nextResourceName = nextBuiltInEntry.getKey();
072                                String nextParamName = nextParam.getName();
073                                add(nextResourceName, nextParamName, nextParam);
074                        }
075
076                        ourLog.trace("Have {} built-in SPs for: {}", nextBuiltInEntry.getValue().size(), nextBuiltInEntry.getKey());
077                }
078        }
079
080        public RuntimeSearchParam get(String theResourceName, String theParamName) {
081                RuntimeSearchParam retVal = null;
082                ResourceSearchParams params = myResourceNameToSpNameToSp.get(theResourceName);
083                if (params != null) {
084                        retVal = params.get(theParamName);
085                }
086                return retVal;
087        }
088
089        public Set<String> getResourceNameKeys() {
090                return myResourceNameToSpNameToSp.keySet();
091        }
092
093        @Override
094        protected ResourceSearchParams getSearchParamMap(String theResourceName) {
095                return myResourceNameToSpNameToSp.computeIfAbsent(theResourceName, k -> new ResourceSearchParams(theResourceName));
096        }
097
098        public static RuntimeSearchParamCache fromReadOnlySearchParamCache(ReadOnlySearchParamCache theBuiltInSearchParams) {
099                RuntimeSearchParamCache retVal = new RuntimeSearchParamCache();
100                retVal.putAll(theBuiltInSearchParams);
101                return retVal;
102        }
103}