001/*-
002 * #%L
003 * HAPI FHIR Subscription Server
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.jpa.topic;
021
022import ca.uhn.fhir.cache.BaseResourceCacheSynchronizer;
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.context.FhirVersionEnum;
025import ca.uhn.fhir.i18n.Msg;
026import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
027import ca.uhn.fhir.rest.param.TokenParam;
028import ca.uhn.fhir.rest.server.util.ISearchParamRegistry;
029import ca.uhn.fhir.subscription.SubscriptionConstants;
030import ca.uhn.fhir.util.Logs;
031import jakarta.annotation.Nonnull;
032import org.hl7.fhir.instance.model.api.IBaseResource;
033import org.hl7.fhir.r5.model.Enumerations;
034import org.hl7.fhir.r5.model.SubscriptionTopic;
035import org.slf4j.Logger;
036import org.springframework.beans.factory.annotation.Autowired;
037
038import java.util.HashSet;
039import java.util.List;
040import java.util.Set;
041
042public class SubscriptionTopicLoader extends BaseResourceCacheSynchronizer {
043        private static final Logger ourLog = Logs.getSubscriptionTopicLog();
044
045        @Autowired
046        private FhirContext myFhirContext;
047
048        @Autowired
049        private SubscriptionTopicRegistry mySubscriptionTopicRegistry;
050
051        @Autowired
052        protected ISearchParamRegistry mySearchParamRegistry;
053
054        /**
055         * Constructor
056         */
057        public SubscriptionTopicLoader() {
058                super("SubscriptionTopic");
059        }
060
061        @Override
062        public void registerListener() {
063                if (!myFhirContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.R4B)) {
064                        return;
065                }
066                super.registerListener();
067        }
068
069        @Override
070        @Nonnull
071        protected SearchParameterMap getSearchParameterMap() {
072                SearchParameterMap map = new SearchParameterMap();
073
074                if (mySearchParamRegistry.getActiveSearchParam(
075                                                "SubscriptionTopic", "status", ISearchParamRegistry.SearchParamLookupContextEnum.ALL)
076                                != null) {
077                        map.add(SubscriptionTopic.SP_STATUS, new TokenParam(null, Enumerations.PublicationStatus.ACTIVE.toCode()));
078                }
079                map.setLoadSynchronousUpTo(SubscriptionConstants.MAX_SUBSCRIPTION_RESULTS);
080                return map;
081        }
082
083        @Override
084        protected void handleInit(List<IBaseResource> resourceList) {
085                updateSubscriptionTopicRegistry(resourceList);
086        }
087
088        @Override
089        protected int syncResourcesIntoCache(List<IBaseResource> resourceList) {
090                return updateSubscriptionTopicRegistry(resourceList);
091        }
092
093        private int updateSubscriptionTopicRegistry(List<IBaseResource> theResourceList) {
094                Set<String> allIds = new HashSet<>();
095                int registeredCount = 0;
096
097                for (IBaseResource resource : theResourceList) {
098                        String nextId = resource.getIdElement().getIdPart();
099                        allIds.add(nextId);
100
101                        boolean registered = mySubscriptionTopicRegistry.register(normalizeToR5(resource));
102                        if (registered) {
103                                registeredCount++;
104                        }
105                }
106
107                mySubscriptionTopicRegistry.unregisterAllIdsNotInCollection(allIds);
108                ourLog.debug("Finished sync subscription topics - registered {}", registeredCount);
109                return registeredCount;
110        }
111
112        private SubscriptionTopic normalizeToR5(IBaseResource theResource) {
113                if (theResource instanceof SubscriptionTopic) {
114                        return (SubscriptionTopic) theResource;
115                } else if (theResource instanceof org.hl7.fhir.r4b.model.SubscriptionTopic) {
116                        return SubscriptionTopicCanonicalizer.canonicalizeTopic(myFhirContext, theResource);
117                } else {
118                        throw new IllegalArgumentException(Msg.code(2332)
119                                        + "Only R4B and R5 SubscriptionTopic is currently supported.  Found " + theResource.getClass());
120                }
121        }
122}