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("SubscriptionTopic", "status") != null) {
075                        map.add(SubscriptionTopic.SP_STATUS, new TokenParam(null, Enumerations.PublicationStatus.ACTIVE.toCode()));
076                }
077                map.setLoadSynchronousUpTo(SubscriptionConstants.MAX_SUBSCRIPTION_RESULTS);
078                return map;
079        }
080
081        @Override
082        protected void handleInit(List<IBaseResource> resourceList) {
083                updateSubscriptionTopicRegistry(resourceList);
084        }
085
086        @Override
087        protected int syncResourcesIntoCache(List<IBaseResource> resourceList) {
088                return updateSubscriptionTopicRegistry(resourceList);
089        }
090
091        private int updateSubscriptionTopicRegistry(List<IBaseResource> theResourceList) {
092                Set<String> allIds = new HashSet<>();
093                int registeredCount = 0;
094
095                for (IBaseResource resource : theResourceList) {
096                        String nextId = resource.getIdElement().getIdPart();
097                        allIds.add(nextId);
098
099                        boolean registered = mySubscriptionTopicRegistry.register(normalizeToR5(resource));
100                        if (registered) {
101                                registeredCount++;
102                        }
103                }
104
105                mySubscriptionTopicRegistry.unregisterAllIdsNotInCollection(allIds);
106                ourLog.debug("Finished sync subscription topics - registered {}", registeredCount);
107                return registeredCount;
108        }
109
110        private SubscriptionTopic normalizeToR5(IBaseResource theResource) {
111                if (theResource instanceof SubscriptionTopic) {
112                        return (SubscriptionTopic) theResource;
113                } else if (theResource instanceof org.hl7.fhir.r4b.model.SubscriptionTopic) {
114                        return SubscriptionTopicCanonicalizer.canonicalizeTopic(myFhirContext, theResource);
115                } else {
116                        throw new IllegalArgumentException(Msg.code(2332)
117                                        + "Only R4B and R5 SubscriptionTopic is currently supported.  Found " + theResource.getClass());
118                }
119        }
120}