001package ca.uhn.fhir.jpa.mdm.svc.candidate;
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.interceptor.model.RequestPartitionId;
024import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
025import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
026import ca.uhn.fhir.mdm.svc.MdmSearchParamSvc;
027import ca.uhn.fhir.jpa.partition.SystemRequestDetails;
028import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
029import ca.uhn.fhir.mdm.api.IMdmSettings;
030import ca.uhn.fhir.rest.api.server.IBundleProvider;
031import org.hl7.fhir.instance.model.api.IAnyResource;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034import org.springframework.beans.factory.annotation.Autowired;
035
036import java.util.Optional;
037
038public class CandidateSearcher {
039        private static final Logger ourLog = LoggerFactory.getLogger(CandidateSearcher.class);
040        private final DaoRegistry myDaoRegistry;
041        private final IMdmSettings myMdmSettings;
042        private final MdmSearchParamSvc myMdmSearchParamSvc;
043
044        @Autowired
045        public CandidateSearcher(DaoRegistry theDaoRegistry, IMdmSettings theMdmSettings, MdmSearchParamSvc theMdmSearchParamSvc) {
046                myDaoRegistry = theDaoRegistry;
047                myMdmSettings = theMdmSettings;
048                myMdmSearchParamSvc = theMdmSearchParamSvc;
049        }
050
051        /**
052         * Perform a search for mdm candidates.
053         *
054         * @param theResourceType     the type of resources searched on
055         * @param theResourceCriteria the criteria used to search for the candidates
056         * @param partitionId         the partition for the search
057         * @return Optional.empty() if >= IMdmSettings.getCandidateSearchLimit() candidates are found, otherwise
058         * return the bundle provider for the search results.
059         */
060        public Optional<IBundleProvider> search(String theResourceType, String theResourceCriteria, RequestPartitionId partitionId) {
061                SearchParameterMap searchParameterMap = myMdmSearchParamSvc.mapFromCriteria(theResourceType, theResourceCriteria);
062
063                searchParameterMap.setLoadSynchronousUpTo(myMdmSettings.getCandidateSearchLimit());
064
065                IFhirResourceDao<?> resourceDao = myDaoRegistry.getResourceDao(theResourceType);
066                SystemRequestDetails systemRequestDetails = new SystemRequestDetails();
067                systemRequestDetails.setRequestPartitionId(partitionId);
068                IBundleProvider retval = resourceDao.search(searchParameterMap, systemRequestDetails);
069
070                if (retval.size() != null && retval.size() >= myMdmSettings.getCandidateSearchLimit()) {
071                        return Optional.empty();
072                }
073                return Optional.of(retval);
074        }
075
076        /**
077         * Perform a search for mdm candidates.
078         *
079         * @param theResourceType     the type of resources searched on
080         * @param theResourceCriteria the criteria used to search for the candidates
081         * @return Optional.empty() if >= IMdmSettings.getCandidateSearchLimit() candidates are found, otherwise
082         * return the bundle provider for the search results.
083         */
084        public Optional<IBundleProvider> search(String theResourceType, String theResourceCriteria) {
085                return this.search(theResourceType, theResourceCriteria, RequestPartitionId.allPartitions());
086        }
087
088        public static String idOrType(IAnyResource theResource, String theResourceType) {
089                if (theResource.getIdElement() == null) {
090                        return theResourceType;
091                }
092                return theResource.getIdElement().toUnqualifiedVersionless().toString();
093        }
094}