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.model.base.composite;
021
022import ca.uhn.fhir.context.RuntimeResourceDefinition;
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.model.api.BaseIdentifiableElement;
025import ca.uhn.fhir.model.api.IResource;
026import ca.uhn.fhir.model.primitive.IdDt;
027import ca.uhn.fhir.model.primitive.StringDt;
028import ca.uhn.fhir.rest.client.api.IRestfulClient;
029import org.hl7.fhir.instance.model.api.IBaseDatatype;
030import org.hl7.fhir.instance.model.api.IBaseReference;
031import org.hl7.fhir.instance.model.api.IBaseResource;
032import org.hl7.fhir.instance.model.api.IIdType;
033
034import static org.apache.commons.lang3.StringUtils.isBlank;
035
036public abstract class BaseResourceReferenceDt extends BaseIdentifiableElement implements IBaseDatatype, IBaseReference {
037
038        private static final long serialVersionUID = 1L;
039        
040        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseResourceReferenceDt.class);
041        private IBaseResource myResource;
042
043        /**
044         * Constructor
045         */
046        public BaseResourceReferenceDt() {
047                // nothing
048        }
049
050        /**
051         * Constructor
052         * 
053         * @param theResource
054         *           The loaded resource itself
055         */
056        public BaseResourceReferenceDt(IResource theResource) {
057                myResource = theResource;
058                setReference(theResource.getId());
059        }
060
061        @Override
062        public abstract StringDt getDisplayElement();
063
064        public abstract IdDt getReference();
065
066        /**
067         * Gets the actual loaded and parsed resource instance, <b>if it is already present</b>. This method will return the
068         * resource instance only if it has previously been loaded using {@link #loadResource(IRestfulClient)} or it was
069         * contained within the resource containing this resource.
070         *
071         * See the FHIR specification section on <a
072         * href="http://www.hl7.org/implement/standards/fhir/references.html#id">contained resources</a> for more
073         * information.
074         * 
075         * @see #loadResource(IRestfulClient)
076         */
077        @Override
078        public IBaseResource getResource() {
079                return myResource;
080        }
081
082        @Override
083        protected boolean isBaseEmpty() {
084                return super.isBaseEmpty() && myResource == null;
085        }
086
087        /**
088         * Returns the referenced resource, fetching it <b>if it has not already been loaded</b>. This method invokes the
089         * HTTP client to retrieve the resource unless it has already been loaded, or was a contained resource in which case
090         * it is simply returned.
091         */
092        public IBaseResource loadResource(IRestfulClient theClient) {
093                if (myResource != null) {
094                        return myResource;
095                }
096
097                IdDt resourceId = getReference();
098                if (resourceId == null || isBlank(resourceId.getValue())) {
099                        throw new IllegalStateException(Msg.code(1905) + "Reference has no resource ID defined");
100                }
101                if (isBlank(resourceId.getBaseUrl()) || isBlank(resourceId.getResourceType())) {
102                        throw new IllegalStateException(Msg.code(1906) + "Reference is not complete (must be in the form [baseUrl]/[resource type]/[resource ID]) - Reference is: " + resourceId.getValue());
103                }
104
105                String resourceUrl = resourceId.getValue();
106
107                ourLog.debug("Loading resource at URL: {}", resourceUrl);
108
109                RuntimeResourceDefinition definition = theClient.getFhirContext().getResourceDefinition(resourceId.getResourceType());
110                Class<? extends IBaseResource> resourceType = definition.getImplementingClass();
111                myResource = theClient.fetchResourceFromUrl(resourceType, resourceUrl);
112                myResource.setId(resourceUrl);
113                return myResource;
114        }
115
116        public abstract BaseResourceReferenceDt setReference(IdDt theReference);
117
118        public BaseResourceReferenceDt setReference(IIdType theReference) {
119                if (theReference instanceof IdDt) {
120                        setReference((IdDt) theReference);
121                } else if (theReference != null) {
122                        setReference(new IdDt(theReference.getValue()));
123                } else {
124                        setReference((IdDt) null);
125                }
126                return this;
127        }
128
129        @Override
130        public BaseResourceReferenceDt setResource(IBaseResource theResource) {
131                myResource = theResource;
132                return this;
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}