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 */
022
023import java.math.BigDecimal;
024import java.util.List;
025
026import org.apache.commons.lang3.StringUtils;
027import org.apache.commons.lang3.builder.ToStringBuilder;
028import org.apache.commons.lang3.builder.ToStringStyle;
029import org.hl7.fhir.instance.model.api.IPrimitiveType;
030
031import ca.uhn.fhir.context.FhirContext;
032import ca.uhn.fhir.model.api.IQueryParameterType;
033import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum;
034import ca.uhn.fhir.model.primitive.DecimalDt;
035import ca.uhn.fhir.model.primitive.UriDt;
036import ca.uhn.fhir.util.CoverageIgnore;
037
038@SuppressWarnings("deprecation")
039public class QuantityParam extends BaseParamWithPrefix<QuantityParam> implements IQueryParameterType {
040
041        private BigDecimal myValue;
042        private String mySystem;
043        private String myUnits;
044
045        /**
046         * Constructor
047         */
048        public QuantityParam() {
049                super();
050        }
051
052        /**
053         * Constructor
054         * 
055         * @param theComparator
056         *            The comparator, or <code>null</code> for an equals comparator
057         * @param theValue
058         *            A quantity value
059         * @param theSystem
060         *            The unit system
061         * @param theUnits
062         *            The unit code
063         * @deprecated Use the equivalent constructor which uses {@link ParamPrefixEnum} instead, as {@link QuantityCompararatorEnum} has been deprecated
064         */
065        @Deprecated
066        public QuantityParam(QuantityCompararatorEnum theComparator, BigDecimal theValue, String theSystem, String theUnits) {
067                setComparator(theComparator);
068                setValue(theValue);
069                setSystem(theSystem);
070                setUnits(theUnits);
071        }
072
073        /**
074         * Constructor
075         * 
076         * @param theComparator
077         *            The comparator, or <code>null</code> for an equals comparator
078         * @param theValue
079         *            A quantity value
080         * @param theSystem
081         *            The unit system
082         * @param theUnits
083         *            The unit code
084         * @deprecated Use the equivalent constructor which uses {@link ParamPrefixEnum} instead, as {@link QuantityCompararatorEnum} has been deprecated
085         */
086        @Deprecated
087        public QuantityParam(QuantityCompararatorEnum theComparator, double theValue, String theSystem, String theUnits) {
088                setComparator(theComparator);
089                setValue(theValue);
090                setSystem(theSystem);
091                setUnits(theUnits);
092        }
093
094        /**
095         * Constructor
096         * 
097         * @param theComparator
098         *            The comparator, or <code>null</code> for an equals comparator
099         * @param theValue
100         *            A quantity value
101         * @param theSystem
102         *            The unit system
103         * @param theUnits
104         *            The unit code
105         * @deprecated Use the equivalent constructor which uses {@link ParamPrefixEnum} instead, as {@link QuantityCompararatorEnum} has been deprecated
106         */
107        @Deprecated
108        public QuantityParam(QuantityCompararatorEnum theComparator, long theValue, String theSystem, String theUnits) {
109                setComparator(theComparator);
110                setValue(theValue);
111                setSystem(theSystem);
112                setUnits(theUnits);
113        }
114        
115        
116        
117        
118        
119        
120        
121        /**
122         * Constructor
123         * 
124         * @param thePrefix
125         *            The comparator, or <code>null</code> for an equals comparator
126         * @param theValue
127         *            A quantity value
128         * @param theSystem
129         *            The unit system
130         * @param theUnits
131         *            The unit code
132         */
133        public QuantityParam(ParamPrefixEnum thePrefix, BigDecimal theValue, String theSystem, String theUnits) {
134                setPrefix(thePrefix);
135                setValue(theValue);
136                setSystem(theSystem);
137                setUnits(theUnits);
138        }
139
140        /**
141         * Constructor
142         * 
143         * @param thePrefix
144         *            The comparator, or <code>null</code> for an equals comparator
145         * @param theValue
146         *            A quantity value
147         * @param theSystem
148         *            The unit system
149         * @param theUnits
150         *            The unit code
151         */
152        public QuantityParam(ParamPrefixEnum thePrefix, double theValue, String theSystem, String theUnits) {
153                setPrefix(thePrefix);
154                setValue(theValue);
155                setSystem(theSystem);
156                setUnits(theUnits);
157        }
158
159        /**
160         * Constructor
161         * 
162         * @param thePrefix
163         *            The comparator, or <code>null</code> for an equals comparator
164         * @param theValue
165         *            A quantity value
166         * @param theSystem
167         *            The unit system
168         * @param theUnits
169         *            The unit code
170         */
171        public QuantityParam(ParamPrefixEnum thePrefix, long theValue, String theSystem, String theUnits) {
172                setPrefix(thePrefix);
173                setValue(theValue);
174                setSystem(theSystem);
175                setUnits(theUnits);
176        }
177
178
179        /**
180         * Constructor
181         * 
182         * @param theQuantity
183         *            A quantity value (with no system or units), such as "100.0" or "gt4"
184         */
185        public QuantityParam(String theQuantity) {
186                setValueAsQueryToken(null, null, null, theQuantity);
187        }
188
189        /**
190         * Constructor
191         * 
192         * @param theQuantity
193         *            A quantity value (with no system or units), such as <code>100</code>
194         */
195        public QuantityParam(long theQuantity) {
196                setValueAsQueryToken(null, null, null, Long.toString(theQuantity));
197        }
198
199        /**
200         * Constructor
201         * 
202         * @param theQuantity
203         *            A quantity value (with no system or units), such as "100.0" or "&lt;=4"
204         * @param theSystem
205         *            The unit system
206         * @param theUnits
207         *            The unit code
208         */
209        public QuantityParam(String theQuantity, String theSystem, String theUnits) {
210                setValueAsQueryToken(null, null, null, theQuantity);
211                setSystem(theSystem);
212                setUnits(theUnits);
213        }
214
215        private void clear() {
216                setPrefix(null);
217                setSystem((String)null);
218                setUnits(null);
219                setValue((BigDecimal)null);
220        }
221
222        @Override
223        String doGetQueryParameterQualifier() {
224                return null;
225        }
226
227        @Override
228        String doGetValueAsQueryToken(FhirContext theContext) {
229                StringBuilder b = new StringBuilder();
230                if (getPrefix() != null) {
231                        b.append(ParameterUtil.escapeWithDefault(getPrefix().getValueForContext(theContext)));
232                }
233
234                b.append(ParameterUtil.escapeWithDefault(getValueAsString()));
235                b.append('|');
236                b.append(ParameterUtil.escapeWithDefault(mySystem));
237                b.append('|');
238                b.append(ParameterUtil.escapeWithDefault(myUnits));
239
240                return b.toString();
241        }
242
243        public String getValueAsString() {
244                if (myValue != null) {
245                        return myValue.toPlainString();
246                }
247                return null;
248        }
249
250        @Override
251        void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
252                clear();
253
254                if (theValue == null) {
255                        return;
256                }
257                List<String> parts = ParameterUtil.splitParameterString(theValue, '|', true);
258
259                if (parts.size() > 0 && StringUtils.isNotBlank(parts.get(0))) {
260                        String value = super.extractPrefixAndReturnRest(parts.get(0));
261                        setValue(value);
262                }
263                if (parts.size() > 1 && StringUtils.isNotBlank(parts.get(1))) {
264                        setSystem(parts.get(1));
265                }
266                if (parts.size() > 2 && StringUtils.isNotBlank(parts.get(2))) {
267                        setUnits(parts.get(2));
268                }
269
270        }
271
272        /**
273         * Returns the system, or null if none was provided
274         * <p>
275         * Note that prior to HAPI FHIR 1.5, this method returned a {@link UriDt}
276         * </p>
277         * 
278         * @since 1.5
279         */
280        public String getSystem() {
281                return mySystem;
282        }
283
284        /**
285         * @deprecated Use {{@link #getSystem()}} instead
286         */
287        @Deprecated
288        @CoverageIgnore
289        public UriDt getSystemAsUriDt() {
290                return new UriDt(mySystem);
291        }
292
293        public String getUnits() {
294                return myUnits;
295        }
296
297        
298        /**
299         * Returns the quantity/value, or null if none was provided
300         * <p>
301         * Note that prior to HAPI FHIR 1.5, this method returned a {@link DecimalDt}
302         * </p>
303         * 
304         * @since 1.5
305         */
306        public BigDecimal getValue() {
307                return myValue;
308        }
309        
310        /**
311         * @deprecated Use {@link #getValue()} instead
312         */
313        @Deprecated
314        public DecimalDt getValueAsDecimalDt() {
315                return new DecimalDt(myValue);
316        }
317
318        /**
319         * @deprecated Use {@link #getPrefix()} with the {@link ParamPrefixEnum#APPROXIMATE} constant
320         */
321        @Deprecated
322        public boolean isApproximate() {
323                return getPrefix() == ParamPrefixEnum.APPROXIMATE;
324        }
325
326        /**
327         * @deprecated Use {@link #setPrefix(ParamPrefixEnum)} with the {@link ParamPrefixEnum#APPROXIMATE} constant
328         */
329        @Deprecated
330        public void setApproximate(boolean theApproximate) {
331                if (theApproximate) {
332                        setPrefix(ParamPrefixEnum.APPROXIMATE);
333                } else {
334                        setPrefix(null);
335                }
336        }
337
338
339        public QuantityParam setSystem(String theSystem) {
340                mySystem = theSystem;
341                return this;
342        }
343
344        public QuantityParam setSystem(IPrimitiveType<String> theSystem) {
345                mySystem = null;
346                if (theSystem != null) {
347                        mySystem = theSystem.getValue();
348                }
349                return this;
350        }
351
352        public QuantityParam setUnits(String theUnits) {
353                myUnits = theUnits;
354                return this;
355        }
356
357        public QuantityParam setValue(BigDecimal theValue) {
358                myValue = theValue;
359                return this;
360        }
361
362        public QuantityParam setValue(IPrimitiveType<BigDecimal> theValue) {
363                myValue = null;
364                if (theValue != null) {
365                        myValue = theValue.getValue();
366                }
367                return this;
368        }
369
370        public QuantityParam setValue(String theValue) {
371                myValue = null;
372                if (theValue != null) {
373                        myValue = new BigDecimal(theValue);
374                }
375                return this;
376        }
377
378        public QuantityParam setValue(double theValue) {
379                // Use the valueOf here because the constructor gives crazy precision
380                // changes due to the floating point conversion
381                myValue = BigDecimal.valueOf(theValue);
382                return this;
383        }
384
385        public QuantityParam setValue(long theValue) {
386                // Use the valueOf here because the constructor gives crazy precision
387                // changes due to the floating point conversion
388                myValue = BigDecimal.valueOf(theValue);
389                return this;
390        }
391
392        @Override
393        public String toString() {
394                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
395                b.append("prefix", getPrefix());
396                b.append("value", myValue);
397                b.append("system", mySystem);
398                b.append("units", myUnits);
399                if (getMissing() != null) {
400                        b.append("missing", getMissing());
401                }
402                return b.toString();
403        }
404
405}