001/*-
002 * #%L
003 * HAPI FHIR Subscription Server
004 * %%
005 * Copyright (C) 2014 - 2025 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.jpa.cache.IResourceChangeListener;
024import ca.uhn.fhir.rest.server.util.ISearchParamRegistry;
025import ca.uhn.fhir.util.Logs;
026import ca.uhn.hapi.converters.canonical.VersionCanonicalizer;
027import jakarta.annotation.Nonnull;
028import org.hl7.fhir.instance.model.api.IBaseResource;
029import org.slf4j.Logger;
030
031import java.util.HashSet;
032import java.util.List;
033import java.util.Set;
034
035public abstract class BaseSubscriptionTopicLoader extends BaseResourceCacheSynchronizer
036                implements ISubscriptionTopicLoader, IResourceChangeListener {
037        private static final Logger ourLog = Logs.getSubscriptionTopicLog();
038
039        private final SubscriptionTopicRegistry mySubscriptionTopicRegistry;
040
041        protected final ISearchParamRegistry mySearchParamRegistry;
042
043        private final VersionCanonicalizer myVersionCanonicalizer;
044
045        /**
046         * Constructor
047         */
048        public BaseSubscriptionTopicLoader(
049                        VersionCanonicalizer theVersionCanonicalizer,
050                        String theResourceName,
051                        SubscriptionTopicRegistry theSubscriptionTopicRegistry,
052                        ISearchParamRegistry theSearchParamRegistry) {
053                super(theResourceName);
054                myVersionCanonicalizer = theVersionCanonicalizer;
055                mySubscriptionTopicRegistry = theSubscriptionTopicRegistry;
056                mySearchParamRegistry = theSearchParamRegistry;
057        }
058
059        @Override
060        public void handleInit(@Nonnull List<IBaseResource> resourceList) {
061                updateSubscriptionTopicRegistry(resourceList);
062        }
063
064        @Override
065        public int syncResourcesIntoCache(@Nonnull List<IBaseResource> resourceList) {
066                return updateSubscriptionTopicRegistry(resourceList);
067        }
068
069        protected int updateSubscriptionTopicRegistry(List<IBaseResource> theResourceList) {
070                Set<String> allIds = new HashSet<>();
071                int registeredCount = 0;
072
073                for (IBaseResource resource : theResourceList) {
074                        String nextId = resource.getIdElement().getIdPart();
075                        allIds.add(nextId);
076
077                        boolean registered =
078                                        mySubscriptionTopicRegistry.register(myVersionCanonicalizer.subscriptionTopicToCanonical(resource));
079                        if (registered) {
080                                registeredCount++;
081                        }
082                }
083
084                mySubscriptionTopicRegistry.unregisterAllIdsNotInCollection(allIds);
085                ourLog.debug("Finished syncing Subscription Topics - registered {}", registeredCount);
086                return registeredCount;
087        }
088}