001package ca.uhn.fhir.util;
002
003import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
004import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
005import ca.uhn.fhir.context.FhirContext;
006import ca.uhn.fhir.context.RuntimeResourceDefinition;
007import org.hl7.fhir.instance.model.api.IBase;
008import org.hl7.fhir.instance.model.api.IBaseBooleanDatatype;
009import org.hl7.fhir.instance.model.api.IBaseExtension;
010import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
011import org.hl7.fhir.instance.model.api.IBaseResource;
012import org.hl7.fhir.instance.model.api.IPrimitiveType;
013import org.thymeleaf.util.Validate;
014
015import java.util.List;
016import java.util.Objects;
017
018/*
019 * #%L
020 * HAPI FHIR - Core Library
021 * %%
022 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
023 * %%
024 * Licensed under the Apache License, Version 2.0 (the "License");
025 * you may not use this file except in compliance with the License.
026 * You may obtain a copy of the License at
027 *
028 *      http://www.apache.org/licenses/LICENSE-2.0
029 *
030 * Unless required by applicable law or agreed to in writing, software
031 * distributed under the License is distributed on an "AS IS" BASIS,
032 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
033 * See the License for the specific language governing permissions and
034 * limitations under the License.
035 * #L%
036 */
037
038/**
039 * Utilities for working with the subscription resource
040 */
041public class SubscriptionUtil {
042
043        private static void populatePrimitiveValue(FhirContext theContext, IBaseResource theSubscription, String theChildName, String theValue) {
044                RuntimeResourceDefinition def = theContext.getResourceDefinition(theSubscription);
045                Validate.isTrue(def.getName().equals("Subscription"), "theResource is not a subscription");
046                BaseRuntimeChildDefinition statusChild = def.getChildByName(theChildName);
047                List<IBase> entries = statusChild.getAccessor().getValues(theSubscription);
048                IPrimitiveType<?> instance;
049                if (entries.size() == 0) {
050                        BaseRuntimeElementDefinition<?> statusElement = statusChild.getChildByName(theChildName);
051                        instance = (IPrimitiveType<?>) statusElement.newInstance(statusChild.getInstanceConstructorArguments());
052                        statusChild.getMutator().addValue(theSubscription, instance);
053                } else {
054                        instance = (IPrimitiveType<?>) entries.get(0);
055                }
056
057                instance.setValueAsString(theValue);
058        }
059
060        public static void setReason(FhirContext theContext, IBaseResource theSubscription, String theMessage) {
061                populatePrimitiveValue(theContext, theSubscription, "reason", theMessage);
062        }
063
064        public static void setStatus(FhirContext theContext, IBaseResource theSubscription, String theStatus) {
065                populatePrimitiveValue(theContext, theSubscription, "status", theStatus);
066        }
067
068        public static boolean isCrossPartition(IBaseResource theSubscription) {
069                if (theSubscription instanceof IBaseHasExtensions) {
070                        IBaseExtension extension = ExtensionUtil.getExtensionByUrl(theSubscription, HapiExtensions.EXTENSION_SUBSCRIPTION_CROSS_PARTITION);
071                        if (Objects.nonNull(extension)) {
072                                try {
073                                        IBaseBooleanDatatype booleanDatatype = (IBaseBooleanDatatype) (extension.getValue());
074                                        return booleanDatatype.getValue();
075                                } catch (ClassCastException theClassCastException) {
076                                        return false;
077                                }
078                        }
079                }
080                return false;
081        }
082}