package ca.uhn.fhir.jpa.subscription;

/*-
 * #%L
 * HAPI FHIR JPA Server
 * %%
 * Copyright (C) 2014 - 2018 University Health Network
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import ca.uhn.fhir.jpa.dao.IFhirResourceDao;
import org.hl7.fhir.r4.model.Subscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;

public abstract class BaseSubscriptionDeliverySubscriber extends BaseSubscriptionSubscriber {
	private static final Logger ourLog = LoggerFactory.getLogger(BaseSubscriptionDeliverySubscriber.class);

	public BaseSubscriptionDeliverySubscriber(IFhirResourceDao<?> theSubscriptionDao, Subscription.SubscriptionChannelType theChannelType, BaseSubscriptionInterceptor theSubscriptionInterceptor) {
		super(theSubscriptionDao, theChannelType, theSubscriptionInterceptor);
	}

	@Override
	public void handleMessage(Message<?> theMessage) throws MessagingException {
		if (!(theMessage.getPayload() instanceof ResourceDeliveryMessage)) {
			ourLog.warn("Unexpected payload type: {}", theMessage.getPayload());
			return;
		}
		try {
			ResourceDeliveryMessage msg = (ResourceDeliveryMessage) theMessage.getPayload();
			if (!subscriptionTypeApplies(getContext(), msg.getSubscription().getBackingSubscription(getContext()))) {
				return;
			}

			CanonicalSubscription updatedSubscription = (CanonicalSubscription)getSubscriptionInterceptor().getIdToSubscription().get(msg.getSubscription().getIdElement(getContext()).getIdPart());
			if (updatedSubscription != null) {
				msg.setSubscription(updatedSubscription);
			}

			handleMessage(msg);
		} catch (Exception e) {
			ourLog.error("Failure handling subscription payload", e);
			throw new MessagingException(theMessage, "Failure handling subscription payload", e);
		}
	}

	public abstract void handleMessage(ResourceDeliveryMessage theMessage) throws Exception;

}

@@ -749,16 +749,118 @@
 rceDao;%0A
+import org.hl7.fhir.instance.model.api.IBaseResource;%0Aimport org.hl7.fhir.instance.model.api.IIdType;%0A
 import o
@@ -2270,16 +2270,522 @@
 ;%0A%09%09%09%7D%0A%0A
+%09%09%09// Reload the payload just in case any interceptors modified%0A%09%09%09// it before it was saved to the database. This is also%0A%09%09%09// useful for resources created in a transaction, since they%0A%09%09%09// can have placeholder IDs in them.%0A%09%09%09IIdType payloadId = msg.getPayloadId(getContext());%0A%09%09%09Class type = Class.forName(payloadId.getResourceType());%0A%09%09%09IFhirResourceDao dao = getSubscriptionDao().getDao(type);%0A%09%09%09IBaseResource loadedPayload = dao.read(payloadId);%0A%09%09%09msg.setPayload(getContext(), loadedPayload);%0A%0A
 %09%09%09handl

Make sure we load subscriptions from DB at some point