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.IBaseResource; 009import org.hl7.fhir.instance.model.api.IPrimitiveType; 010import org.thymeleaf.util.Validate; 011 012import java.util.List; 013 014/* 015 * #%L 016 * HAPI FHIR - Core Library 017 * %% 018 * Copyright (C) 2014 - 2021 Smile CDR, Inc. 019 * %% 020 * Licensed under the Apache License, Version 2.0 (the "License"); 021 * you may not use this file except in compliance with the License. 022 * You may obtain a copy of the License at 023 * 024 * http://www.apache.org/licenses/LICENSE-2.0 025 * 026 * Unless required by applicable law or agreed to in writing, software 027 * distributed under the License is distributed on an "AS IS" BASIS, 028 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 029 * See the License for the specific language governing permissions and 030 * limitations under the License. 031 * #L% 032 */ 033 034/** 035 * Utilities for working with the subscription resource 036 */ 037public class SubscriptionUtil { 038 039 private static void populatePrimitiveValue(FhirContext theContext, IBaseResource theSubscription, String theChildName, String theValue) { 040 RuntimeResourceDefinition def = theContext.getResourceDefinition(theSubscription); 041 Validate.isTrue(def.getName().equals("Subscription"), "theResource is not a subscription"); 042 BaseRuntimeChildDefinition statusChild = def.getChildByName(theChildName); 043 List<IBase> entries = statusChild.getAccessor().getValues(theSubscription); 044 IPrimitiveType<?> instance; 045 if (entries.size() == 0) { 046 BaseRuntimeElementDefinition<?> statusElement = statusChild.getChildByName(theChildName); 047 instance = (IPrimitiveType<?>) statusElement.newInstance(statusChild.getInstanceConstructorArguments()); 048 statusChild.getMutator().addValue(theSubscription, instance); 049 } else { 050 instance = (IPrimitiveType<?>) entries.get(0); 051 } 052 053 instance.setValueAsString(theValue); 054 } 055 056 public static void setReason(FhirContext theContext, IBaseResource theSubscription, String theMessage) { 057 populatePrimitiveValue(theContext, theSubscription, "reason", theMessage); 058 } 059 060 public static void setStatus(FhirContext theContext, IBaseResource theSubscription, String theStatus) { 061 populatePrimitiveValue(theContext, theSubscription, "status", theStatus); 062 } 063 064}