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.entity.MdmLink;
025import ca.uhn.fhir.mdm.api.IMdmLink;
026import ca.uhn.fhir.mdm.log.Logs;
027import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId;
028import org.hl7.fhir.instance.model.api.IAnyResource;
029import org.slf4j.Logger;
030import org.springframework.stereotype.Service;
031
032import java.util.ArrayList;
033import java.util.List;
034import java.util.Optional;
035
036@Service
037public class FindCandidateByLinkSvc extends BaseCandidateFinder {
038        private static final Logger ourLog = Logs.getMdmTroubleshootingLog();
039
040        /**
041         * Attempt to find a currently matching Golden Resource, based on the presence of an {@link MdmLink} entity.
042         *
043         * @param theTarget the {@link IAnyResource} that we want to find candidate Golden Resources for.
044         * @return an Optional list of {@link MatchedGoldenResourceCandidate} indicating matches.
045         */
046        @Override
047        protected List<MatchedGoldenResourceCandidate> findMatchGoldenResourceCandidates(IAnyResource theTarget) {
048                List<MatchedGoldenResourceCandidate> retval = new ArrayList<>();
049
050                ResourcePersistentId targetPid = myIdHelperService.getPidOrNull(RequestPartitionId.allPartitions(), theTarget);
051                if (targetPid != null) {
052                        Optional<? extends IMdmLink> oLink = myMdmLinkDaoSvc.getMatchedLinkForSourcePid(targetPid);
053                        if (oLink.isPresent()) {
054                                ResourcePersistentId goldenResourcePid = oLink.get().getGoldenResourcePersistenceId();
055                                ourLog.debug("Resource previously linked. Using existing link.");
056                                        retval.add(new MatchedGoldenResourceCandidate(goldenResourcePid, oLink.get()));
057                        }
058                }
059                return retval;
060        }
061
062        @Override
063        protected CandidateStrategyEnum getStrategy() {
064                return CandidateStrategyEnum.LINK;
065        }
066}