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