001package ca.uhn.fhir.jpa.subscription.module.standalone;
002
003/*-
004 * #%L
005 * HAPI FHIR Subscription Server
006 * %%
007 * Copyright (C) 2014 - 2020 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
025import ca.uhn.fhir.jpa.subscription.module.cache.ISubscriptionProvider;
026import ca.uhn.fhir.jpa.subscription.module.cache.SubscriptionRegistry;
027import ca.uhn.fhir.model.dstu2.valueset.ResourceTypeEnum;
028import ca.uhn.fhir.rest.api.CacheControlDirective;
029import ca.uhn.fhir.rest.api.server.IBundleProvider;
030import ca.uhn.fhir.rest.client.api.IGenericClient;
031import ca.uhn.fhir.rest.server.SimpleBundleProvider;
032import ca.uhn.fhir.util.BundleUtil;
033import org.hl7.fhir.instance.model.api.IBaseBundle;
034import org.hl7.fhir.instance.model.api.IBaseResource;
035import org.springframework.beans.factory.annotation.Autowired;
036import org.springframework.stereotype.Service;
037
038@Service
039public class FhirClientSubscriptionProvider implements ISubscriptionProvider {
040        @Autowired
041        private FhirContext myFhirContext;
042        @Autowired
043        private SubscriptionRegistry mySubscriptionRegistry;
044
045        IGenericClient myClient;
046
047        @Autowired
048        public FhirClientSubscriptionProvider(IGenericClient theClient) {
049                myClient = theClient;
050        }
051
052        @Override
053        public IBundleProvider search(SearchParameterMap theMap) {
054                FhirContext fhirContext = myClient.getFhirContext();
055
056                String searchURL = ResourceTypeEnum.SUBSCRIPTION.getCode() + theMap.toNormalizedQueryString(myFhirContext);
057
058                IBaseBundle bundle = myClient
059                        .search()
060                        .byUrl(searchURL)
061                        .cacheControl(new CacheControlDirective().setNoCache(true))
062                        .execute();
063
064                return new SimpleBundleProvider(BundleUtil.toListOfResources(fhirContext, bundle));
065        }
066
067        @Override
068        public boolean loadSubscription(IBaseResource theResource) {
069                return mySubscriptionRegistry.registerSubscriptionUnlessAlreadyRegistered(theResource);
070        }
071}