001package ca.uhn.fhir.jpa.batch.reader;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.interceptor.model.RequestPartitionId;
024import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
025import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
026import ca.uhn.fhir.jpa.dao.IResultIterator;
027import ca.uhn.fhir.jpa.dao.ISearchBuilder;
028import ca.uhn.fhir.jpa.dao.SearchBuilderFactory;
029import ca.uhn.fhir.jpa.model.search.SearchRuntimeDetails;
030import ca.uhn.fhir.jpa.partition.SystemRequestDetails;
031import ca.uhn.fhir.jpa.searchparam.ResourceSearch;
032import org.springframework.beans.factory.annotation.Autowired;
033
034import javax.annotation.Nonnull;
035import java.util.UUID;
036
037/**
038 * This service is used by batch processes to search resources
039 */
040public class BatchResourceSearcher {
041        @Autowired
042        private SearchBuilderFactory mySearchBuilderFactory;
043        @Autowired
044        private DaoRegistry myDaoRegistry;
045
046        public IResultIterator performSearch(ResourceSearch theResourceSearch, Integer theBatchSize) {
047                String resourceName = theResourceSearch.getResourceName();
048                RequestPartitionId requestPartitionId = theResourceSearch.getRequestPartitionId();
049
050                IFhirResourceDao<?> dao = myDaoRegistry.getResourceDao(resourceName);
051                final ISearchBuilder sb = mySearchBuilderFactory.newSearchBuilder(dao, resourceName, theResourceSearch.getResourceType());
052                sb.setFetchSize(theBatchSize);
053                SystemRequestDetails requestDetails = buildSystemRequestDetails(requestPartitionId);
054                SearchRuntimeDetails searchRuntimeDetails = new SearchRuntimeDetails(requestDetails, UUID.randomUUID().toString());
055                IResultIterator resultIter = sb.createQuery(theResourceSearch.getSearchParameterMap(), searchRuntimeDetails, requestDetails, requestPartitionId);
056                return resultIter;
057        }
058
059        @Nonnull
060        private SystemRequestDetails buildSystemRequestDetails(RequestPartitionId theRequestPartitionId) {
061                SystemRequestDetails retval = new SystemRequestDetails();
062                retval.setRequestPartitionId(theRequestPartitionId);
063                return retval;
064        }
065}