001/*- 002 * #%L 003 * HAPI FHIR - Master Data Management 004 * %% 005 * Copyright (C) 2014 - 2024 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.mdm.svc; 021 022import ca.uhn.fhir.context.FhirContext; 023import ca.uhn.fhir.interceptor.model.RequestPartitionId; 024import ca.uhn.fhir.jpa.subscription.channel.api.ChannelProducerSettings; 025import ca.uhn.fhir.jpa.subscription.channel.api.IChannelFactory; 026import ca.uhn.fhir.jpa.subscription.model.ResourceModifiedJsonMessage; 027import ca.uhn.fhir.jpa.subscription.model.ResourceModifiedMessage; 028import ca.uhn.fhir.mdm.api.IMdmChannelSubmitterSvc; 029import ca.uhn.fhir.mdm.log.Logs; 030import ca.uhn.fhir.rest.api.Constants; 031import org.hl7.fhir.instance.model.api.IAnyResource; 032import org.hl7.fhir.instance.model.api.IBaseResource; 033import org.slf4j.Logger; 034import org.springframework.beans.factory.annotation.Autowired; 035import org.springframework.messaging.MessageChannel; 036 037import static ca.uhn.fhir.mdm.api.IMdmSettings.EMPI_CHANNEL_NAME; 038 039/** 040 * This class is responsible for manual submissions of {@link IAnyResource} resources onto the MDM Channel. 041 */ 042public class MdmChannelSubmitterSvcImpl implements IMdmChannelSubmitterSvc { 043 private static final Logger ourLog = Logs.getMdmTroubleshootingLog(); 044 045 private MessageChannel myMdmChannelProducer; 046 047 private FhirContext myFhirContext; 048 049 private IChannelFactory myChannelFactory; 050 051 @Override 052 public void submitResourceToMdmChannel(IBaseResource theResource) { 053 ResourceModifiedJsonMessage resourceModifiedJsonMessage = new ResourceModifiedJsonMessage(); 054 ResourceModifiedMessage resourceModifiedMessage = new ResourceModifiedMessage( 055 myFhirContext, 056 theResource, 057 ResourceModifiedMessage.OperationTypeEnum.MANUALLY_TRIGGERED, 058 null, 059 (RequestPartitionId) theResource.getUserData(Constants.RESOURCE_PARTITION_ID)); 060 resourceModifiedMessage.setOperationType(ResourceModifiedMessage.OperationTypeEnum.MANUALLY_TRIGGERED); 061 resourceModifiedJsonMessage.setPayload(resourceModifiedMessage); 062 boolean success = getMdmChannelProducer().send(resourceModifiedJsonMessage); 063 if (!success) { 064 ourLog.error("Failed to submit {} to MDM Channel.", resourceModifiedMessage.getPayloadId()); 065 } 066 } 067 068 @Autowired 069 public MdmChannelSubmitterSvcImpl(FhirContext theFhirContext, IChannelFactory theIChannelFactory) { 070 myFhirContext = theFhirContext; 071 myChannelFactory = theIChannelFactory; 072 } 073 074 private void init() { 075 ChannelProducerSettings channelSettings = new ChannelProducerSettings(); 076 myMdmChannelProducer = myChannelFactory.getOrCreateProducer( 077 EMPI_CHANNEL_NAME, ResourceModifiedJsonMessage.class, channelSettings); 078 } 079 080 private MessageChannel getMdmChannelProducer() { 081 if (myMdmChannelProducer == null) { 082 init(); 083 } 084 return myMdmChannelProducer; 085 } 086}