001package ca.uhn.fhir.jpa.mdm.svc; 002 003/*- 004 * #%L 005 * HAPI FHIR JPA Server - Master Data Management 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.context.FhirContext; 024import ca.uhn.fhir.context.RuntimeResourceDefinition; 025import ca.uhn.fhir.interceptor.model.RequestPartitionId; 026import ca.uhn.fhir.jpa.api.dao.DaoRegistry; 027import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; 028import ca.uhn.fhir.jpa.api.pid.HomogeneousResourcePidList; 029import ca.uhn.fhir.jpa.api.pid.IResourcePidList; 030import ca.uhn.fhir.jpa.api.svc.IGoldenResourceSearchSvc; 031import ca.uhn.fhir.jpa.partition.SystemRequestDetails; 032import ca.uhn.fhir.jpa.searchparam.MatchUrlService; 033import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; 034import ca.uhn.fhir.mdm.api.MdmConstants; 035import ca.uhn.fhir.rest.api.Constants; 036import ca.uhn.fhir.rest.api.SortOrderEnum; 037import ca.uhn.fhir.rest.api.SortSpec; 038import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId; 039import ca.uhn.fhir.rest.param.DateRangeParam; 040import ca.uhn.fhir.rest.param.TokenParam; 041import ca.uhn.fhir.util.DateRangeUtil; 042import org.springframework.beans.factory.annotation.Autowired; 043import org.springframework.transaction.annotation.Transactional; 044 045import javax.annotation.Nonnull; 046import javax.annotation.Nullable; 047import java.util.Date; 048import java.util.List; 049 050public class GoldenResourceSearchSvcImpl implements IGoldenResourceSearchSvc { 051 @Autowired 052 private MatchUrlService myMatchUrlService; 053 054 @Autowired 055 private DaoRegistry myDaoRegistry; 056 057 @Autowired 058 private FhirContext myFhirContext; 059 060 @Override 061 @Transactional 062 public IResourcePidList fetchGoldenResourceIdsPage(Date theStart, Date theEnd, @Nonnull Integer thePageSize, @Nullable RequestPartitionId theRequestPartitionId, @Nonnull String theResourceType) { 063 return fetchResourceIdsPageWithResourceType(theStart, theEnd, thePageSize, theResourceType, theRequestPartitionId); 064 } 065 066 private IResourcePidList fetchResourceIdsPageWithResourceType(Date theStart, Date theEnd, int thePageSize, String theResourceType, RequestPartitionId theRequestPartitionId) { 067 068 RuntimeResourceDefinition def = myFhirContext.getResourceDefinition(theResourceType); 069 070 SearchParameterMap searchParamMap = myMatchUrlService.translateMatchUrl(theResourceType, def); 071 searchParamMap.setSort(new SortSpec(Constants.PARAM_LASTUPDATED, SortOrderEnum.ASC)); 072 DateRangeParam chunkDateRange = DateRangeUtil.narrowDateRange(searchParamMap.getLastUpdated(), theStart, theEnd); 073 searchParamMap.setLastUpdated(chunkDateRange); 074 searchParamMap.setCount(thePageSize); // request this many pids 075 searchParamMap.add("_tag", new TokenParam(MdmConstants.SYSTEM_GOLDEN_RECORD_STATUS, MdmConstants.CODE_GOLDEN_RECORD)); 076 077 IFhirResourceDao<?> dao = myDaoRegistry.getResourceDao(theResourceType); 078 SystemRequestDetails request = new SystemRequestDetails(); 079 request.setRequestPartitionId(theRequestPartitionId); 080 List<ResourcePersistentId> ids = dao.searchForIds(searchParamMap, request); 081 082 Date lastDate = null; 083 if (ids.size() > 0) { 084 lastDate = dao.readByPid(ids.get(ids.size() - 1)).getMeta().getLastUpdated(); 085 } 086 087 return new HomogeneousResourcePidList(theResourceType, ids, lastDate); 088 } 089}