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