001package ca.uhn.fhir.model.base.composite;
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;
024
025import org.apache.commons.lang3.StringUtils;
026
027import ca.uhn.fhir.context.FhirContext;
028import ca.uhn.fhir.model.api.BaseIdentifiableElement;
029import ca.uhn.fhir.model.api.ICompositeDatatype;
030import ca.uhn.fhir.model.api.IQueryParameterType;
031//TODO: Use of a deprecated method should be resolved.
032import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum;
033import ca.uhn.fhir.model.primitive.BoundCodeDt;
034import ca.uhn.fhir.model.primitive.CodeDt;
035import ca.uhn.fhir.model.primitive.DecimalDt;
036import ca.uhn.fhir.model.primitive.StringDt;
037import ca.uhn.fhir.model.primitive.UriDt;
038import ca.uhn.fhir.rest.param.QuantityParam;
039
040public abstract class BaseQuantityDt extends BaseIdentifiableElement implements ICompositeDatatype, IQueryParameterType {
041
042        private static final long serialVersionUID = -925486613033086056L;
043
044        /**
045         * Sets the value(s) for <b>value</b> (Numerical value (with implicit precision))
046         *
047     * <p>
048     * <b>Definition:</b>
049     * The value of the measured amount. The value includes an implicit precision in the presentation of the value
050     * </p> 
051         */
052        public abstract BaseQuantityDt setValue(BigDecimal theValue);
053
054        
055        @Override
056        public void setValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
057                getComparatorElement().setValue(null);
058                setCode( null);
059                setSystem(null);
060                setUnits( null);
061                setValue( null);
062
063                if (theValue == null) {
064                        return;
065                }
066                String[] parts = theValue.split("\\|");
067                if (parts.length > 0 && StringUtils.isNotBlank(parts[0])) {
068                        if (parts[0].startsWith("<=")) {
069                                //TODO: Use of a deprecated method should be resolved.
070                                getComparatorElement().setValue(QuantityCompararatorEnum.LESSTHAN_OR_EQUALS.getCode());
071                                setValue(new BigDecimal(parts[0].substring(2)));
072                        } else if (parts[0].startsWith("<")) {
073                                //TODO: Use of a deprecated method should be resolved.
074                                getComparatorElement().setValue(QuantityCompararatorEnum.LESSTHAN.getCode());
075                                setValue(new BigDecimal(parts[0].substring(1)));
076                        } else if (parts[0].startsWith(">=")) {
077                                //TODO: Use of a deprecated method should be resolved.
078                                getComparatorElement().setValue(QuantityCompararatorEnum.GREATERTHAN_OR_EQUALS.getCode());
079                                setValue(new BigDecimal(parts[0].substring(2)));
080                        } else if (parts[0].startsWith(">")) {
081                                //TODO: Use of a deprecated method should be resolved.
082                                getComparatorElement().setValue(QuantityCompararatorEnum.GREATERTHAN.getCode());
083                                setValue(new BigDecimal(parts[0].substring(1)));
084                        } else {
085                                setValue(new BigDecimal(parts[0]));
086                        }
087                }
088                if (parts.length > 1 && StringUtils.isNotBlank(parts[1])) {
089                        setSystem(parts[1]);
090                }
091                if (parts.length > 2 && StringUtils.isNotBlank(parts[2])) {
092                        setUnits(parts[2]);
093                }
094
095        }
096        
097        /**
098         * Gets the value(s) for <b>comparator</b> (&lt; | &lt;= | &gt;= | &gt; - how to understand the value).
099         * creating it if it does
100         * not exist. Will not return <code>null</code>.
101         *
102     * <p>
103     * <b>Definition:</b>
104     * How the value should be understood and represented - whether the actual value is greater or less than 
105     * the stated value due to measurement issues. E.g. if the comparator is \"&lt;\" , then the real value is &lt; stated value
106     * </p> 
107         */
108        public abstract BoundCodeDt<?> getComparatorElement();
109
110        @Override
111        public String getValueAsQueryToken(FhirContext theContext) {
112                StringBuilder b= new StringBuilder();
113                if (getComparatorElement() != null) {
114                        b.append(getComparatorElement().getValue());
115                }
116                if (!getValueElement().isEmpty()) {
117                        b.append(getValueElement().getValueAsString());
118                }
119                b.append('|');
120                if (!getSystemElement().isEmpty()) {
121                b.append(getSystemElement().getValueAsString());
122                }
123                b.append('|');
124                if (!getUnitsElement().isEmpty()) {
125                b.append(getUnitsElement().getValueAsString());
126                }
127                
128                return b.toString();
129        }
130        
131
132        @Override
133        public String getQueryParameterQualifier() {
134                return null;
135        }       
136        
137        
138        
139        
140
141        /**
142         * Sets the value for <b>units</b> (Unit representation)
143         *
144     * <p>
145     * <b>Definition:</b>
146     * A human-readable form of the units
147     * </p> 
148         */
149        public abstract BaseQuantityDt setUnits( String theString);
150
151 
152        /**
153         * Gets the value(s) for <b>system</b> (System that defines coded unit form).
154         * creating it if it does
155         * not exist. Will not return <code>null</code>.
156         *
157     * <p>
158     * <b>Definition:</b>
159     * The identification of the system that provides the coded form of the unit
160     * </p> 
161         */
162        public abstract UriDt getSystemElement();
163
164        
165
166        /**
167         * Sets the value for <b>system</b> (System that defines coded unit form)
168         *
169     * <p>
170     * <b>Definition:</b>
171     * The identification of the system that provides the coded form of the unit
172     * </p> 
173         */
174        public abstract BaseQuantityDt setSystem( String theUri);
175 
176        /**
177         * Gets the value(s) for <b>code</b> (Coded form of the unit).
178         * creating it if it does
179         * not exist. Will not return <code>null</code>.
180         *
181     * <p>
182     * <b>Definition:</b>
183     * A computer processable form of the units in some unit representation system
184     * </p> 
185         */
186        public abstract CodeDt getCodeElement();
187
188        /**
189         * Sets the value for <b>code</b> (Coded form of the unit)
190         *
191     * <p>
192     * <b>Definition:</b>
193     * A computer processable form of the units in some unit representation system
194     * </p> 
195         */
196        public abstract BaseQuantityDt setCode( String theCode);
197        /**
198         * Gets the value(s) for <b>units</b> (Unit representation).
199         * creating it if it does
200         * not exist. Will not return <code>null</code>.
201         *
202     * <p>
203     * <b>Definition:</b>
204     * A human-readable form of the units
205     * </p> 
206         */
207        public abstract StringDt getUnitsElement() ;
208        /**
209         * Gets the value(s) for <b>value</b> (Numerical value (with implicit precision)).
210         * creating it if it does
211         * not exist. Will not return <code>null</code>.
212         *
213     * <p>
214     * <b>Definition:</b>
215     * The value of the measured amount. The value includes an implicit precision in the presentation of the value
216     * </p> 
217         */
218        public abstract DecimalDt getValueElement();
219        
220        /**
221         * <b>Not supported!</b>
222         * 
223         * @deprecated get/setMissing is not supported in StringDt. Use {@link QuantityParam} instead if you
224         * need this functionality
225         */
226        @Deprecated
227        @Override
228        public Boolean getMissing() {
229                return null;
230        }
231
232        /**
233         * <b>Not supported!</b>
234         * 
235         * @deprecated get/setMissing is not supported in StringDt. Use {@link QuantityParam} instead if you
236         * need this functionality
237         */
238        @Deprecated
239        @Override
240        public IQueryParameterType setMissing(Boolean theMissing) {
241                throw new UnsupportedOperationException("get/setMissing is not supported in StringDt. Use {@link StringParam} instead if you need this functionality");
242        }
243
244        
245}