View Javadoc
1   package ca.uhn.fhir.jpa.search;
2   
3   /*-
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.util.List;
24  
25  import org.hl7.fhir.instance.model.api.IBaseResource;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.transaction.PlatformTransactionManager;
29  import org.springframework.transaction.TransactionStatus;
30  import org.springframework.transaction.support.TransactionCallback;
31  import org.springframework.transaction.support.TransactionTemplate;
32  
33  import ca.uhn.fhir.jpa.dao.IDao;
34  import ca.uhn.fhir.jpa.dao.ISearchBuilder;
35  import ca.uhn.fhir.jpa.entity.Search;
36  import ca.uhn.fhir.jpa.entity.SearchStatusEnum;
37  import ca.uhn.fhir.jpa.search.SearchCoordinatorSvcImpl.SearchTask;
38  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
39  
40  public class PersistedJpaSearchFirstPageBundleProvider extends PersistedJpaBundleProvider {
41  	private static final Logger ourLog = LoggerFactory.getLogger(PersistedJpaSearchFirstPageBundleProvider.class);
42  	private SearchTask mySearchTask;
43  	private ISearchBuilder mySearchBuilder;
44  	private Search mySearch;
45  	private PlatformTransactionManager myTxManager;
46  
47  	public PersistedJpaSearchFirstPageBundleProvider(Search theSearch, IDao theDao, SearchTask theSearchTask, ISearchBuilder theSearchBuilder, PlatformTransactionManager theTxManager) {
48  		super(theSearch.getUuid(), theDao);
49  		setSearchEntity(theSearch);
50  		mySearchTask = theSearchTask;
51  		mySearchBuilder = theSearchBuilder;
52  		mySearch = theSearch;
53  		myTxManager = theTxManager;
54  	}
55  
56  	@Override
57  	public List<IBaseResource> getResources(int theFromIndex, int theToIndex) {
58  		SearchCoordinatorSvcImpl.verifySearchHasntFailedOrThrowInternalErrorException(mySearch);
59  
60  		ourLog.trace("Fetching search resource PIDs");
61  		final List<Long> pids = mySearchTask.getResourcePids(theFromIndex, theToIndex);
62  		ourLog.trace("Done fetching search resource PIDs");
63  
64  		TransactionTemplate txTemplate = new TransactionTemplate(myTxManager);
65  		txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
66  		List<IBaseResource> retVal = txTemplate.execute(new TransactionCallback<List<IBaseResource>>() {
67  			@Override
68  			public List<IBaseResource> doInTransaction(TransactionStatus theStatus) {
69  				return toResourceList(mySearchBuilder, pids);
70  			}
71  		});
72  
73  		ourLog.trace("Loaded resources to return");
74  
75  		return retVal;
76  	}
77  
78  	@Override
79  	public Integer size() {
80  		ourLog.trace("Waiting for initial sync");
81  		Integer size = mySearchTask.awaitInitialSync();
82  		ourLog.trace("Finished waiting for local sync");
83  
84  		SearchCoordinatorSvcImpl.verifySearchHasntFailedOrThrowInternalErrorException(mySearch);
85  		if (size != null) {
86  			return size;
87  		}
88  		return super.size();
89  	}
90  
91  }