001/*-
002 * #%L
003 * HAPI FHIR Subscription Server
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.subscription.match.matcher.matching;
021
022import ca.uhn.fhir.jpa.searchparam.matcher.InMemoryMatchResult;
023import ca.uhn.fhir.jpa.searchparam.matcher.InMemoryResourceMatcher;
024import ca.uhn.fhir.jpa.subscription.match.matcher.subscriber.SubscriptionCriteriaParser;
025import ca.uhn.fhir.jpa.subscription.model.CanonicalSubscription;
026import org.springframework.beans.factory.annotation.Autowired;
027
028public class SubscriptionStrategyEvaluator {
029
030        @Autowired
031        private InMemoryResourceMatcher myInMemoryResourceMatcher;
032
033        /**
034         * Constructor
035         */
036        public SubscriptionStrategyEvaluator() {
037                super();
038        }
039
040        public SubscriptionMatchingStrategy determineStrategy(CanonicalSubscription theSubscription) {
041                if (theSubscription.isTopicSubscription()) {
042                        return SubscriptionMatchingStrategy.TOPIC;
043                }
044                String criteriaString = theSubscription.getCriteriaString();
045                return determineStrategy(criteriaString);
046        }
047
048        public SubscriptionMatchingStrategy determineStrategy(String criteriaString) {
049                SubscriptionCriteriaParser.SubscriptionCriteria criteria = SubscriptionCriteriaParser.parse(criteriaString);
050                if (criteria == null) {
051                        return SubscriptionMatchingStrategy.DATABASE;
052                }
053                if (criteria.getCriteria() == null) {
054                        return SubscriptionMatchingStrategy.IN_MEMORY;
055                } else {
056                        InMemoryMatchResult result = myInMemoryResourceMatcher.canBeEvaluatedInMemory(criteriaString);
057                        if (result.supported()) {
058                                return SubscriptionMatchingStrategy.IN_MEMORY;
059                        } else {
060                                return SubscriptionMatchingStrategy.DATABASE;
061                        }
062                }
063        }
064}