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