001/*-
002 * #%L
003 * HAPI FHIR JPA 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.search;
021
022import ca.uhn.fhir.interceptor.model.RequestPartitionId;
023import ca.uhn.fhir.jpa.config.JpaConfig;
024import ca.uhn.fhir.jpa.dao.ISearchBuilder;
025import ca.uhn.fhir.jpa.entity.Search;
026import ca.uhn.fhir.jpa.entity.SearchTypeEnum;
027import ca.uhn.fhir.jpa.model.dao.JpaPid;
028import ca.uhn.fhir.jpa.model.search.SearchStatusEnum;
029import ca.uhn.fhir.jpa.search.builder.tasks.SearchTask;
030import ca.uhn.fhir.rest.api.server.IBundleProvider;
031import ca.uhn.fhir.rest.api.server.RequestDetails;
032import ca.uhn.fhir.rest.param.HistorySearchStyleEnum;
033import jakarta.annotation.Nonnull;
034import jakarta.annotation.Nullable;
035import org.springframework.beans.factory.annotation.Autowired;
036import org.springframework.context.ApplicationContext;
037
038import java.util.Date;
039import java.util.List;
040import java.util.UUID;
041
042import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
043
044public class PersistedJpaBundleProviderFactory {
045
046        @Autowired
047        private ApplicationContext myApplicationContext;
048
049        public PersistedJpaBundleProvider newInstance(RequestDetails theRequest, String theUuid) {
050                Object retVal = myApplicationContext.getBean(JpaConfig.PERSISTED_JPA_BUNDLE_PROVIDER, theRequest, theUuid);
051                return (PersistedJpaBundleProvider) retVal;
052        }
053
054        public PersistedJpaBundleProvider newInstance(RequestDetails theRequest, Search theSearch) {
055                Object retVal =
056                                myApplicationContext.getBean(JpaConfig.PERSISTED_JPA_BUNDLE_PROVIDER_BY_SEARCH, theRequest, theSearch);
057                return (PersistedJpaBundleProvider) retVal;
058        }
059
060        public PersistedJpaSearchFirstPageBundleProvider newInstanceFirstPage(
061                        RequestDetails theRequestDetails,
062                        SearchTask theTask,
063                        ISearchBuilder<JpaPid> theSearchBuilder,
064                        RequestPartitionId theRequestPartitionId) {
065                return (PersistedJpaSearchFirstPageBundleProvider) myApplicationContext.getBean(
066                                JpaConfig.PERSISTED_JPA_SEARCH_FIRST_PAGE_BUNDLE_PROVIDER,
067                                theRequestDetails,
068                                theTask,
069                                theSearchBuilder,
070                                theRequestPartitionId);
071        }
072
073        public IBundleProvider history(
074                        RequestDetails theRequest,
075                        String theResourceType,
076                        @Nullable JpaPid theResourcePid,
077                        Date theRangeStartInclusive,
078                        Date theRangeEndInclusive,
079                        Integer theOffset,
080                        RequestPartitionId theRequestPartitionId) {
081                return history(
082                                theRequest,
083                                theResourceType,
084                                theResourcePid,
085                                theRangeStartInclusive,
086                                theRangeEndInclusive,
087                                theOffset,
088                                null,
089                                theRequestPartitionId);
090        }
091
092        public IBundleProvider history(
093                        RequestDetails theRequest,
094                        String theResourceType,
095                        @Nullable JpaPid theResourcePid,
096                        Date theRangeStartInclusive,
097                        Date theRangeEndInclusive,
098                        Integer theOffset,
099                        HistorySearchStyleEnum searchParameterType,
100                        RequestPartitionId theRequestPartitionId) {
101                String resourceName = defaultIfBlank(theResourceType, null);
102
103                Search search = new Search();
104                search.setOffset(theOffset);
105                search.setDeleted(false);
106                search.setCreated(new Date());
107                search.setLastUpdated(theRangeStartInclusive, theRangeEndInclusive);
108                search.setUuid(UUID.randomUUID().toString());
109                search.setResourceType(resourceName);
110                search.setResourceId(theResourcePid);
111                search.setSearchType(SearchTypeEnum.HISTORY);
112                search.setStatus(SearchStatusEnum.FINISHED);
113                search.setHistorySearchStyle(searchParameterType);
114
115                PersistedJpaBundleProvider provider = newInstance(theRequest, search);
116                provider.setRequestPartitionId(theRequestPartitionId);
117
118                return provider;
119        }
120
121        /**
122         * Create an unlimited history bundle provider for bulk export operations
123         * that can paginate through history entries for specific resource IDs.
124         */
125        public IBundleProvider historyFromResourceIds(
126                        String theResourceType,
127                        @Nullable List<String> theResourceIds,
128                        RequestPartitionId theRequestPartitionId,
129                        @Nullable Date theRangeStartInclusive,
130                        @Nonnull Date theRangeEndInclusive) {
131
132                return (PersistedJpaIdSearchBundleProvider) myApplicationContext.getBean(
133                                JpaConfig.PERSISTED_JPA_ID_SEARCH_BUNDLE_PROVIDER,
134                                theResourceType,
135                                theResourceIds,
136                                theRequestPartitionId,
137                                theRangeStartInclusive,
138                                theRangeEndInclusive);
139        }
140}