View Javadoc
1   package ca.uhn.fhir.util;
2   
3   import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
4   import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
5   import ca.uhn.fhir.context.FhirContext;
6   import ca.uhn.fhir.context.RuntimeResourceDefinition;
7   import org.hl7.fhir.instance.model.api.IBase;
8   import org.hl7.fhir.instance.model.api.IBaseResource;
9   import org.hl7.fhir.instance.model.api.IPrimitiveType;
10  import org.thymeleaf.util.Validate;
11  
12  import java.util.List;
13  
14  /*
15   * #%L
16   * HAPI FHIR - Core Library
17   * %%
18   * Copyright (C) 2014 - 2018 University Health Network
19   * %%
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   * 
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   * 
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   * #L%
32   */
33  
34  /**
35   * Utilities for working with the subscription resource
36   */
37  public class SubscriptionUtil {
38  
39  	private static void populatePrimitiveValue(FhirContext theContext, IBaseResource theSubscription, String theChildName, String theValue) {
40  		RuntimeResourceDefinition def = theContext.getResourceDefinition(theSubscription);
41  		Validate.isTrue(def.getName().equals("Subscription"), "theResource is not a subscription");
42  		BaseRuntimeChildDefinition statusChild = def.getChildByName(theChildName);
43  		List<IBase> entries = statusChild.getAccessor().getValues(theSubscription);
44  		IPrimitiveType<?> instance;
45  		if (entries.size() == 0) {
46  			BaseRuntimeElementDefinition<?> statusElement = statusChild.getChildByName(theChildName);
47  			instance = (IPrimitiveType<?>) statusElement.newInstance(statusChild.getInstanceConstructorArguments());
48  			statusChild.getMutator().addValue(theSubscription, instance);
49  		} else {
50  			instance = (IPrimitiveType<?>) entries.get(0);
51  		}
52  
53  		instance.setValueAsString(theValue);
54  	}
55  
56  	public static void setReason(FhirContext theContext, IBaseResource theSubscription, String theMessage) {
57  		populatePrimitiveValue(theContext, theSubscription, "reason", theMessage);
58  	}
59  
60  	public static void setStatus(FhirContext theContext, IBaseResource theSubscription, String theStatus) {
61  		populatePrimitiveValue(theContext, theSubscription, "status", theStatus);
62  	}
63  
64  }