001/*-
002 * #%L
003 * HAPI FHIR - Master Data Management
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.mdm.interceptor;
021
022import ca.uhn.fhir.interceptor.api.Hook;
023import ca.uhn.fhir.interceptor.api.Interceptor;
024import ca.uhn.fhir.interceptor.api.Pointcut;
025import ca.uhn.fhir.jpa.api.config.JpaStorageSettings;
026import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
027import ca.uhn.fhir.mdm.api.MdmConstants;
028import ca.uhn.fhir.mdm.svc.MdmSearchExpansionSvc;
029import ca.uhn.fhir.rest.api.server.RequestDetails;
030import ca.uhn.fhir.rest.param.ReferenceParam;
031import ca.uhn.fhir.rest.param.TokenParam;
032import org.springframework.beans.factory.annotation.Autowired;
033
034/**
035 * This interceptor replaces the auto-generated CapabilityStatement that is generated
036 * by the HAPI FHIR Server with a static hard-coded resource.
037 */
038@Interceptor
039public class MdmSearchExpandingInterceptor {
040
041        private static final MdmSearchExpansionSvc.IParamTester PARAM_TESTER = (paramName, param) -> {
042                boolean retVal = false;
043                if (param instanceof ReferenceParam) {
044                        retVal = ((ReferenceParam) param).isMdmExpand();
045                } else if (param instanceof TokenParam) {
046                        retVal = ((TokenParam) param).isMdmExpand();
047                }
048                return retVal;
049        };
050
051        @Autowired
052        private JpaStorageSettings myStorageSettings;
053
054        @Autowired
055        private MdmSearchExpansionSvc myMdmSearchExpansionSvc;
056
057        @Hook(
058                        value = Pointcut.STORAGE_PRESEARCH_REGISTERED,
059                        order = MdmConstants.ORDER_PRESEARCH_REGISTERED_MDM_SEARCH_EXPANDING_INTERCEPTOR)
060        public void hook(RequestDetails theRequestDetails, SearchParameterMap theSearchParameterMap) {
061
062                if (myStorageSettings.isAllowMdmExpansion()) {
063                        myMdmSearchExpansionSvc.expandSearchAndStoreInRequestDetails(
064                                        theRequestDetails, theSearchParameterMap, PARAM_TESTER);
065                }
066        }
067}