001package ca.uhn.fhir.jpa.subscription.match.matcher.subscriber;
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.jpa.subscription.match.registry.SubscriptionCanonicalizer;
025import ca.uhn.fhir.jpa.subscription.match.registry.SubscriptionRegistry;
026import ca.uhn.fhir.jpa.subscription.model.ResourceModifiedJsonMessage;
027import ca.uhn.fhir.jpa.subscription.model.ResourceModifiedMessage;
028import org.hl7.fhir.instance.model.api.IBaseResource;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031import org.springframework.beans.factory.annotation.Autowired;
032import org.springframework.messaging.Message;
033import org.springframework.messaging.MessageHandler;
034import org.springframework.messaging.MessagingException;
035
036import javax.annotation.Nonnull;
037
038/**
039 * Responsible for transitioning subscription resources from REQUESTED to ACTIVE
040 * Once activated, the subscription is added to the SubscriptionRegistry.
041 * <p>
042 * Also validates criteria.  If invalid, rejects the subscription without persisting the subscription.
043 */
044public class SubscriptionRegisteringSubscriber extends BaseSubscriberForSubscriptionResources implements MessageHandler {
045        private Logger ourLog = LoggerFactory.getLogger(SubscriptionRegisteringSubscriber.class);
046        @Autowired
047        private FhirContext myFhirContext;
048        @Autowired
049        private SubscriptionRegistry mySubscriptionRegistry;
050        @Autowired
051        private SubscriptionCanonicalizer mySubscriptionCanonicalizer;
052
053        /**
054         * Constructor
055         */
056        public SubscriptionRegisteringSubscriber() {
057                super();
058        }
059
060        @Override
061        public void handleMessage(@Nonnull Message<?> theMessage) throws MessagingException {
062                if (!(theMessage instanceof ResourceModifiedJsonMessage)) {
063                        ourLog.warn("Received message of unexpected type on matching channel: {}", theMessage);
064                        return;
065                }
066
067                ResourceModifiedMessage payload = ((ResourceModifiedJsonMessage) theMessage).getPayload();
068
069                if (!isSubscription(payload)) {
070                        return;
071                }
072
073                switch (payload.getOperationType()) {
074                        case DELETE:
075                                mySubscriptionRegistry.unregisterSubscriptionIfRegistered(payload.getId(myFhirContext).getIdPart());
076                                break;
077                        case CREATE:
078                        case UPDATE:
079                                IBaseResource subscription = payload.getNewPayload(myFhirContext);
080                                String statusString = mySubscriptionCanonicalizer.getSubscriptionStatus(subscription);
081                                if ("active".equals(statusString)) {
082                                        mySubscriptionRegistry.registerSubscriptionUnlessAlreadyRegistered(payload.getNewPayload(myFhirContext));
083                                } else {
084                                        mySubscriptionRegistry.unregisterSubscriptionIfRegistered(payload.getId(myFhirContext).getIdPart());
085                                }
086                                break;
087                        case MANUALLY_TRIGGERED:
088                        default:
089                                break;
090                }
091
092        }
093
094}