001package ca.uhn.fhir.rest.param;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
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.api.IQueryParameterType;
025import ca.uhn.fhir.model.primitive.StringDt;
026import ca.uhn.fhir.rest.api.Constants;
027import org.apache.commons.lang3.StringUtils;
028import org.apache.commons.lang3.builder.EqualsBuilder;
029import org.apache.commons.lang3.builder.HashCodeBuilder;
030import org.apache.commons.lang3.builder.ToStringBuilder;
031import org.apache.commons.lang3.builder.ToStringStyle;
032
033import static org.apache.commons.lang3.StringUtils.defaultString;
034
035public class StringParam extends BaseParam implements IQueryParameterType {
036
037        private boolean myContains;
038        private boolean myExact;
039        private String myValue;
040
041        private Boolean myMdmExpand;
042
043        /**
044         * Constructor
045         */
046        public StringParam() {
047        }
048
049        /**
050         * Constructor
051         */
052        public StringParam(String theValue) {
053                setValue(theValue);
054        }
055
056        /**
057         * Constructor
058         */
059        public StringParam(String theValue, boolean theExact) {
060                setValue(theValue);
061                setExact(theExact);
062        }
063
064        @Override
065        String doGetQueryParameterQualifier() {
066                if (isExact()) {
067                        return Constants.PARAMQUALIFIER_STRING_EXACT;
068                } else if (isContains()) {
069                        return Constants.PARAMQUALIFIER_STRING_CONTAINS;
070                } else {
071                        return null;
072                }
073        }
074
075        @Override
076        String doGetValueAsQueryToken(FhirContext theContext) {
077                return ParameterUtil.escape(myValue);
078        }
079
080        public boolean isMdmExpand() {
081                return myMdmExpand != null && myMdmExpand;
082        }
083
084        public StringParam setMdmExpand(boolean theMdmExpand) {
085                myMdmExpand = theMdmExpand;
086                return this;
087        }
088
089        @Override
090        public int hashCode() {
091                return new HashCodeBuilder(17, 37)
092                        .append(myExact)
093                        .append(myContains)
094                        .append(myValue)
095                        .append(getMissing())
096                        .toHashCode();
097        }
098
099        @Override
100        void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
101                if (Constants.PARAMQUALIFIER_STRING_EXACT.equals(theQualifier)) {
102                        setExact(true);
103                } else {
104                        setExact(false);
105                }
106                if (Constants.PARAMQUALIFIER_STRING_CONTAINS.equals(theQualifier)) {
107                        setContains(true);
108                } else {
109                        setContains(false);
110                }
111                myValue = ParameterUtil.unescape(theValue);
112        }
113
114        @Override
115        public boolean equals(Object obj) {
116                if (this == obj) {
117                        return true;
118                }
119                if (obj == null) {
120                        return false;
121                }
122                if (!(obj instanceof StringParam)) {
123                        return false;
124                }
125
126                StringParam other = (StringParam) obj;
127
128                EqualsBuilder eb = new EqualsBuilder();
129                eb.append(myExact, other.myExact);
130                eb.append(myContains, other.myContains);
131                eb.append(myValue, other.myValue);
132                eb.append(getMissing(), other.getMissing());
133
134                return eb.isEquals();
135        }
136
137        public String getValue() {
138                return myValue;
139        }
140
141        public StringParam setValue(String theValue) {
142                myValue = theValue;
143                return this;
144        }
145
146        public StringDt getValueAsStringDt() {
147                return new StringDt(myValue);
148        }
149
150        public String getValueNotNull() {
151                return defaultString(myValue);
152        }
153
154        /**
155         * String parameter modifier <code>:contains</code>
156         */
157        public boolean isContains() {
158                return myContains;
159        }
160
161        /**
162         * String parameter modifier <code>:contains</code>
163         */
164        public StringParam setContains(boolean theContains) {
165                myContains = theContains;
166                if (myContains) {
167                        setExact(false);
168                        setMissing(null);
169                }
170                return this;
171        }
172
173        public boolean isEmpty() {
174                return StringUtils.isEmpty(myValue);
175        }
176
177        public boolean isExact() {
178                return myExact;
179        }
180
181        public StringParam setExact(boolean theExact) {
182                myExact = theExact;
183                if (myExact) {
184                        setContains(false);
185                        setMissing(null);
186                }
187                return this;
188        }
189
190        @Override
191        public String toString() {
192                ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
193                builder.append("value", getValue());
194                if (myExact) {
195                        builder.append("exact", myExact);
196                }
197                if (myContains) {
198                        builder.append("contains", myContains);
199                }
200                if (getMissing() != null) {
201                        builder.append("missing", getMissing().booleanValue());
202                }
203                return builder.toString();
204        }
205
206}