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