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