001package ca.uhn.fhir.jpa.dao.mdm;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
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.dao.data.IMdmLinkDao;
024import ca.uhn.fhir.jpa.dao.index.IJpaIdHelperService;
025import ca.uhn.fhir.mdm.api.MdmMatchResultEnum;
026import ca.uhn.fhir.mdm.log.Logs;
027import ca.uhn.fhir.model.primitive.IdDt;
028import org.hl7.fhir.instance.model.api.IBaseResource;
029import org.hl7.fhir.instance.model.api.IIdType;
030import org.slf4j.Logger;
031import org.springframework.beans.factory.annotation.Autowired;
032import org.springframework.stereotype.Service;
033
034import javax.annotation.Nonnull;
035import java.util.HashSet;
036import java.util.List;
037import java.util.Set;
038
039@Service
040public class MdmLinkExpandSvc {
041        private static final Logger ourLog = Logs.getMdmTroubleshootingLog();
042
043        @Autowired
044        private IMdmLinkDao myMdmLinkDao;
045        @Autowired
046        private IJpaIdHelperService myIdHelperService;
047
048        public MdmLinkExpandSvc() {
049        }
050
051        /**
052         * Given a source resource, perform MDM expansion and return all the resource IDs of all resources that are
053         * MDM-Matched to this resource.
054         *
055         * @param theResource The resource to MDM-Expand
056         * @return A set of strings representing the FHIR IDs of the expanded resources.
057         */
058        public Set<String> expandMdmBySourceResource(IBaseResource theResource) {
059                ourLog.debug("About to MDM-expand source resource {}", theResource);
060                return expandMdmBySourceResourceId(theResource.getIdElement());
061        }
062
063        /**
064         *  Given a resource ID of a source resource, perform MDM expansion and return all the resource IDs of all resources that are
065         *  MDM-Matched to this resource.
066         *
067         * @param theId The Resource ID of the resource to MDM-Expand
068         * @return A set of strings representing the FHIR ids of the expanded resources.
069         */
070        public Set<String> expandMdmBySourceResourceId(IIdType theId) {
071                ourLog.debug("About to expand source resource with resource id {}", theId);
072                Long pidOrThrowException = myIdHelperService.getPidOrThrowException(theId);
073                return expandMdmBySourceResourcePid(pidOrThrowException);
074        }
075
076        /**
077         *  Given a PID of a source resource, perform MDM expansion and return all the resource IDs of all resources that are
078         *  MDM-Matched to this resource.
079         *
080         * @param theSourceResourcePid The PID of the resource to MDM-Expand
081         * @return A set of strings representing the FHIR ids of the expanded resources.
082         */
083        public Set<String> expandMdmBySourceResourcePid(Long theSourceResourcePid) {
084                ourLog.debug("About to expand source resource with PID {}", theSourceResourcePid);
085                List<IMdmLinkDao.MdmPidTuple> goldenPidSourcePidTuples = myMdmLinkDao.expandPidsBySourcePidAndMatchResult(theSourceResourcePid, MdmMatchResultEnum.MATCH);
086                return flattenPidTuplesToSet(theSourceResourcePid, goldenPidSourcePidTuples);
087        }
088
089        /**
090         *  Given a PID of a golden resource, perform MDM expansion and return all the resource IDs of all resources that are
091         *  MDM-Matched to this golden resource.
092         *
093         * @param theGoldenResourcePid The PID of the golden resource to MDM-Expand.
094         * @return A set of strings representing the FHIR ids of the expanded resources.
095         */
096        public Set<String> expandMdmByGoldenResourceId(Long theGoldenResourcePid) {
097                ourLog.debug("About to expand golden resource with PID {}", theGoldenResourcePid);
098                List<IMdmLinkDao.MdmPidTuple> goldenPidSourcePidTuples = myMdmLinkDao.expandPidsByGoldenResourcePidAndMatchResult(theGoldenResourcePid, MdmMatchResultEnum.MATCH);
099                return flattenPidTuplesToSet(theGoldenResourcePid, goldenPidSourcePidTuples);
100        }
101
102
103        /**
104         *  Given a resource ID of a golden resource, perform MDM expansion and return all the resource IDs of all resources that are
105         *  MDM-Matched to this golden resource.
106         *
107         * @param theGoldenResourcePid The resource ID of the golden resource to MDM-Expand.
108         * @return A set of strings representing the FHIR ids of the expanded resources.
109         */
110        public Set<String> expandMdmByGoldenResourcePid(Long theGoldenResourcePid) {
111                ourLog.debug("About to expand golden resource with PID {}", theGoldenResourcePid);
112                List<IMdmLinkDao.MdmPidTuple> goldenPidSourcePidTuples = myMdmLinkDao.expandPidsByGoldenResourcePidAndMatchResult(theGoldenResourcePid, MdmMatchResultEnum.MATCH);
113                return flattenPidTuplesToSet(theGoldenResourcePid, goldenPidSourcePidTuples);
114        }
115
116        public Set<String> expandMdmByGoldenResourceId(IdDt theId) {
117                ourLog.debug("About to expand golden resource with golden resource id {}", theId);
118                Long pidOrThrowException = myIdHelperService.getPidOrThrowException(theId);
119                return expandMdmByGoldenResourcePid(pidOrThrowException);
120        }
121
122        @Nonnull
123        private Set<String> flattenPidTuplesToSet(Long initialPid, List<IMdmLinkDao.MdmPidTuple> goldenPidSourcePidTuples) {
124                Set<Long> flattenedPids = new HashSet<>();
125                goldenPidSourcePidTuples.forEach(tuple -> {
126                        flattenedPids.add(tuple.getSourcePid());
127                        flattenedPids.add(tuple.getGoldenPid());
128                });
129                Set<String> resourceIds = myIdHelperService.translatePidsToFhirResourceIds(flattenedPids);
130                ourLog.debug("Pid {} has been expanded to [{}]", initialPid, String.join(",", resourceIds));
131                return resourceIds;
132        }
133}