001/* 002 * #%L 003 * HAPI FHIR - Core Library 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.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( 043 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 isDefinedAsCrossPartitionSubcription(IBaseResource theSubscription) { 069 if (theSubscription instanceof IBaseHasExtensions) { 070 IBaseExtension extension = ExtensionUtil.getExtensionByUrl( 071 theSubscription, HapiExtensions.EXTENSION_SUBSCRIPTION_CROSS_PARTITION); 072 if (Objects.nonNull(extension)) { 073 try { 074 IBaseBooleanDatatype booleanDatatype = (IBaseBooleanDatatype) (extension.getValue()); 075 return booleanDatatype.getValue(); 076 } catch (ClassCastException theClassCastException) { 077 return false; 078 } 079 } 080 } 081 return false; 082 } 083}