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.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
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);
			}

			// Reload the payload just in case any interceptors modified
			// it before it was saved to the database. This is also
			// useful for resources created in a transaction, since they
			// can have placeholder IDs in them.
			IIdType payloadId = msg.getPayloadId(getContext());
			Class type = getContext().getResourceDefinition(payloadId.getResourceType()).getImplementingClass();
			IFhirResourceDao dao = getSubscriptionDao().getDao(type);
			IBaseResource loadedPayload = dao.read(payloadId);
			msg.setPayload(getContext(), loadedPayload);

			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;

}

@@ -314,17 +314,16 @@
 se at%0A *
- 
 %0A *     
@@ -368,17 +368,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -1251,16 +1251,72 @@
 .class);
+%0A%09private boolean myReloadResourceBeforeDelivery = true;
 %0A%0A%09publi
@@ -2116,16 +2116,17 @@
 ription)
+ 
 getSubsc
@@ -2325,16 +2325,58 @@
 ;%0A%09%09%09%7D%0A%0A
+%09%09%09if (myReloadResourceBeforeDelivery) %7B%0A%09
 %09%09%09// Re
@@ -2434,16 +2434,17 @@
 fied%0A%09%09%09
+%09
 // it be
@@ -2491,16 +2491,17 @@
 is also%0A
+%09
 %09%09%09// us
@@ -2559,16 +2559,17 @@
 they%0A%09%09%09
+%09
 // can h
@@ -2597,16 +2597,17 @@
 n them.%0A
+%09
 %09%09%09IIdTy
@@ -2653,16 +2653,17 @@
 ext());%0A
+%09
 %09%09%09Class
@@ -2761,16 +2761,17 @@
 s();%0A%09%09%09
+%09
 IFhirRes
@@ -2823,16 +2823,17 @@
 pe);%0A%09%09%09
+%09
 IBaseRes
@@ -2875,16 +2875,17 @@
 oadId);%0A
+%09
 %09%09%09msg.s
@@ -2923,16 +2923,21 @@
 ayload);
+%0A%09%09%09%7D
 %0A%0A%09%09%09han
@@ -3228,10 +3228,170 @@
 ption;%0A%0A
+%09public void setReloadResourceBeforeDelivery(boolean theReloadResourceBeforeDelivery) %7B%0A%09%09myReloadResourceBeforeDelivery = theReloadResourceBeforeDelivery;%0A%09%7D%0A%0A
 %7D%0A

Add config