001package ca.uhn.fhir.mdm.provider; 002 003/*- 004 * #%L 005 * HAPI FHIR - Master Data Management 006 * %% 007 * Copyright (C) 2014 - 2024 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.context.FhirContext; 024import ca.uhn.fhir.interceptor.api.HookParams; 025import ca.uhn.fhir.interceptor.api.IInterceptorBroadcaster; 026import ca.uhn.fhir.interceptor.api.Pointcut; 027import ca.uhn.fhir.mdm.api.IMdmControllerSvc; 028import ca.uhn.fhir.mdm.api.params.MdmHistorySearchParameters; 029import ca.uhn.fhir.mdm.model.mdmevents.MdmHistoryEvent; 030import ca.uhn.fhir.mdm.model.mdmevents.MdmLinkWithRevisionJson; 031import ca.uhn.fhir.model.api.annotation.Description; 032import ca.uhn.fhir.rest.annotation.Operation; 033import ca.uhn.fhir.rest.annotation.OperationParam; 034import ca.uhn.fhir.rest.api.server.RequestDetails; 035import ca.uhn.fhir.rest.server.provider.ProviderConstants; 036import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; 037import ca.uhn.fhir.util.ParametersUtil; 038import org.hl7.fhir.instance.model.api.IBaseParameters; 039import org.hl7.fhir.instance.model.api.IPrimitiveType; 040import org.slf4j.Logger; 041 042import java.util.List; 043import java.util.stream.Collectors; 044 045import static org.apache.commons.collections4.CollectionUtils.isNotEmpty; 046import static org.slf4j.LoggerFactory.getLogger; 047 048public class MdmLinkHistoryProviderDstu3Plus extends BaseMdmProvider { 049 private static final Logger ourLog = getLogger(MdmLinkHistoryProviderDstu3Plus.class); 050 051 private final IMdmControllerSvc myMdmControllerSvc; 052 053 private final IInterceptorBroadcaster myInterceptorBroadcaster; 054 055 public MdmLinkHistoryProviderDstu3Plus( 056 FhirContext theFhirContext, 057 IMdmControllerSvc theMdmControllerSvc, 058 IInterceptorBroadcaster theIInterceptorBroadcaster) { 059 super(theFhirContext); 060 myMdmControllerSvc = theMdmControllerSvc; 061 myInterceptorBroadcaster = theIInterceptorBroadcaster; 062 } 063 064 @Operation(name = ProviderConstants.MDM_LINK_HISTORY, idempotent = true) 065 public IBaseParameters historyLinks( 066 @Description(value = "The id of the Golden Resource (e.g. Golden Patient Resource).") 067 @OperationParam( 068 name = ProviderConstants.MDM_QUERY_LINKS_GOLDEN_RESOURCE_ID, 069 min = 0, 070 max = OperationParam.MAX_UNLIMITED, 071 typeName = "string") 072 List<IPrimitiveType<String>> theMdmGoldenResourceIds, 073 @Description(value = "The id of the source resource (e.g. Patient resource).") 074 @OperationParam( 075 name = ProviderConstants.MDM_QUERY_LINKS_RESOURCE_ID, 076 min = 0, 077 max = OperationParam.MAX_UNLIMITED, 078 typeName = "string") 079 List<IPrimitiveType<String>> theResourceIds, 080 ServletRequestDetails theRequestDetails) { 081 validateMdmLinkHistoryParameters(theMdmGoldenResourceIds, theResourceIds); 082 083 final List<String> goldenResourceIdsToUse = 084 convertToStringsIncludingCommaDelimitedIfNotNull(theMdmGoldenResourceIds); 085 final List<String> resourceIdsToUse = convertToStringsIncludingCommaDelimitedIfNotNull(theResourceIds); 086 087 final IBaseParameters retVal = ParametersUtil.newInstance(myFhirContext); 088 089 final MdmHistorySearchParameters mdmHistorySearchParameters = new MdmHistorySearchParameters() 090 .setGoldenResourceIds(goldenResourceIdsToUse) 091 .setSourceIds(resourceIdsToUse); 092 093 final List<MdmLinkWithRevisionJson> mdmLinkRevisionsFromSvc = 094 myMdmControllerSvc.queryLinkHistory(mdmHistorySearchParameters, theRequestDetails); 095 096 parametersFromMdmLinkRevisions(retVal, mdmLinkRevisionsFromSvc); 097 098 if (myInterceptorBroadcaster.hasHooks(Pointcut.MDM_POST_LINK_HISTORY)) { 099 // MDM_POST_LINK_HISTORY hook 100 MdmHistoryEvent historyEvent = new MdmHistoryEvent(); 101 historyEvent.setMdmLinkRevisions(mdmLinkRevisionsFromSvc); 102 if (isNotEmpty(theResourceIds)) { 103 historyEvent.setSourceIds(theResourceIds.stream() 104 .map(IPrimitiveType::getValueAsString) 105 .collect(Collectors.toList())); 106 } 107 if (isNotEmpty(theMdmGoldenResourceIds)) { 108 historyEvent.setGoldenResourceIds(theMdmGoldenResourceIds.stream() 109 .map(IPrimitiveType::getValueAsString) 110 .collect(Collectors.toList())); 111 } 112 113 HookParams params = new HookParams(); 114 params.add(RequestDetails.class, theRequestDetails); 115 params.add(MdmHistoryEvent.class, historyEvent); 116 myInterceptorBroadcaster.callHooks(Pointcut.MDM_POST_LINK_HISTORY, params); 117 } 118 119 return retVal; 120 } 121}