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