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