001package ca.uhn.fhir.jpa.subscription.match.matcher.matching;
002
003/*-
004 * #%L
005 * HAPI FHIR Subscription Server
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
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.context.RuntimeResourceDefinition;
025import ca.uhn.fhir.interceptor.model.RequestPartitionId;
026import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
027import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
028import ca.uhn.fhir.jpa.searchparam.MatchUrlService;
029import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
030import ca.uhn.fhir.jpa.searchparam.matcher.InMemoryMatchResult;
031import ca.uhn.fhir.jpa.subscription.model.CanonicalSubscription;
032import ca.uhn.fhir.jpa.subscription.model.ResourceModifiedMessage;
033import ca.uhn.fhir.jpa.subscription.util.SubscriptionUtil;
034import ca.uhn.fhir.rest.api.server.IBundleProvider;
035import org.hl7.fhir.instance.model.api.IBaseResource;
036import org.hl7.fhir.instance.model.api.IIdType;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039import org.springframework.beans.factory.annotation.Autowired;
040
041public class DaoSubscriptionMatcher implements ISubscriptionMatcher {
042        private final Logger ourLog = LoggerFactory.getLogger(DaoSubscriptionMatcher.class);
043
044        @Autowired
045        DaoRegistry myDaoRegistry;
046
047        @Autowired
048        MatchUrlService myMatchUrlService;
049
050        @Autowired
051        private FhirContext myCtx;
052
053        @Override
054        public InMemoryMatchResult match(CanonicalSubscription theSubscription, ResourceModifiedMessage theMsg) {
055                IIdType id = theMsg.getPayloadId(myCtx);
056                String criteria = theSubscription.getCriteriaString();
057
058                // Run the subscriptions query and look for matches, add the id as part of the criteria to avoid getting matches of previous resources rather than the recent resource
059                criteria += "&_id=" + id.toUnqualifiedVersionless().getValue();
060
061                IBundleProvider results = performSearch(criteria, theSubscription);
062
063                ourLog.debug("Subscription check found {} results for query: {}", results.size(), criteria);
064
065                return InMemoryMatchResult.fromBoolean(results.size() > 0);
066        }
067
068        /**
069         * Search based on a query criteria
070         */
071        private IBundleProvider performSearch(String theCriteria, CanonicalSubscription theSubscription) {
072                IFhirResourceDao<?> subscriptionDao = myDaoRegistry.getSubscriptionDao();
073                RuntimeResourceDefinition responseResourceDef = subscriptionDao.validateCriteriaAndReturnResourceDefinition(theCriteria);
074                SearchParameterMap responseCriteriaUrl = myMatchUrlService.translateMatchUrl(theCriteria, responseResourceDef);
075
076                IFhirResourceDao<? extends IBaseResource> responseDao = myDaoRegistry.getResourceDao(responseResourceDef.getImplementingClass());
077                responseCriteriaUrl.setLoadSynchronousUpTo(1);
078
079                return responseDao.search(responseCriteriaUrl, SubscriptionUtil.createRequestDetailForPartitionedRequest(theSubscription));
080        }
081
082}