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.topic.status;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.jpa.subscription.match.registry.ActiveSubscription;
024import ca.uhn.fhir.jpa.subscription.model.CanonicalSubscription;
025import ca.uhn.fhir.jpa.topic.SubscriptionTopicUtil;
026import org.hl7.fhir.instance.model.api.IBaseResource;
027import org.hl7.fhir.r5.model.Enumerations;
028import org.hl7.fhir.r5.model.Reference;
029import org.hl7.fhir.r5.model.SubscriptionStatus;
030
031import java.util.List;
032import java.util.UUID;
033
034public class R5NotificationStatusBuilder implements INotificationStatusBuilder<SubscriptionStatus> {
035        private final FhirContext myFhirContext;
036
037        public R5NotificationStatusBuilder(FhirContext theFhirContext) {
038                myFhirContext = theFhirContext;
039        }
040
041        @Override
042        public SubscriptionStatus buildNotificationStatus(
043                        List<IBaseResource> theResources, ActiveSubscription theActiveSubscription, String theTopicUrl) {
044                long eventNumber = theActiveSubscription.getDeliveriesCount();
045                CanonicalSubscription canonicalSubscription = theActiveSubscription.getSubscription();
046
047                SubscriptionStatus subscriptionStatus = new SubscriptionStatus();
048                subscriptionStatus.setId(UUID.randomUUID().toString());
049                subscriptionStatus.setStatus(Enumerations.SubscriptionStatusCodes.ACTIVE);
050                subscriptionStatus.setType(SubscriptionStatus.SubscriptionNotificationType.EVENTNOTIFICATION);
051                // WIP STR5 events-since-subscription-start should be read from the database
052                subscriptionStatus.setEventsSinceSubscriptionStart(eventNumber);
053                SubscriptionStatus.SubscriptionStatusNotificationEventComponent event =
054                                subscriptionStatus.addNotificationEvent();
055                event.setEventNumber(eventNumber);
056                if (!theResources.isEmpty() && !SubscriptionTopicUtil.isEmptyContentTopicSubscription(canonicalSubscription)) {
057                        event.setFocus(new Reference(theResources.get(0).getIdElement()));
058                }
059                subscriptionStatus.setSubscription(
060                                new Reference(theActiveSubscription.getSubscription().getIdElement(myFhirContext)));
061                subscriptionStatus.setTopic(theTopicUrl);
062                return subscriptionStatus;
063        }
064}