View Javadoc
1   package ca.uhn.fhir.jpa.subscription;
2   
3   /*-
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import ca.uhn.fhir.context.FhirContext;
24  import ca.uhn.fhir.jpa.dao.IFhirResourceDao;
25  import org.hl7.fhir.instance.model.api.IBaseResource;
26  import org.hl7.fhir.instance.model.api.IPrimitiveType;
27  import org.hl7.fhir.r4.model.Subscription;
28  import org.springframework.messaging.MessageHandler;
29  
30  public abstract class BaseSubscriptionSubscriber implements MessageHandler {
31  
32  	private final IFhirResourceDao<?> mySubscriptionDao;
33  	private final Subscription.SubscriptionChannelType myChannelType;
34  	private final BaseSubscriptionInterceptor mySubscriptionInterceptor;
35  
36  	/**
37  	 * Constructor
38  	 */
39  	public BaseSubscriptionSubscriber(IFhirResourceDao<?> theSubscriptionDao, Subscription.SubscriptionChannelType theChannelType, BaseSubscriptionInterceptor theSubscriptionInterceptor) {
40  		mySubscriptionDao = theSubscriptionDao;
41  		myChannelType = theChannelType;
42  		mySubscriptionInterceptor = theSubscriptionInterceptor;
43  	}
44  
45  	public Subscription.SubscriptionChannelType getChannelType() {
46  		return myChannelType;
47  	}
48  
49  	public FhirContext getContext() {
50  		return getSubscriptionDao().getContext();
51  	}
52  
53  	public IFhirResourceDao getSubscriptionDao() {
54  		return mySubscriptionDao;
55  	}
56  
57  	public BaseSubscriptionInterceptor getSubscriptionInterceptor() {
58  		return mySubscriptionInterceptor;
59  	}
60  
61  
62  	/**
63  	 * Does this subscription type (e.g. rest hook, websocket, etc) apply to this interceptor?
64  	 */
65  	protected boolean subscriptionTypeApplies(FhirContext theCtx, IBaseResource theSubscription) {
66  		Subscription.SubscriptionChannelType channelType = getChannelType();
67  		return subscriptionTypeApplies(theCtx, theSubscription, channelType);
68  	}
69  
70  	/**
71  	 * Does this subscription type (e.g. rest hook, websocket, etc) apply to this interceptor?
72  	 */
73  	static boolean subscriptionTypeApplies(FhirContext theCtx, IBaseResource theSubscription, Subscription.SubscriptionChannelType theChannelType) {
74  		IPrimitiveType<?> subscriptionType = theCtx.newTerser().getSingleValueOrNull(theSubscription, BaseSubscriptionInterceptor.SUBSCRIPTION_TYPE, IPrimitiveType.class);
75  		boolean subscriptionTypeApplies = false;
76   		if (subscriptionType != null) {
77  			if (theChannelType.toCode().equals(subscriptionType.getValueAsString())) {
78  				subscriptionTypeApplies = true;
79  			}
80  		}
81  		return subscriptionTypeApplies;
82  	}
83  
84  }