001package ca.uhn.fhir.rest.param;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 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 */
022import static org.apache.commons.lang3.StringUtils.defaultString;
023
024import org.apache.commons.lang3.StringUtils;
025import org.apache.commons.lang3.builder.ToStringBuilder;
026import org.apache.commons.lang3.builder.ToStringStyle;
027
028import ca.uhn.fhir.model.api.IQueryParameterType;
029import ca.uhn.fhir.model.primitive.StringDt;
030import ca.uhn.fhir.rest.server.Constants;
031
032public class StringParam extends BaseParam implements IQueryParameterType {
033
034        private boolean myExact;
035        private String myValue;
036        private boolean myContains;
037
038        public StringParam() {
039        }
040
041        public StringParam(String theValue) {
042                setValue(theValue);
043        }
044
045        public StringParam(String theValue, boolean theExact) {
046                setValue(theValue);
047                setExact(theExact);
048        }
049
050        @Override
051        String doGetQueryParameterQualifier() {
052                if (isExact()) {
053                        return Constants.PARAMQUALIFIER_STRING_EXACT;
054                } else if (isContains()) {
055                        return Constants.PARAMQUALIFIER_STRING_CONTAINS;
056                } else {
057                        return null;
058                }
059        }
060
061        @Override
062        String doGetValueAsQueryToken() {
063                return ParameterUtil.escape(myValue);
064        }
065
066        @Override
067        void doSetValueAsQueryToken(String theQualifier, String theValue) {
068                if (Constants.PARAMQUALIFIER_STRING_EXACT.equals(theQualifier)) {
069                        setExact(true);
070                } else {
071                        setExact(false);
072                }
073                if (Constants.PARAMQUALIFIER_STRING_CONTAINS.equals(theQualifier)) {
074                        setContains(true);
075                } else {
076                        setContains(false);
077                }
078                myValue = ParameterUtil.unescape(theValue);
079        }
080
081        public String getValue() {
082                return myValue;
083        }
084
085        public StringDt getValueAsStringDt() {
086                return new StringDt(myValue);
087        }
088
089        public String getValueNotNull() {
090                return defaultString(myValue);
091        }
092
093        public boolean isEmpty() {
094                return StringUtils.isEmpty(myValue);
095        }
096
097        public boolean isExact() {
098                return myExact;
099        }
100
101        public StringParam setExact(boolean theExact) {
102                myExact = theExact;
103                if (myExact) {
104                        setContains(false);
105                        setMissing(null);
106                }
107                return this;
108        }
109
110        /**
111         * String parameter modifier <code>:contains</code>
112         */
113        public boolean isContains() {
114                return myContains;
115        }
116
117        public StringParam setValue(String theValue) {
118                myValue = theValue;
119                return this;
120        }
121
122        @Override
123        public String toString() {
124                ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
125                builder.append("value", getValue());
126                if (myExact) {
127                        builder.append("exact", myExact);
128                }
129                if (myContains) {
130                        builder.append("contains", myContains);
131                }
132                if (getMissing() != null) {
133                        builder.append("missing", getMissing().booleanValue());
134                }
135                return builder.toString();
136        }
137
138        /**
139         * String parameter modifier <code>:contains</code>
140         */
141        public StringParam setContains(boolean theContains) {
142                myContains = theContains;
143                if (myContains) {
144                        setExact(false);
145                        setMissing(null);
146                }
147                return this;
148        }
149
150}