001package ca.uhn.fhir.jpa.dao.data;
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.entity.MdmLink;
024import ca.uhn.fhir.mdm.api.MdmMatchResultEnum;
025import org.springframework.data.domain.Pageable;
026import org.springframework.data.jpa.repository.JpaRepository;
027import org.springframework.data.jpa.repository.Modifying;
028import org.springframework.data.jpa.repository.Query;
029import org.springframework.data.repository.query.Param;
030import org.springframework.stereotype.Repository;
031
032import java.util.Date;
033import java.util.List;
034import java.util.Optional;
035
036@Repository
037public interface IMdmLinkDao extends JpaRepository<MdmLink, Long>, IHapiFhirJpaRepository {
038        @Modifying
039        @Query("DELETE FROM MdmLink f WHERE myGoldenResourcePid = :pid OR mySourcePid = :pid")
040        int deleteWithAnyReferenceToPid(@Param("pid") Long thePid);
041
042        @Modifying
043        @Query("DELETE FROM MdmLink f WHERE (myGoldenResourcePid = :pid OR mySourcePid = :pid) AND myMatchResult <> :matchResult")
044        int deleteWithAnyReferenceToPidAndMatchResultNot(@Param("pid") Long thePid, @Param("matchResult") MdmMatchResultEnum theMatchResult);
045
046        @Query("SELECT ml2.myGoldenResourcePid as goldenPid, ml2.mySourcePid as sourcePid FROM MdmLink ml2 " +
047                "WHERE ml2.myMatchResult=:matchResult " +
048                "AND ml2.myGoldenResourcePid IN (" +
049                        "SELECT ml.myGoldenResourcePid FROM MdmLink ml " +
050                        "INNER JOIN ResourceLink hrl " +
051                        "ON hrl.myTargetResourcePid=ml.mySourcePid " +
052                        "AND hrl.mySourceResourcePid=:groupPid " +
053                        "AND hrl.mySourcePath='Group.member.entity' " +
054                        "AND hrl.myTargetResourceType='Patient'" +
055                ")")
056        List<MdmPidTuple> expandPidsFromGroupPidGivenMatchResult(@Param("groupPid") Long theGroupPid, @Param("matchResult") MdmMatchResultEnum theMdmMatchResultEnum);
057
058        @Query("SELECT ml FROM MdmLink ml WHERE ml.mySourcePid = :sourcePid AND ml.myMatchResult = :matchResult")
059        Optional<MdmLink> findBySourcePidAndMatchResult(@Param("sourcePid") Long theSourcePid, @Param("matchResult") MdmMatchResultEnum theMatch);
060
061        interface MdmPidTuple {
062                Long getGoldenPid();
063                Long getSourcePid();
064        }
065
066        @Query("SELECT ml.myGoldenResourcePid as goldenPid, ml.mySourcePid as sourcePid " +
067                "FROM MdmLink ml " +
068                "INNER JOIN MdmLink ml2 " +
069                "on ml.myGoldenResourcePid=ml2.myGoldenResourcePid " +
070                "WHERE ml2.mySourcePid=:sourcePid " +
071                "AND ml2.myMatchResult=:matchResult " +
072                "AND ml.myMatchResult=:matchResult")
073        List<MdmPidTuple> expandPidsBySourcePidAndMatchResult(@Param("sourcePid") Long theSourcePid, @Param("matchResult") MdmMatchResultEnum theMdmMatchResultEnum);
074
075        @Query("SELECT ml.myGoldenResourcePid as goldenPid, ml.mySourcePid as sourcePid FROM MdmLink ml WHERE ml.myGoldenResourcePid = :goldenPid and ml.myMatchResult = :matchResult")
076        List<MdmPidTuple> expandPidsByGoldenResourcePidAndMatchResult(@Param("goldenPid") Long theSourcePid, @Param("matchResult") MdmMatchResultEnum theMdmMatchResultEnum);
077
078        @Query("SELECT ml.myId FROM MdmLink ml WHERE ml.myMdmSourceType = :resourceName AND ml.myCreated <= :highThreshold ORDER BY ml.myCreated DESC")
079        List<Long> findPidByResourceNameAndThreshold(@Param("resourceName") String theResourceName, @Param("highThreshold") Date theHighThreshold, Pageable thePageable);
080
081        @Query("SELECT ml.myId FROM MdmLink ml WHERE ml.myMdmSourceType = :resourceName AND ml.myCreated <= :highThreshold AND ml.myPartitionIdValue IN :partitionId ORDER BY ml.myCreated DESC")
082        List<Long> findPidByResourceNameAndThresholdAndPartitionId(@Param("resourceName") String theResourceName, @Param("highThreshold") Date theHighThreshold, @Param("partitionId") List<Integer> thePartitionIds, Pageable thePageable);
083}