001package ca.uhn.fhir.rest.param;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2020 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.model.primitive.IdDt;
025import ca.uhn.fhir.util.CoverageIgnore;
026import org.apache.commons.lang3.builder.ToStringBuilder;
027import org.apache.commons.lang3.builder.ToStringStyle;
028import org.hl7.fhir.instance.model.api.IBaseResource;
029import org.hl7.fhir.instance.model.api.IIdType;
030
031import java.math.BigDecimal;
032
033import static ca.uhn.fhir.model.primitive.IdDt.isValidLong;
034import static org.apache.commons.lang3.StringUtils.isBlank;
035import static org.apache.commons.lang3.StringUtils.isNotBlank;
036
037public class ReferenceParam extends BaseParam /*implements IQueryParameterType*/ {
038
039        private String myChain;
040        private String myResourceType;
041        private String myBaseUrl;
042        private String myValue;
043        private String myIdPart;
044
045        /**
046         * Constructor
047         */
048        public ReferenceParam() {
049                super();
050        }
051
052        /**
053         * Constructor
054         */
055        public ReferenceParam(String theValue) {
056                setValueAsQueryToken(null, null, null, theValue);
057        }
058
059        /**
060         * Constructor
061         */
062        public ReferenceParam(String theChain, String theValue) {
063                setValueAsQueryToken(null, null, null, theValue);
064                setChain(theChain);
065        }
066
067        /**
068         * Constructor
069         */
070        public ReferenceParam(String theResourceType, String theChain, String theValue) {
071                String qualifier = "";
072                if (isNotBlank(theResourceType)) {
073                        qualifier = ":" + theResourceType;
074                }
075                if (isNotBlank(theChain)) {
076                        qualifier = qualifier + "." + theChain;
077                }
078
079                setValueAsQueryToken(null, null, qualifier, theValue);
080        }
081
082        /**
083         * Constructor
084         *
085         * @since 5.0.0
086         */
087        public ReferenceParam(IIdType theValue) {
088                if (theValue != null) {
089                        setValueAsQueryToken(null, null, null, theValue.getValue());
090                }
091        }
092
093        @Override
094        String doGetQueryParameterQualifier() {
095                StringBuilder b = new StringBuilder();
096                if (isNotBlank(myChain)) {
097                        if (isNotBlank(getResourceType())) {
098                                b.append(':');
099                                b.append(getResourceType());
100                        }
101                        b.append('.');
102                        b.append(myChain);
103                }
104                if (b.length() != 0) {
105                        return b.toString();
106                }
107                return null;
108        }
109
110        @Override
111        String doGetValueAsQueryToken(FhirContext theContext) {
112                if (isBlank(getResourceType())) {
113                        return myValue; // e.g. urn:asdjd or 123 or cid:wieiuru or #1
114                } else {
115                        if (isBlank(getChain()) && isNotBlank(getResourceType())) {
116                                return getResourceType() + "/" + getIdPart();
117                        }
118                        return myValue;
119                }
120        }
121
122        @Override
123        void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
124                String q = theQualifier;
125                if (isNotBlank(q)) {
126                        if (q.startsWith(":")) {
127                                int nextIdx = q.indexOf('.');
128                                if (nextIdx != -1) {
129                                        myChain = q.substring(nextIdx + 1);
130                                        myResourceType = q.substring(1, nextIdx);
131                                } else {
132                                        myChain = null;
133                                        myResourceType = q.substring(1);
134                                }
135
136                                myValue = theValue;
137                                myIdPart = theValue;
138
139                                IdDt id = new IdDt(theValue);
140                                if (!id.hasBaseUrl() && id.hasIdPart() && id.hasResourceType()) {
141                                        if (id.getResourceType().equals(myResourceType)) {
142                                                myIdPart = id.getIdPart();
143                                        }
144                                }
145
146                        } else if (q.startsWith(".")) {
147                                myChain = q.substring(1);
148                                myResourceType = null;
149                                myValue = theValue;
150                                myIdPart = theValue;
151                        }
152                } else {
153                        myChain = null;
154                        myValue = theValue;
155                        IdDt id = new IdDt(theValue);
156                        myResourceType = id.getResourceType();
157                        myIdPart = id.getIdPart();
158                        myBaseUrl = id.getBaseUrl();
159                }
160
161        }
162
163
164        @CoverageIgnore
165        public String getBaseUrl() {
166                return myBaseUrl;
167        }
168
169
170        public String getChain() {
171                return myChain;
172        }
173
174        public ReferenceParam setChain(String theChain) {
175                myChain = theChain;
176                return this;
177        }
178
179        @CoverageIgnore
180        public String getIdPart() {
181                return myIdPart;
182        }
183
184        @CoverageIgnore
185        public BigDecimal getIdPartAsBigDecimal() {
186                return new IdDt(myValue).getIdPartAsBigDecimal();
187        }
188
189        @CoverageIgnore
190        public Long getIdPartAsLong() {
191                return new IdDt(myValue).getIdPartAsLong();
192        }
193
194        public String getResourceType() {
195                if (isNotBlank(myResourceType)) {
196                        return myResourceType;
197                }
198                if (isBlank(myChain)) {
199                        return new IdDt(myValue).getResourceType();
200                }
201                return null;
202        }
203
204        public Class<? extends IBaseResource> getResourceType(FhirContext theCtx) {
205                if (isBlank(getResourceType())) {
206                        return null;
207                }
208                return theCtx.getResourceDefinition(getResourceType()).getImplementingClass();
209        }
210
211        public String getValue() {
212                return myValue;
213        }
214
215        /**
216         * Note that the parameter to this method <b>must</b> be a resource reference, e.g
217         * <code>123</code> or <code>Patient/123</code> or <code>http://example.com/fhir/Patient/123</code>
218         * or something like this. This is not appropriate for cases where a chain is being used and
219         * the value is for a different type of parameter (e.g. a token). In that case, use one of the
220         * setter constructors.
221         */
222        public ReferenceParam setValue(String theValue) {
223                IdDt id = new IdDt(theValue);
224                String qualifier = null;
225                if (id.hasResourceType()) {
226                        qualifier = ":" + id.getResourceType();
227                }
228                setValueAsQueryToken(null, null, qualifier, id.getIdPart());
229                return this;
230        }
231
232        public boolean hasResourceType() {
233                return isNotBlank(myResourceType);
234        }
235
236        @Override
237        protected boolean isSupportsChain() {
238                return true;
239        }
240
241        /**
242         * Returns a new param containing the same value as this param, but with the type copnverted
243         * to {@link DateParam}. This is useful if you are using reference parameters and want to handle
244         * chained parameters of different types in a single method.
245         * <p>
246         * See <a href="https://hapifhir.io/hapi-fhir/docs/server_plain/rest_operations_search.html#chained-resource-references">Dynamic Chains</a>
247         * in the HAPI FHIR documentation for an example of how to use this method.
248         * </p>
249         */
250        public DateParam toDateParam(FhirContext theContext) {
251                DateParam retVal = new DateParam();
252                retVal.setValueAsQueryToken(theContext, null, null, getValueAsQueryToken(theContext));
253                return retVal;
254        }
255
256        /**
257         * Returns a new param containing the same value as this param, but with the type copnverted
258         * to {@link NumberParam}. This is useful if you are using reference parameters and want to handle
259         * chained parameters of different types in a single method.
260         * <p>
261         * See <a href="https://hapifhir.io/hapi-fhir/docs/server_plain/rest_operations_search.html#chained-resource-references">Dynamic Chains</a>
262         * in the HAPI FHIR documentation for an example of how to use this method.
263         * </p>
264         */
265        public NumberParam toNumberParam(FhirContext theContext) {
266                NumberParam retVal = new NumberParam();
267                retVal.setValueAsQueryToken(theContext, null, null, getValueAsQueryToken(theContext));
268                return retVal;
269        }
270
271        /**
272         * Returns a new param containing the same value as this param, but with the type copnverted
273         * to {@link QuantityParam}. This is useful if you are using reference parameters and want to handle
274         * chained parameters of different types in a single method.
275         * <p>
276         * See <a href="https://hapifhir.io/hapi-fhir/docs/server_plain/rest_operations_search.html#chained-resource-references">Dynamic Chains</a>
277         * in the HAPI FHIR documentation for an example of how to use this method.
278         * </p>
279         */
280        public QuantityParam toQuantityParam(FhirContext theContext) {
281                QuantityParam retVal = new QuantityParam();
282                retVal.setValueAsQueryToken(theContext, null, null, getValueAsQueryToken(theContext));
283                return retVal;
284        }
285
286        @Override
287        public String toString() {
288                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
289                if (isNotBlank(myChain)) {
290                        b.append("chain", myChain);
291                }
292                b.append("value", getValue());
293                return b.build();
294        }
295
296        /**
297         * Returns a new param containing the same value as this param, but with the type copnverted
298         * to {@link StringParam}. This is useful if you are using reference parameters and want to handle
299         * chained parameters of different types in a single method.
300         * <p>
301         * See <a href="https://hapifhir.io/hapi-fhir/docs/server_plain/rest_operations_search.html#chained-resource-references">Dynamic Chains</a>
302         * in the HAPI FHIR documentation for an example of how to use this method.
303         * </p>
304         */
305        public StringParam toStringParam(FhirContext theContext) {
306                StringParam retVal = new StringParam();
307                retVal.setValueAsQueryToken(theContext, null, null, getValueAsQueryToken(theContext));
308                return retVal;
309        }
310
311        /**
312         * Returns a new param containing the same value as this param, but with the type copnverted
313         * to {@link TokenParam}. This is useful if you are using reference parameters and want to handle
314         * chained parameters of different types in a single method.
315         * <p>
316         * See <a href="https://hapifhir.io/hapi-fhir/docs/server_plain/rest_operations_search.html#chained-resource-references">Dynamic Chains</a>
317         * in the HAPI FHIR documentation for an example of how to use this method.
318         * </p>
319         */
320        public TokenParam toTokenParam(FhirContext theContext) {
321                TokenParam retVal = new TokenParam();
322                retVal.setValueAsQueryToken(theContext, null, null, getValueAsQueryToken(theContext));
323                return retVal;
324        }
325
326        public boolean isIdPartValidLong() {
327                return isValidLong(getIdPart());
328        }
329}