001/*-
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2024 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.rest.server.util;
021
022import ca.uhn.fhir.context.ComboSearchParamType;
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.context.RuntimeResourceDefinition;
025import ca.uhn.fhir.context.RuntimeSearchParam;
026import ca.uhn.fhir.context.phonetic.IPhoneticEncoder;
027import ca.uhn.fhir.i18n.Msg;
028import jakarta.annotation.Nonnull;
029import jakarta.annotation.Nullable;
030import org.apache.commons.lang3.Validate;
031import org.hl7.fhir.instance.model.api.IIdType;
032
033import java.util.ArrayList;
034import java.util.List;
035import java.util.Optional;
036import java.util.Set;
037
038public class FhirContextSearchParamRegistry implements ISearchParamRegistry {
039
040        private final List<RuntimeSearchParam> myExtraSearchParams = new ArrayList<>();
041        private final FhirContext myCtx;
042
043        /**
044         * Constructor
045         */
046        public FhirContextSearchParamRegistry(@Nonnull FhirContext theCtx) {
047                Validate.notNull(theCtx, "theCtx must not be null");
048                myCtx = theCtx;
049        }
050
051        @Override
052        public void forceRefresh() {
053                // nothing
054        }
055
056        @Override
057        public RuntimeSearchParam getActiveSearchParam(String theResourceName, String theParamName) {
058                return getActiveSearchParams(theResourceName).get(theParamName);
059        }
060
061        @Override
062        public ResourceSearchParams getActiveSearchParams(String theResourceName) {
063                ResourceSearchParams retval = new ResourceSearchParams(theResourceName);
064                RuntimeResourceDefinition nextResDef = myCtx.getResourceDefinition(theResourceName);
065                for (RuntimeSearchParam nextSp : nextResDef.getSearchParams()) {
066                        retval.put(nextSp.getName(), nextSp);
067                }
068
069                for (RuntimeSearchParam next : myExtraSearchParams) {
070                        retval.put(next.getName(), next);
071                }
072
073                return retval;
074        }
075
076        public void addSearchParam(RuntimeSearchParam theSearchParam) {
077                myExtraSearchParams.add(theSearchParam);
078        }
079
080        @Override
081        public List<RuntimeSearchParam> getActiveComboSearchParams(String theResourceName, Set<String> theParamNames) {
082                throw new UnsupportedOperationException(Msg.code(2066));
083        }
084
085        @Nullable
086        @Override
087        public RuntimeSearchParam getActiveSearchParamByUrl(String theUrl) {
088                // simple implementation for test support
089                return myCtx.getResourceTypes().stream()
090                                .flatMap(type -> getActiveSearchParams(type).values().stream())
091                                .filter(rsp -> theUrl.equals(rsp.getUri()))
092                                .findFirst()
093                                .orElse(null);
094        }
095
096        @Override
097        public List<RuntimeSearchParam> getActiveComboSearchParams(String theResourceName) {
098                throw new UnsupportedOperationException(Msg.code(2068));
099        }
100
101        @Override
102        public List<RuntimeSearchParam> getActiveComboSearchParams(
103                        String theResourceName, ComboSearchParamType theParamType) {
104                throw new UnsupportedOperationException(Msg.code(2209));
105        }
106
107        @Override
108        public Optional<RuntimeSearchParam> getActiveComboSearchParamById(String theResourceName, IIdType theId) {
109                throw new UnsupportedOperationException(Msg.code(2211));
110        }
111
112        @Override
113        public void requestRefresh() {
114                // nothing
115        }
116
117        @Override
118        public void setPhoneticEncoder(IPhoneticEncoder thePhoneticEncoder) {
119                // nothing
120        }
121}