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.jpa.mdm.svc.MdmResourceDaoSvc; 024import ca.uhn.fhir.mdm.log.Logs; 025import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId; 026import org.hl7.fhir.instance.model.api.IAnyResource; 027import org.hl7.fhir.instance.model.api.IBaseResource; 028import org.slf4j.Logger; 029import org.springframework.beans.factory.annotation.Autowired; 030import org.springframework.stereotype.Service; 031 032@Service 033public class MdmGoldenResourceFindingSvc { 034 035 private static final Logger ourLog = Logs.getMdmTroubleshootingLog(); 036 037 @Autowired 038 private MdmResourceDaoSvc myMdmResourceDaoSvc; 039 040 @Autowired 041 private FindCandidateByEidSvc myFindCandidateByEidSvc; 042 043 @Autowired 044 private FindCandidateByLinkSvc myFindCandidateByLinkSvc; 045 046 @Autowired 047 private FindCandidateByExampleSvc myFindCandidateByExampleSvc; 048 049 /** 050 * Given an incoming IBaseResource, limited to the supported MDM type, return a list of {@link MatchedGoldenResourceCandidate} 051 * indicating possible candidates for a matching Golden Resource. Uses several separate methods for finding candidates: 052 * <p> 053 * 0. First, check the incoming Resource for an EID. If it is present, and we can find a Golden Resource with this EID, it automatically matches. 054 * 1. First, check link table for any entries where this baseresource is the source of a Golden Resource. If found, return. 055 * 2. If none are found, attempt to find Golden Resources which link to this theResource. 056 * 3. If none are found, attempt to find Golden Resources similar to our incoming resource based on the MDM rules and field matchers. 057 * 4. If none are found, attempt to find Golden Resources that are linked to sources that are similar to our incoming resource based on the MDM rules and 058 * field matchers. 059 * 060 * @param theResource the {@link IBaseResource} we are attempting to find matching candidate Golden Resources for. 061 * @return A list of {@link MatchedGoldenResourceCandidate} indicating all potential Golden Resource matches. 062 */ 063 public CandidateList findGoldenResourceCandidates(IAnyResource theResource) { 064 CandidateList matchedGoldenResourceCandidates = myFindCandidateByEidSvc.findCandidates(theResource); 065 066 if (matchedGoldenResourceCandidates.isEmpty()) { 067 matchedGoldenResourceCandidates = myFindCandidateByLinkSvc.findCandidates(theResource); 068 } 069 070 if (matchedGoldenResourceCandidates.isEmpty()) { 071 //OK, so we have not found any links in the MdmLink table with us as a source. Next, let's find 072 //possible Golden Resources matches by following MDM rules. 073 matchedGoldenResourceCandidates = myFindCandidateByExampleSvc.findCandidates(theResource); 074 } 075 076 return matchedGoldenResourceCandidates; 077 } 078 079 public IAnyResource getGoldenResourceFromMatchedGoldenResourceCandidate(MatchedGoldenResourceCandidate theMatchedGoldenResourceCandidate, String theResourceType) { 080 ResourcePersistentId goldenResourcePid = theMatchedGoldenResourceCandidate.getCandidateGoldenResourcePid(); 081 return myMdmResourceDaoSvc.readGoldenResourceByPid(goldenResourcePid, theResourceType); 082 } 083}