001/*
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2023 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.search;
021
022import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
023import ca.uhn.fhir.jpa.dao.SearchBuilderFactory;
024import ca.uhn.fhir.jpa.partition.IRequestPartitionHelperSvc;
025import ca.uhn.fhir.rest.api.server.IBundleProvider;
026import ca.uhn.fhir.rest.api.server.RequestDetails;
027import ca.uhn.fhir.rest.server.BasePagingProvider;
028import org.springframework.beans.factory.annotation.Autowired;
029
030import javax.annotation.Nullable;
031
032// Note: this class is not annotated with @Service because we want to
033// explicitly define it in BaseConfig.java. This is done so that
034// implementors can override if they want to.
035public class DatabaseBackedPagingProvider extends BasePagingProvider {
036
037        @Autowired
038        private DaoRegistry myDaoRegistry;
039
040        @Autowired
041        private SearchBuilderFactory mySearchBuilderFactory;
042
043        @Autowired
044        private PersistedJpaBundleProviderFactory myPersistedJpaBundleProviderFactory;
045
046        @Autowired
047        private IRequestPartitionHelperSvc myRequestPartitionHelperSvc;
048
049        /**
050         * Constructor
051         */
052        public DatabaseBackedPagingProvider() {
053                super();
054        }
055
056        /**
057         * Constructor
058         *
059         * @deprecated Use {@link DatabaseBackedPagingProvider} as this constructor has no purpose
060         */
061        @Deprecated
062        public DatabaseBackedPagingProvider(int theSize) {
063                this();
064        }
065
066        @Override
067        public synchronized IBundleProvider retrieveResultList(RequestDetails theRequestDetails, String theId) {
068                PersistedJpaBundleProvider provider = myPersistedJpaBundleProviderFactory.newInstance(theRequestDetails, theId);
069                return validateAndReturnBundleProvider(provider);
070        }
071
072        /**
073         * Subclasses may override in order to modify the bundle provider being returned
074         */
075        @Nullable
076        protected PersistedJpaBundleProvider validateAndReturnBundleProvider(PersistedJpaBundleProvider theBundleProvider) {
077                if (!theBundleProvider.ensureSearchEntityLoaded()) {
078                        return null;
079                }
080                return theBundleProvider;
081        }
082
083        @Override
084        public synchronized String storeResultList(RequestDetails theRequestDetails, IBundleProvider theList) {
085                String uuid = theList.getUuid();
086                return uuid;
087        }
088}