001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2025 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.model.primitive.UriDt;
024import ca.uhn.fhir.rest.api.Constants;
025import org.apache.commons.lang3.builder.EqualsBuilder;
026import org.apache.commons.lang3.builder.HashCodeBuilder;
027import org.apache.commons.lang3.builder.ToStringBuilder;
028import org.apache.commons.lang3.builder.ToStringStyle;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032import static org.apache.commons.lang3.StringUtils.defaultString;
033
034public class SpecialParam extends BaseParam /*implements IQueryParameterType*/ {
035        private static final Logger ourLog = LoggerFactory.getLogger(StringParam.class);
036
037        private String myValue;
038        private boolean myContains;
039
040        /**
041         * Constructor
042         */
043        public SpecialParam() {
044                super();
045        }
046
047        @Override
048        String doGetQueryParameterQualifier() {
049                if (myContains) {
050                        return Constants.PARAMQUALIFIER_STRING_CONTAINS;
051                } else {
052                        return null;
053                }
054        }
055
056        /**
057         * {@inheritDoc}
058         */
059        @Override
060        String doGetValueAsQueryToken() {
061                return ParameterUtil.escape(getValue());
062        }
063
064        /**
065         * {@inheritDoc}
066         */
067        @Override
068        void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theParameter) {
069                if (Constants.PARAMQUALIFIER_STRING_CONTAINS.equals(theQualifier)) {
070                        if (theParamName.equalsIgnoreCase(Constants.PARAM_TEXT)
071                                        || theParamName.equalsIgnoreCase(Constants.PARAM_CONTENT)) {
072                                setContains(true);
073                        } else {
074                                ourLog.debug(
075                                                "Attempted to set the :contains modifier on a special search parameter that was not `_text` or `_content`. This is not supported.");
076                        }
077                }
078                setValue(ParameterUtil.unescape(theParameter));
079        }
080
081        /**
082         * Returns the value for the token (generally the value to the right of the
083         * vertical bar on the URL)
084         */
085        public String getValue() {
086                return myValue;
087        }
088
089        public String getValueNotNull() {
090                return defaultString(myValue);
091        }
092
093        public SpecialParam setValue(String theValue) {
094                myValue = theValue;
095                return this;
096        }
097
098        @Override
099        public String toString() {
100                ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
101                builder.append("value", getValue());
102                if (getMissing() != null) {
103                        builder.append(":missing", getMissing());
104                }
105                return builder.toString();
106        }
107
108        private static String toSystemValue(UriDt theSystem) {
109                return theSystem.getValueAsString();
110        }
111        /**
112         * Special parameter modifier <code>:contains</code> for _text and _content
113         */
114        public boolean isContains() {
115                return myContains;
116        }
117
118        /**
119         * Special parameter modifier <code>:contains</code> for _text and _content
120         */
121        public SpecialParam setContains(boolean theContains) {
122                myContains = theContains;
123                if (myContains) {
124                        setMissing(null);
125                }
126                return this;
127        }
128
129        @Override
130        public int hashCode() {
131                return new HashCodeBuilder(17, 37)
132                                .append(isContains())
133                                .append(getValue())
134                                .append(getMissing())
135                                .toHashCode();
136        }
137
138        @Override
139        public boolean equals(Object obj) {
140                if (this == obj) {
141                        return true;
142                }
143                if (obj == null) {
144                        return false;
145                }
146                if (!(obj instanceof SpecialParam)) {
147                        return false;
148                }
149
150                SpecialParam other = (SpecialParam) obj;
151
152                EqualsBuilder eb = new EqualsBuilder();
153                eb.append(myContains, other.myContains);
154                eb.append(myValue, other.myValue);
155                eb.append(getMissing(), other.getMissing());
156
157                return eb.isEquals();
158        }
159}