View Javadoc
1   package ca.uhn.fhir.model.base.composite;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import static org.apache.commons.lang3.StringUtils.isBlank;
24  
25  import org.hl7.fhir.instance.model.api.IBaseDatatype;
26  import org.hl7.fhir.instance.model.api.IBaseReference;
27  import org.hl7.fhir.instance.model.api.IBaseResource;
28  import org.hl7.fhir.instance.model.api.IIdType;
29  
30  import ca.uhn.fhir.context.RuntimeResourceDefinition;
31  import ca.uhn.fhir.model.api.BaseIdentifiableElement;
32  import ca.uhn.fhir.model.api.IResource;
33  import ca.uhn.fhir.model.primitive.IdDt;
34  import ca.uhn.fhir.model.primitive.StringDt;
35  import ca.uhn.fhir.rest.client.api.IRestfulClient;
36  
37  public abstract class BaseResourceReferenceDt extends BaseIdentifiableElement implements IBaseDatatype, IBaseReference {
38  
39  	private static final long serialVersionUID = 1L;
40  	
41  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseResourceReferenceDt.class);
42  	private IBaseResource myResource;
43  
44  	/**
45  	 * Constructor
46  	 */
47  	public BaseResourceReferenceDt() {
48  		// nothing
49  	}
50  
51  	/**
52  	 * Constructor
53  	 * 
54  	 * @param theResource
55  	 *           The loaded resource itself
56  	 */
57  	public BaseResourceReferenceDt(IResource theResource) {
58  		myResource = theResource;
59  		setReference(theResource.getId());
60  	}
61  
62  	@Override
63  	public abstract StringDt getDisplayElement();
64  
65  	public abstract IdDt getReference();
66  
67  	/**
68  	 * Gets the actual loaded and parsed resource instance, <b>if it is already present</b>. This method will return the
69  	 * resource instance only if it has previously been loaded using {@link #loadResource(IRestfulClient)} or it was
70  	 * contained within the resource containing this resource.
71  	 *
72  	 * See the FHIR specification section on <a
73  	 * href="http://www.hl7.org/implement/standards/fhir/references.html#id">contained resources</a> for more
74  	 * information.
75  	 * 
76  	 * @see #loadResource(IRestfulClient)
77  	 */
78  	@Override
79  	public IBaseResource getResource() {
80  		return myResource;
81  	}
82  
83  	@Override
84  	protected boolean isBaseEmpty() {
85  		return super.isBaseEmpty() && myResource == null;
86  	}
87  
88  	/**
89  	 * Returns the referenced resource, fetching it <b>if it has not already been loaded</b>. This method invokes the
90  	 * HTTP client to retrieve the resource unless it has already been loaded, or was a contained resource in which case
91  	 * it is simply returned.
92  	 */
93  	public IBaseResource loadResource(IRestfulClient theClient) {
94  		if (myResource != null) {
95  			return myResource;
96  		}
97  
98  		IdDt resourceId = getReference();
99  		if (resourceId == null || isBlank(resourceId.getValue())) {
100 			throw new IllegalStateException("Reference has no resource ID defined");
101 		}
102 		if (isBlank(resourceId.getBaseUrl()) || isBlank(resourceId.getResourceType())) {
103 			throw new IllegalStateException("Reference is not complete (must be in the form [baseUrl]/[resource type]/[resource ID]) - Reference is: " + resourceId.getValue());
104 		}
105 
106 		String resourceUrl = resourceId.getValue();
107 
108 		ourLog.debug("Loading resource at URL: {}", resourceUrl);
109 
110 		RuntimeResourceDefinition definition = theClient.getFhirContext().getResourceDefinition(resourceId.getResourceType());
111 		Class<? extends IBaseResource> resourceType = definition.getImplementingClass();
112 		myResource = theClient.fetchResourceFromUrl(resourceType, resourceUrl);
113 		myResource.setId(resourceUrl);
114 		return myResource;
115 	}
116 
117 	public abstract BaseResourceReferenceDt setReference(IdDt theReference);
118 
119 	public BaseResourceReferenceDt setReference(IIdType theReference) {
120 		if (theReference instanceof IdDt) {
121 			setReference((IdDt) theReference);
122 		} else if (theReference != null) {
123 			setReference(new IdDt(theReference.getValue()));
124 		} else {
125 			setReference((IdDt) null);
126 		}
127 		return this;
128 	}
129 
130 	@Override
131 	public void setResource(IBaseResource theResource) {
132 		myResource = theResource;
133 	}
134 
135 	@Override
136 	public String toString() {
137 		org.apache.commons.lang3.builder.ToStringBuilder b = new org.apache.commons.lang3.builder.ToStringBuilder(this, org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE);
138 		b.append("reference", getReference().getValueAsString());
139 		b.append("loaded", getResource() != null);
140 		return b.toString();
141 	}
142 
143 }