001package ca.uhn.fhir.jpa.subscription.match.matcher.matching;
002
003/*-
004 * #%L
005 * HAPI FHIR Subscription Server
006 * %%
007 * Copyright (C) 2014 - 2021 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.jpa.api.dao.DaoRegistry;
026import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
027import ca.uhn.fhir.jpa.searchparam.MatchUrlService;
028import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
029import ca.uhn.fhir.jpa.searchparam.matcher.InMemoryMatchResult;
030import ca.uhn.fhir.jpa.subscription.model.CanonicalSubscription;
031import ca.uhn.fhir.jpa.subscription.model.ResourceModifiedMessage;
032import ca.uhn.fhir.rest.api.server.IBundleProvider;
033import org.hl7.fhir.instance.model.api.IBaseResource;
034import org.hl7.fhir.instance.model.api.IIdType;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037import org.springframework.beans.factory.annotation.Autowired;
038
039public class DaoSubscriptionMatcher implements ISubscriptionMatcher {
040        private Logger ourLog = LoggerFactory.getLogger(DaoSubscriptionMatcher.class);
041
042        @Autowired
043        DaoRegistry myDaoRegistry;
044
045        @Autowired
046        MatchUrlService myMatchUrlService;
047
048        @Autowired
049        private FhirContext myCtx;
050
051        @Override
052        public InMemoryMatchResult match(CanonicalSubscription theSubscription, ResourceModifiedMessage theMsg) {
053                IIdType id = theMsg.getId(myCtx);
054                String resourceType = id.getResourceType();
055                String resourceId = id.getIdPart();
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=" + resourceType + "/" + resourceId;
060
061                IBundleProvider results = performSearch(criteria);
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) {
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);
080        }
081
082}