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.provider;
021
022import ca.uhn.fhir.context.ConfigurationException;
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.i18n.Msg;
025import ca.uhn.fhir.interceptor.api.IInterceptorBroadcaster;
026import ca.uhn.fhir.jpa.api.config.JpaStorageSettings;
027import ca.uhn.fhir.mdm.api.IMdmControllerSvc;
028import ca.uhn.fhir.mdm.api.IMdmSettings;
029import ca.uhn.fhir.mdm.api.IMdmSubmitSvc;
030import ca.uhn.fhir.rest.server.provider.ResourceProviderFactory;
031import jakarta.annotation.PreDestroy;
032import org.springframework.beans.factory.annotation.Autowired;
033import org.springframework.stereotype.Service;
034
035import java.util.function.Supplier;
036
037@Service
038public class MdmProviderLoader {
039        @Autowired
040        private FhirContext myFhirContext;
041
042        @Autowired
043        private ResourceProviderFactory myResourceProviderFactory;
044
045        @Autowired
046        private MdmControllerHelper myMdmControllerHelper;
047
048        @Autowired
049        private IMdmControllerSvc myMdmControllerSvc;
050
051        @Autowired
052        private IMdmSubmitSvc myMdmSubmitSvc;
053
054        @Autowired
055        private IMdmSettings myMdmSettings;
056
057        @Autowired
058        private JpaStorageSettings myStorageSettings;
059
060        @Autowired
061        private IInterceptorBroadcaster myInterceptorBroadcaster;
062
063        private Supplier<Object> myMdmProviderSupplier;
064        private Supplier<Object> myMdmHistoryProviderSupplier;
065
066        public void loadProvider() {
067                switch (myFhirContext.getVersion().getVersion()) {
068                        case DSTU3:
069                        case R4:
070                        case R5:
071                                // We store the supplier so that removeSupplier works properly
072                                myMdmProviderSupplier = () -> new MdmProviderDstu3Plus(
073                                                myFhirContext,
074                                                myMdmControllerSvc,
075                                                myMdmControllerHelper,
076                                                myMdmSubmitSvc,
077                                                myInterceptorBroadcaster,
078                                                myMdmSettings);
079                                // We store the supplier so that removeSupplier works properly
080                                myResourceProviderFactory.addSupplier(myMdmProviderSupplier);
081                                if (myStorageSettings.isNonResourceDbHistoryEnabled()) {
082                                        myMdmHistoryProviderSupplier = () -> new MdmLinkHistoryProviderDstu3Plus(
083                                                        myFhirContext, myMdmControllerSvc, myInterceptorBroadcaster);
084                                        myResourceProviderFactory.addSupplier(myMdmHistoryProviderSupplier);
085                                }
086                                break;
087                        default:
088                                throw new ConfigurationException(Msg.code(1497) + "MDM not supported for FHIR version "
089                                                + myFhirContext.getVersion().getVersion());
090                }
091        }
092
093        @PreDestroy
094        public void unloadProvider() {
095                if (myMdmProviderSupplier != null) {
096                        myResourceProviderFactory.removeSupplier(myMdmProviderSupplier);
097                }
098                if (myMdmHistoryProviderSupplier != null) {
099                        myResourceProviderFactory.removeSupplier(myMdmHistoryProviderSupplier);
100                }
101        }
102}