001/* 002 * #%L 003 * HAPI FHIR - Core Library 004 * %% 005 * Copyright (C) 2014 - 2023 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.util; 021 022import ca.uhn.fhir.context.BaseRuntimeChildDefinition; 023import ca.uhn.fhir.context.BaseRuntimeElementDefinition; 024import ca.uhn.fhir.context.FhirContext; 025import ca.uhn.fhir.context.RuntimeResourceDefinition; 026import org.hl7.fhir.instance.model.api.IBase; 027import org.hl7.fhir.instance.model.api.IBaseBooleanDatatype; 028import org.hl7.fhir.instance.model.api.IBaseExtension; 029import org.hl7.fhir.instance.model.api.IBaseHasExtensions; 030import org.hl7.fhir.instance.model.api.IBaseResource; 031import org.hl7.fhir.instance.model.api.IPrimitiveType; 032import org.thymeleaf.util.Validate; 033 034import java.util.List; 035import java.util.Objects; 036 037/** 038 * Utilities for working with the subscription resource 039 */ 040public class SubscriptionUtil { 041 042 private static void populatePrimitiveValue(FhirContext theContext, IBaseResource theSubscription, String theChildName, String theValue) { 043 RuntimeResourceDefinition def = theContext.getResourceDefinition(theSubscription); 044 Validate.isTrue(def.getName().equals("Subscription"), "theResource is not a subscription"); 045 BaseRuntimeChildDefinition statusChild = def.getChildByName(theChildName); 046 List<IBase> entries = statusChild.getAccessor().getValues(theSubscription); 047 IPrimitiveType<?> instance; 048 if (entries.size() == 0) { 049 BaseRuntimeElementDefinition<?> statusElement = statusChild.getChildByName(theChildName); 050 instance = (IPrimitiveType<?>) statusElement.newInstance(statusChild.getInstanceConstructorArguments()); 051 statusChild.getMutator().addValue(theSubscription, instance); 052 } else { 053 instance = (IPrimitiveType<?>) entries.get(0); 054 } 055 056 instance.setValueAsString(theValue); 057 } 058 059 public static void setReason(FhirContext theContext, IBaseResource theSubscription, String theMessage) { 060 populatePrimitiveValue(theContext, theSubscription, "reason", theMessage); 061 } 062 063 public static void setStatus(FhirContext theContext, IBaseResource theSubscription, String theStatus) { 064 populatePrimitiveValue(theContext, theSubscription, "status", theStatus); 065 } 066 067 public static boolean isCrossPartition(IBaseResource theSubscription) { 068 if (theSubscription instanceof IBaseHasExtensions) { 069 IBaseExtension extension = ExtensionUtil.getExtensionByUrl(theSubscription, HapiExtensions.EXTENSION_SUBSCRIPTION_CROSS_PARTITION); 070 if (Objects.nonNull(extension)) { 071 try { 072 IBaseBooleanDatatype booleanDatatype = (IBaseBooleanDatatype) (extension.getValue()); 073 return booleanDatatype.getValue(); 074 } catch (ClassCastException theClassCastException) { 075 return false; 076 } 077 } 078 } 079 return false; 080 } 081}