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.svc.IIdHelperService;
025import ca.uhn.fhir.i18n.Msg;
026import ca.uhn.fhir.mdm.api.IMdmSettings;
027import ca.uhn.fhir.mdm.log.Logs;
028import ca.uhn.fhir.mdm.rules.json.MdmFilterSearchParamJson;
029import ca.uhn.fhir.mdm.rules.json.MdmResourceSearchParamJson;
030import ca.uhn.fhir.rest.api.server.IBundleProvider;
031import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId;
032import org.hl7.fhir.instance.model.api.IAnyResource;
033import org.hl7.fhir.instance.model.api.IBaseResource;
034import org.slf4j.Logger;
035import org.springframework.beans.factory.annotation.Autowired;
036import org.springframework.stereotype.Service;
037import org.springframework.transaction.annotation.Transactional;
038
039import java.util.Collection;
040import java.util.Collections;
041import java.util.HashMap;
042import java.util.List;
043import java.util.Map;
044import java.util.Optional;
045import java.util.stream.Collectors;
046
047import static ca.uhn.fhir.jpa.mdm.svc.candidate.CandidateSearcher.idOrType;
048import static ca.uhn.fhir.mdm.api.MdmConstants.ALL_RESOURCE_SEARCH_PARAM_TYPE;
049
050@Service
051public class MdmCandidateSearchSvc {
052
053        private static final Logger ourLog = Logs.getMdmTroubleshootingLog();
054
055        @Autowired
056        private IMdmSettings myMdmSettings;
057        @Autowired
058        private IIdHelperService myIdHelperService;
059        @Autowired
060        private MdmCandidateSearchCriteriaBuilderSvc myMdmCandidateSearchCriteriaBuilderSvc;
061        @Autowired
062        private CandidateSearcher myCandidateSearcher;
063
064        public MdmCandidateSearchSvc() {
065        }
066
067        /**
068         * Given a source resource, search for all resources that are considered an MDM match based on defined MDM rules.
069         *
070         * @param theResourceType the resource type of the resource being matched
071         * @param theResource the {@link IBaseResource} we are attempting to match.
072         * @param theRequestPartitionId  the {@link RequestPartitionId} representation of the partitions we are limited to when attempting to match
073         *
074         * @return the list of candidate {@link IBaseResource} which could be matches to theResource
075         */
076        @Transactional
077        public Collection<IAnyResource> findCandidates(String theResourceType, IAnyResource theResource, RequestPartitionId theRequestPartitionId) {
078                Map<ResourcePersistentId, IAnyResource> matchedPidsToResources = new HashMap<>();
079                List<MdmFilterSearchParamJson> filterSearchParams = myMdmSettings.getMdmRules().getCandidateFilterSearchParams();
080                List<String> filterCriteria = buildFilterQuery(filterSearchParams, theResourceType);
081                List<MdmResourceSearchParamJson> candidateSearchParams = myMdmSettings.getMdmRules().getCandidateSearchParams();
082
083                //If there are zero MdmResourceSearchParamJson, we end up only making a single search, otherwise we
084                //must perform one search per MdmResourceSearchParamJson.
085                if (candidateSearchParams.isEmpty()) {
086                        searchForIdsAndAddToMap(theResourceType, theResource, matchedPidsToResources, filterCriteria, null, theRequestPartitionId);
087                } else {
088                        for (MdmResourceSearchParamJson resourceSearchParam : candidateSearchParams) {
089
090                                if (!isSearchParamForResource(theResourceType, resourceSearchParam)) {
091                                        continue;
092                                }
093
094                                searchForIdsAndAddToMap(theResourceType, theResource, matchedPidsToResources, filterCriteria, resourceSearchParam, theRequestPartitionId);
095                        }
096                }
097                // Obviously we don't want to consider the incoming resource as a potential candidate.
098                // Sometimes, we are running this function on a resource that has not yet been persisted,
099                // so it may not have an ID yet, precluding the need to remove it.
100                if (theResource.getIdElement().getIdPart() != null) {
101                        if (matchedPidsToResources.remove(myIdHelperService.getPidOrNull(RequestPartitionId.allPartitions(), theResource)) != null) {
102                                ourLog.debug("Removing incoming resource {} from list of candidates.", theResource.getIdElement().toUnqualifiedVersionless());
103                        }
104                }
105
106                ourLog.info("Candidate search found {} matching resources for {}", matchedPidsToResources.size(), idOrType(theResource, theResourceType));
107                return matchedPidsToResources.values();
108        }
109
110        private boolean isSearchParamForResource(String theResourceType, MdmResourceSearchParamJson resourceSearchParam) {
111                String resourceType = resourceSearchParam.getResourceType();
112                return resourceType.equals(theResourceType) || resourceType.equalsIgnoreCase(ALL_RESOURCE_SEARCH_PARAM_TYPE);
113        }
114
115        /*
116         * Helper method which performs too much work currently.
117         * 1. Build a full query string for the given filter and resource criteria.
118         * 2. Convert that URL to a SearchParameterMap.
119         * 3. Execute a Synchronous search on the DAO using that parameter map.
120         * 4. Store all results in `theMatchedPidsToResources`
121         */
122        @SuppressWarnings("rawtypes")
123        private void searchForIdsAndAddToMap(String theResourceType, IAnyResource theResource, Map<ResourcePersistentId, IAnyResource> theMatchedPidsToResources, List<String> theFilterCriteria, MdmResourceSearchParamJson resourceSearchParam, RequestPartitionId theRequestPartitionId) {
124                //1.
125                Optional<String> oResourceCriteria = myMdmCandidateSearchCriteriaBuilderSvc.buildResourceQueryString(theResourceType, theResource, theFilterCriteria, resourceSearchParam);
126                if (!oResourceCriteria.isPresent()) {
127                        return;
128                }
129                String resourceCriteria = oResourceCriteria.get();
130                ourLog.debug("Searching for {} candidates with {}", theResourceType, resourceCriteria);
131
132                //2.
133                Optional<IBundleProvider> bundleProvider = myCandidateSearcher.search(theResourceType, resourceCriteria, theRequestPartitionId);
134                if (!bundleProvider.isPresent()) {
135                        throw new TooManyCandidatesException(Msg.code(762) + "More than " + myMdmSettings.getCandidateSearchLimit() + " candidate matches found for " + resourceCriteria + ".  Aborting mdm matching.");
136                }
137                List<IBaseResource> resources = bundleProvider.get().getAllResources();
138
139                int initialSize = theMatchedPidsToResources.size();
140
141                //4.
142                resources.forEach(resource -> theMatchedPidsToResources.put(myIdHelperService.getPidOrNull(RequestPartitionId.allPartitions(), resource), (IAnyResource) resource));
143
144                int newSize = theMatchedPidsToResources.size();
145
146                if (ourLog.isDebugEnabled()) {
147                        ourLog.debug("Candidate search added {} {}s", newSize - initialSize, theResourceType);
148                }
149        }
150
151        private List<String> buildFilterQuery(List<MdmFilterSearchParamJson> theFilterSearchParams, String theResourceType) {
152                return Collections.unmodifiableList(theFilterSearchParams.stream()
153                        .filter(spFilterJson -> paramIsOnCorrectType(theResourceType, spFilterJson))
154                        .map(this::convertToQueryString)
155                        .collect(Collectors.toList()));
156        }
157
158        private boolean paramIsOnCorrectType(String theResourceType, MdmFilterSearchParamJson spFilterJson) {
159                return spFilterJson.getResourceType().equals(theResourceType) || spFilterJson.getResourceType().equalsIgnoreCase(ALL_RESOURCE_SEARCH_PARAM_TYPE);
160        }
161
162        private String convertToQueryString(MdmFilterSearchParamJson theSpFilterJson) {
163                String qualifier = theSpFilterJson.getTokenParamModifierAsString();
164                return theSpFilterJson.getSearchParam() + qualifier + "=" + theSpFilterJson.getFixedValue();
165        }
166}