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.interceptor.model.RequestPartitionId;
024import ca.uhn.fhir.jpa.mdm.svc.candidate.MdmCandidateSearchSvc;
025import ca.uhn.fhir.mdm.api.IMdmMatchFinderSvc;
026import ca.uhn.fhir.mdm.api.MatchedTarget;
027import ca.uhn.fhir.mdm.log.Logs;
028import ca.uhn.fhir.mdm.rules.svc.MdmResourceMatcherSvc;
029import org.hl7.fhir.instance.model.api.IAnyResource;
030import org.slf4j.Logger;
031import org.springframework.beans.factory.annotation.Autowired;
032import org.springframework.stereotype.Service;
033
034import javax.annotation.Nonnull;
035import org.springframework.transaction.annotation.Transactional;
036import java.util.Collection;
037import java.util.List;
038import java.util.stream.Collectors;
039
040import static ca.uhn.fhir.jpa.mdm.svc.candidate.CandidateSearcher.idOrType;
041
042@Service
043public class MdmMatchFinderSvcImpl implements IMdmMatchFinderSvc {
044
045        private static final Logger ourLog = Logs.getMdmTroubleshootingLog();
046
047        @Autowired
048        private MdmCandidateSearchSvc myMdmCandidateSearchSvc;
049        @Autowired
050        private MdmResourceMatcherSvc myMdmResourceMatcherSvc;
051
052        @Override
053        @Nonnull
054        @Transactional
055        public List<MatchedTarget> getMatchedTargets(String theResourceType, IAnyResource theResource, RequestPartitionId theRequestPartitionId) {
056                Collection<IAnyResource> targetCandidates = myMdmCandidateSearchSvc.findCandidates(theResourceType, theResource, theRequestPartitionId);
057
058                List<MatchedTarget> matches = targetCandidates.stream()
059                        .map(candidate -> new MatchedTarget(candidate, myMdmResourceMatcherSvc.getMatchResult(theResource, candidate)))
060                        .collect(Collectors.toList());
061
062                ourLog.trace("Found {} matched targets for {}.", matches.size(), idOrType(theResource, theResourceType));
063                return matches;
064        }
065
066}