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