001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2024 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.search.SearchStatusEnum;
028import ca.uhn.fhir.jpa.search.builder.tasks.SearchTask;
029import ca.uhn.fhir.rest.api.server.IBundleProvider;
030import ca.uhn.fhir.rest.api.server.RequestDetails;
031import ca.uhn.fhir.rest.param.HistorySearchStyleEnum;
032import org.springframework.beans.factory.annotation.Autowired;
033import org.springframework.context.ApplicationContext;
034
035import java.util.Date;
036import java.util.UUID;
037
038import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
039
040public class PersistedJpaBundleProviderFactory {
041
042        @Autowired
043        private ApplicationContext myApplicationContext;
044
045        public PersistedJpaBundleProvider newInstance(RequestDetails theRequest, String theUuid) {
046                Object retVal = myApplicationContext.getBean(JpaConfig.PERSISTED_JPA_BUNDLE_PROVIDER, theRequest, theUuid);
047                return (PersistedJpaBundleProvider) retVal;
048        }
049
050        public PersistedJpaBundleProvider newInstance(RequestDetails theRequest, Search theSearch) {
051                Object retVal =
052                                myApplicationContext.getBean(JpaConfig.PERSISTED_JPA_BUNDLE_PROVIDER_BY_SEARCH, theRequest, theSearch);
053                return (PersistedJpaBundleProvider) retVal;
054        }
055
056        public PersistedJpaSearchFirstPageBundleProvider newInstanceFirstPage(
057                        RequestDetails theRequestDetails,
058                        SearchTask theTask,
059                        ISearchBuilder theSearchBuilder,
060                        RequestPartitionId theRequestPartitionId) {
061                return (PersistedJpaSearchFirstPageBundleProvider) myApplicationContext.getBean(
062                                JpaConfig.PERSISTED_JPA_SEARCH_FIRST_PAGE_BUNDLE_PROVIDER,
063                                theRequestDetails,
064                                theTask,
065                                theSearchBuilder,
066                                theRequestPartitionId);
067        }
068
069        public IBundleProvider history(
070                        RequestDetails theRequest,
071                        String theResourceType,
072                        Long theResourcePid,
073                        Date theRangeStartInclusive,
074                        Date theRangeEndInclusive,
075                        Integer theOffset,
076                        RequestPartitionId theRequestPartitionId) {
077                return history(
078                                theRequest,
079                                theResourceType,
080                                theResourcePid,
081                                theRangeStartInclusive,
082                                theRangeEndInclusive,
083                                theOffset,
084                                null,
085                                theRequestPartitionId);
086        }
087
088        public IBundleProvider history(
089                        RequestDetails theRequest,
090                        String theResourceType,
091                        Long theResourcePid,
092                        Date theRangeStartInclusive,
093                        Date theRangeEndInclusive,
094                        Integer theOffset,
095                        HistorySearchStyleEnum searchParameterType,
096                        RequestPartitionId theRequestPartitionId) {
097                String resourceName = defaultIfBlank(theResourceType, null);
098
099                Search search = new Search();
100                search.setOffset(theOffset);
101                search.setDeleted(false);
102                search.setCreated(new Date());
103                search.setLastUpdated(theRangeStartInclusive, theRangeEndInclusive);
104                search.setUuid(UUID.randomUUID().toString());
105                search.setResourceType(resourceName);
106                search.setResourceId(theResourcePid);
107                search.setSearchType(SearchTypeEnum.HISTORY);
108                search.setStatus(SearchStatusEnum.FINISHED);
109                search.setHistorySearchStyle(searchParameterType);
110
111                PersistedJpaBundleProvider provider = newInstance(theRequest, search);
112                provider.setRequestPartitionId(theRequestPartitionId);
113
114                return provider;
115        }
116}