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