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