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.rest.param;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.model.api.IQueryParameterType;
025import ca.uhn.fhir.rest.api.Constants;
026import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
027
028import static org.apache.commons.lang3.StringUtils.isNotBlank;
029
030/**
031 * Base class for RESTful operation parameter types
032 */
033public abstract class BaseParam implements IQueryParameterType {
034
035        private Boolean myMissing;
036
037        abstract String doGetQueryParameterQualifier();
038
039        abstract String doGetValueAsQueryToken(FhirContext theContext);
040
041        abstract void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue);
042
043        /**
044         * If set to non-null value, indicates that this parameter has been populated with a "[name]:missing=true" or "[name]:missing=false" vale instead of a normal value
045         */
046        @Override
047        public Boolean getMissing() {
048                return myMissing;
049        }
050
051        @Override
052        public final String getQueryParameterQualifier() {
053                if (myMissing != null) {
054                        return Constants.PARAMQUALIFIER_MISSING;
055                }
056                return doGetQueryParameterQualifier();
057        }
058
059        @Override
060        public final String getValueAsQueryToken(FhirContext theContext) {
061                if (myMissing != null) {
062                        return myMissing ? Constants.PARAMQUALIFIER_MISSING_TRUE : Constants.PARAMQUALIFIER_MISSING_FALSE;
063                }
064                return doGetValueAsQueryToken(theContext);
065        }
066
067        /**
068         * Does this parameter type support chained parameters (only reference should return <code>true</code> for this)
069         */
070        protected boolean isSupportsChain() {
071                return false;
072        }
073
074        /**
075         * If set to non-null value, indicates that this parameter has been populated
076         * with a "[name]:missing=true" or "[name]:missing=false" value instead of a
077         * normal value
078         * 
079         * @return Returns a reference to <code>this</code> for easier method chaining
080         */
081        @Override
082        public BaseParam setMissing(Boolean theMissing) {
083                myMissing = theMissing;
084                return this;
085        }
086
087        @Override
088        public final void setValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
089                if (Constants.PARAMQUALIFIER_MISSING.equals(theQualifier)) {
090                        myMissing = "true".equals(theValue);
091                        doSetValueAsQueryToken(theContext, theParamName, null, null);
092                } else {
093                        if (isNotBlank(theQualifier) && theQualifier.charAt(0) == '.') {
094                                if (!isSupportsChain()) {
095                                        String msg = theContext.getLocalizer().getMessage(BaseParam.class, "chainNotSupported", theParamName + theQualifier, theQualifier);
096                                        throw new InvalidRequestException(Msg.code(1935) + msg);
097                                }
098                        }
099
100                        myMissing = null;
101                        doSetValueAsQueryToken(theContext, theParamName, theQualifier, theValue);
102                }
103        }
104
105}