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; 021 022import ca.uhn.fhir.jpa.mdm.dao.MdmLinkDaoSvc; 023import ca.uhn.fhir.mdm.api.IMdmLink; 024import ca.uhn.fhir.mdm.api.IMdmLinkQuerySvc; 025import ca.uhn.fhir.mdm.api.MdmHistorySearchParameters; 026import ca.uhn.fhir.mdm.api.MdmLinkJson; 027import ca.uhn.fhir.mdm.api.MdmLinkSourceEnum; 028import ca.uhn.fhir.mdm.api.MdmLinkWithRevision; 029import ca.uhn.fhir.mdm.api.MdmLinkWithRevisionJson; 030import ca.uhn.fhir.mdm.api.MdmMatchResultEnum; 031import ca.uhn.fhir.mdm.api.MdmQuerySearchParameters; 032import ca.uhn.fhir.mdm.api.paging.MdmPageRequest; 033import ca.uhn.fhir.mdm.model.MdmTransactionContext; 034import org.hl7.fhir.instance.model.api.IIdType; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037import org.springframework.beans.factory.annotation.Autowired; 038import org.springframework.data.domain.Page; 039import org.springframework.transaction.annotation.Transactional; 040 041import java.util.Comparator; 042import java.util.List; 043import java.util.stream.Collectors; 044 045public class MdmLinkQuerySvcImplSvc implements IMdmLinkQuerySvc { 046 047 private static final Logger ourLog = LoggerFactory.getLogger(MdmLinkQuerySvcImplSvc.class); 048 049 @Autowired 050 MdmLinkDaoSvc myMdmLinkDaoSvc; 051 052 @Autowired 053 IMdmModelConverterSvc myMdmModelConverterSvc; 054 055 @Override 056 @Deprecated 057 @Transactional 058 public Page<MdmLinkJson> queryLinks( 059 IIdType theGoldenResourceId, 060 IIdType theSourceResourceId, 061 MdmMatchResultEnum theMatchResult, 062 MdmLinkSourceEnum theLinkSource, 063 MdmTransactionContext theMdmContext, 064 MdmPageRequest thePageRequest) { 065 MdmQuerySearchParameters mdmQuerySearchParameters = new MdmQuerySearchParameters(thePageRequest) 066 .setGoldenResourceId(theGoldenResourceId) 067 .setSourceId(theSourceResourceId) 068 .setMatchResult(theMatchResult) 069 .setLinkSource(theLinkSource); 070 071 return queryLinks(mdmQuerySearchParameters, theMdmContext); 072 } 073 074 @Override 075 @Deprecated 076 @Transactional 077 public Page<MdmLinkJson> queryLinks( 078 IIdType theGoldenResourceId, 079 IIdType theSourceResourceId, 080 MdmMatchResultEnum theMatchResult, 081 MdmLinkSourceEnum theLinkSource, 082 MdmTransactionContext theMdmContext, 083 MdmPageRequest thePageRequest, 084 List<Integer> thePartitionIds) { 085 MdmQuerySearchParameters mdmQuerySearchParameters = new MdmQuerySearchParameters(thePageRequest) 086 .setGoldenResourceId(theGoldenResourceId) 087 .setSourceId(theSourceResourceId) 088 .setMatchResult(theMatchResult) 089 .setLinkSource(theLinkSource) 090 .setPartitionIds(thePartitionIds); 091 092 return queryLinks(mdmQuerySearchParameters, theMdmContext); 093 } 094 095 @Override 096 @Transactional 097 public Page<MdmLinkJson> queryLinks( 098 MdmQuerySearchParameters theMdmQuerySearchParameters, MdmTransactionContext theMdmContext) { 099 @SuppressWarnings("unchecked") 100 Page<? extends IMdmLink<?>> mdmLinks = myMdmLinkDaoSvc.executeTypedQuery(theMdmQuerySearchParameters); 101 return mdmLinks.map(myMdmModelConverterSvc::toJson); 102 } 103 104 @Override 105 @Transactional 106 public Page<MdmLinkJson> getDuplicateGoldenResources( 107 MdmTransactionContext theMdmContext, MdmPageRequest thePageRequest) { 108 return getDuplicateGoldenResources(theMdmContext, thePageRequest, null, null); 109 } 110 111 @Override 112 @Transactional 113 public Page<MdmLinkJson> getDuplicateGoldenResources( 114 MdmTransactionContext theMdmContext, 115 MdmPageRequest thePageRequest, 116 List<Integer> thePartitionIds, 117 String theRequestResourceType) { 118 MdmQuerySearchParameters mdmQuerySearchParameters = new MdmQuerySearchParameters(thePageRequest) 119 .setMatchResult(MdmMatchResultEnum.POSSIBLE_DUPLICATE) 120 .setPartitionIds(thePartitionIds) 121 .setResourceType(theRequestResourceType); 122 123 @SuppressWarnings("unchecked") 124 Page<? extends IMdmLink<?>> mdmLinkPage = myMdmLinkDaoSvc.executeTypedQuery(mdmQuerySearchParameters); 125 return mdmLinkPage.map(myMdmModelConverterSvc::toJson); 126 } 127 128 @Override 129 public List<MdmLinkWithRevisionJson> queryLinkHistory(MdmHistorySearchParameters theMdmHistorySearchParameters) { 130 @SuppressWarnings("unchecked") 131 final List<MdmLinkWithRevision<? extends IMdmLink<?>>> mdmLinkHistoryFromDao = 132 myMdmLinkDaoSvc.findMdmLinkHistory(theMdmHistorySearchParameters); 133 134 Comparator<MdmLinkWithRevisionJson> linkHistoryComparator = 135 Comparator.<MdmLinkWithRevisionJson, String>comparing( 136 l -> l.getMdmLink().getGoldenResourceId()) 137 .thenComparing(l -> l.getMdmLink().getSourceId()) 138 .thenComparing(Comparator.comparingLong(MdmLinkWithRevisionJson::getRevisionNumber) 139 .reversed()); 140 141 return mdmLinkHistoryFromDao.stream() 142 .map(myMdmModelConverterSvc::toJson) 143 .sorted(linkHistoryComparator) 144 .collect(Collectors.toUnmodifiableList()); 145 } 146}