Skip to content

Instantly share code, notes, and snippets.

@jamesagnew
Created May 20, 2022 19:23
Show Gist options
  • Save jamesagnew/ae4e14c563f00b0db4ab0e42b0fcca51 to your computer and use it in GitHub Desktop.
Save jamesagnew/ae4e14c563f00b0db4ab0e42b0fcca51 to your computer and use it in GitHub Desktop.
import ca.uhn.fhir.interceptor.api.Hook;
import ca.uhn.fhir.interceptor.api.Pointcut;
import ca.uhn.fhir.interceptor.api.Interceptor;
import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
import ca.uhn.fhir.jpa.partition.SystemRequestDetails;
import ca.uhn.fhir.jpa.subscription.channel.subscription.ISubscriptionDeliveryChannelNamer;
import ca.uhn.fhir.jpa.subscription.channel.subscription.SubscriptionChannelRegistry;
import ca.uhn.fhir.jpa.subscription.model.CanonicalSubscription;
import ca.uhn.fhir.jpa.subscription.model.ResourceDeliveryJsonMessage;
import ca.uhn.fhir.jpa.subscription.model.ResourceDeliveryMessage;
import ca.uhn.fhir.rest.server.messaging.BaseResourceMessage;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.Subscription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageChannel;
@Interceptor
public class DeleteInterceptor {
@Autowired
private DaoRegistry myDaoRegistry;
@Autowired
private SubscriptionCanonicalizer mySubscriptionCanonicalizer;
@Autowired
private ISubscriptionDeliveryChannelNamer mySubscriptionDeliveryChannelNamer;
@Autowired
private SubscriptionChannelRegistry mySubscriptionChannelRegistry;
private volatile CanonicalSubscription myCanonicalSubscription;
@Hook(Pointcut.STORAGE_PRECOMMIT_RESOURCE_DELETED)
public void delete(IBaseResource theResource) {
String subscriptionId = "Subscription/the-subscription-id"; // TODO: change this
// Lazy load this once
CanonicalSubscription canonicalSubscription = myCanonicalSubscription;
if (canonicalSubscription == null) {
IFhirResourceDao<Subscription> dao = myDaoRegistry.getResourceDao(Subscription.class);
Subscription subscription = dao.read(new IdType(subscriptionId), new SystemRequestDetails());
canonicalSubscription = mySubscriptionCanonicalizer.canonicalize(subscription);
myCanonicalSubscription = canonicalSubscription;
}
// Create a deletion message
ResourceDeliveryMessage deliveryMsg = new ResourceDeliveryMessage();
deliveryMsg.setSubscription(canonicalSubscription);
deliveryMsg.setPayloadId(theResource.getIdElement());
deliveryMsg.setOperationType(BaseResourceMessage.OperationTypeEnum.DELETE);
// Figure out the channel name and send a delivery message to it
String channelName = mySubscriptionDeliveryChannelNamer.nameFromSubscription(canonicalSubscription);
MessageChannel channel = mySubscriptionChannelRegistry.getDeliverySenderChannel(channelName);
ResourceDeliveryJsonMessage wrappedMsg = new ResourceDeliveryJsonMessage(deliveryMsg);
channel.send(wrappedMsg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment