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