org.hl7.fhir.dstu3.model.Quantity Java Examples

The following examples show how to use org.hl7.fhir.dstu3.model.Quantity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ObservationStatsBuilder.java    From org.hl7.fhir.core with Apache License 2.0 7 votes vote down vote up
private static void addAge(Bundle b, int y, int m, String v) throws FHIRException {
  Observation obs = new Observation();
  obs.setId("obs-example-age-weight-"+Integer.toString(y)+"-"+Integer.toString(m));
  obs.setSubject(new Reference().setReference("Patient/123"));
  obs.setStatus(ObservationStatus.FINAL);
  Calendar when = Calendar.getInstance();
  when.add(Calendar.YEAR, -y);
  when.add(Calendar.MONTH, m);
  obs.setEffective(new DateTimeType(when));
  obs.getCode().addCoding().setCode("29463-7").setSystem("http://loinc.org");
  obs.setValue(new Quantity());
  obs.getValueQuantity().setCode("kg");
  obs.getValueQuantity().setSystem("http://unitsofmeasure.org");
  obs.getValueQuantity().setUnit("kg");
  obs.getValueQuantity().setValue(new BigDecimal(v));
  b.addEntry().setFullUrl("http://hl7.org/fhir/Observation/"+obs.getId()).setResource(obs);
}
 
Example #2
Source File: Convert.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
public Quantity makeQuantityFromPQ(Element pq, String units) throws Exception {
if (pq == null)
   return null;
Quantity qty = new Quantity();
String n = pq.getAttribute("value").replace(",", "").trim();
try {
  qty.setValue(new BigDecimal(n));
} catch (Exception e) {
	throw new Exception("Unable to process value '"+n+"'", e);
}			
units = Utilities.noString(pq.getAttribute("unit")) ? units : pq.getAttribute("unit");
if (!Utilities.noString(units)) {
	if (ucumSvc == null || ucumSvc.validate(units) != null)
		qty.setUnit(units);
	else {
		qty.setCode(units);
		qty.setSystem("http://unitsofmeasure.org");
		qty.setUnit(ucumSvc.getCommonDisplay(units));
	}
}
return qty;		
}
 
Example #3
Source File: QuestionnaireBuilder.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
private boolean instanceOf(TypeRefComponent t, Element obj) {
  if (t.getCode().equals("Reference")) {
    if (!(obj instanceof Reference)) {
      return false;
    } else {
      String url = ((Reference) obj).getReference();
      // there are several problems here around profile matching. This process is degenerative, and there's probably nothing we can do to solve it
      if (url.startsWith("http:") || url.startsWith("https:"))
          return true;
      else if (t.hasProfile() && t.getProfile().startsWith("http://hl7.org/fhir/StructureDefinition/")) 
        return url.startsWith(t.getProfile().substring(40)+'/');
      else
        return true;
    }
  } else if (t.getCode().equals("Quantity")) {
    return obj instanceof Quantity;
  } else
    throw new NotImplementedException("Not Done Yet");
}
 
Example #4
Source File: ObservationAccessor.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
public List<ObservationComponent> getComponents(DomainResource resource){
	org.hl7.fhir.dstu3.model.Observation fhirObservation =
		(org.hl7.fhir.dstu3.model.Observation) resource;
	List<ObservationComponent> components = new ArrayList<>();
	
	for (ObservationComponentComponent o : fhirObservation.getComponent()) {
		ObservationComponent component = new ObservationComponent(o.getId());
		CodeableConcept codeableConcept = o.getCode();
		if (codeableConcept != null) {
			component.setCoding(ModelUtil.getCodingsFromConcept(codeableConcept));
			component.setExtensions(getExtensions(o));
			
			if (o.hasValueQuantity()) {
				Quantity quantity = (Quantity) o.getValue();
				component.setNumericValue(quantity.getValue());
				component.setNumericValueUnit(quantity.getUnit());
			} else if (o.hasValueStringType()) {
				StringType stringType = (StringType) o.getValue();
				component.setStringValue(stringType.getValue());
			}
		}
		components.add(component);
	}
	return components;
}
 
Example #5
Source File: ConceptDao.java    From careconnect-reference-implementation with Apache License 2.0 6 votes vote down vote up
@Override
public ConceptEntity findAddCode(Quantity quantity) {
    Coding code = new Coding().setCode(quantity.getCode()).setSystem(quantity.getSystem());
    ConceptEntity conceptEntity = findCode(code);

    // 12/Jan/2018 KGM to cope with LOINC codes and depreciated SNOMED codes.
    if (conceptEntity == null) {
        CodeSystemEntity system = codeSystemRepository.findBySystem(quantity.getSystem());
        if (system !=null) {
            conceptEntity = new ConceptEntity();
            conceptEntity.setCode(quantity.getCode());
            conceptEntity.setDescription(quantity.getUnit());
            conceptEntity.setDisplay(quantity.getUnit());
            conceptEntity.setCodeSystem(system);
            em.persist(conceptEntity);
        } else {
            throw new IllegalArgumentException("Unsupported system "+quantity.getSystem());
        }
    }
    return conceptEntity;
}
 
Example #6
Source File: AvroConverterTest.java    From bunsen with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecimal() {

  BigDecimal originalDecimal = ((Quantity) testObservation.getValue()).getValue();

  // Decode the Avro decimal to ensure the expected value is there.
  BigDecimal avroDecimal  = (BigDecimal) ((Record)
      ((Record) avroObservation.get("value"))
      .get("quantity"))
      .get("value");

  Assert.assertEquals(originalDecimal.compareTo(avroDecimal), 0);

  Assert.assertEquals(originalDecimal.compareTo(
      ((Quantity) testObservationDecoded
          .getValue())
          .getValue()), 0);
}
 
Example #7
Source File: TestData.java    From bunsen with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a FHIR Observation for testing purposes.
 */
public static Observation newObservation() {

  // Observation based on https://www.hl7.org/FHIR/observation-example-bloodpressure.json.html
  Observation observation = new Observation();

  observation.setId("blood-pressure");

  Identifier identifier = observation.addIdentifier();
  identifier.setSystem("urn:ietf:rfc:3986");
  identifier.setValue("urn:uuid:187e0c12-8dd2-67e2-99b2-bf273c878281");

  observation.setStatus(Observation.ObservationStatus.FINAL);

  Quantity quantity = new Quantity();
  quantity.setValue(new java.math.BigDecimal("123.45"));
  quantity.setUnit("mm[Hg]");
  observation.setValue(quantity);

  return observation;
}
 
Example #8
Source File: SpecificRecordsTest.java    From bunsen with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecimal() {

  BigDecimal originalDecimal = ((org.hl7.fhir.dstu3.model.Quantity)
      testObservation.getValue()).getValue();

  // Decode the Avro decimal to ensure the expected value is there.
  BigDecimal avroDecimal  = avroObservation.getValue().getQuantity().getValue();

  Assert.assertEquals(originalDecimal.compareTo(avroDecimal), 0);

  Assert.assertEquals(originalDecimal.compareTo(
      ((Quantity) testObservationDecoded
          .getValue())
          .getValue()), 0);
}
 
Example #9
Source File: ObservationAccessor.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
public Optional<String> getNumericValueUnit(DomainResource resource){
	org.hl7.fhir.dstu3.model.Observation fhirObservation =
		(org.hl7.fhir.dstu3.model.Observation) resource;
	if (fhirObservation.hasValueQuantity()) {
		Quantity quantity = (Quantity) fhirObservation.getValue();
		if (quantity.getUnit() != null) {
			return Optional.of(quantity.getUnit());
		}
	}
	return Optional.empty();
}
 
Example #10
Source File: ObservationStatsBuilder.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private static Type makeQty(int sat, String human, String ucum) {
  Quantity q = new Quantity();
  q.setCode(ucum);
  q.setSystem("http://unitsofmeasure.org");
  q.setUnit(human);
  q.setValue(new BigDecimal(sat));
  return q;
}
 
Example #11
Source File: ObservationAccessor.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
public Optional<BigDecimal> getNumericValue(DomainResource resource){
	org.hl7.fhir.dstu3.model.Observation fhirObservation =
		(org.hl7.fhir.dstu3.model.Observation) resource;
	if (fhirObservation.hasValueQuantity()) {
		Quantity quantity = (Quantity) fhirObservation.getValue();
		if (quantity.getValue() != null) {
			return Optional.of(quantity.getValue());
		}
	}
	return Optional.empty();
}
 
Example #12
Source File: ObservationAccessor.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
public void setNumericValue(DomainResource resource, BigDecimal value, String unit){
	org.hl7.fhir.dstu3.model.Observation fhirObservation =
		(org.hl7.fhir.dstu3.model.Observation) resource;
	Quantity q = new Quantity();
	q.setUnit(unit);
	q.setValue(value);
	fhirObservation.setValue(q);
}
 
Example #13
Source File: FhirStu3.java    From synthea with Apache License 2.0 5 votes vote down vote up
static Type mapValueToFHIRType(Object value, String unit) {
  if (value == null) {
    return null;

  } else if (value instanceof Condition) {
    Code conditionCode = ((HealthRecord.Entry) value).codes.get(0);
    return mapCodeToCodeableConcept(conditionCode, SNOMED_URI);

  } else if (value instanceof Code) {
    return mapCodeToCodeableConcept((Code) value, SNOMED_URI);

  } else if (value instanceof String) {
    return new StringType((String) value);

  } else if (value instanceof Number) {
    double dblVal = ((Number) value).doubleValue();
    PlainBigDecimal bigVal = new PlainBigDecimal(dblVal);
    return new Quantity().setValue(bigVal)
        .setCode(unit).setSystem(UNITSOFMEASURE_URI)
        .setUnit(unit);
  } else if (value instanceof Components.SampledData) {
    return mapValueToSampledData((Components.SampledData) value, unit);
  } else {
    throw new IllegalArgumentException("unexpected observation value class: "
        + value.getClass().toString() + "; " + value);
  }
}
 
Example #14
Source File: SparkRowConverterTest.java    From bunsen with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecimal() {

  BigDecimal originalDecimal = ((Quantity) testObservation.getValue()).getValue();

  // Use compareTo since equals checks scale as well.
  Assert.assertTrue(originalDecimal.compareTo(
      (BigDecimal) testObservationDataset.select(
          "value.quantity.value").head().get(0)) == 0);

  Assert.assertEquals(originalDecimal.compareTo(
      ((Quantity) testObservationDecoded
          .getValue())
          .getValue()), 0);
}
 
Example #15
Source File: TestData.java    From bunsen with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new Observation for testing.
 *
 * @return a FHIR Observation for testing.
 */
public static Observation newObservation() {
  Observation observation = new Observation();

  observation.setId("blood-pressure");

  Identifier identifier = observation.addIdentifier();
  identifier.setSystem("urn:ietf:rfc:3986");
  identifier.setValue("urn:uuid:187e0c12-8dd2-67e2-99b2-bf273c878281");

  observation.setStatus(Observation.ObservationStatus.FINAL);

  CodeableConcept obsCode = new CodeableConcept();

  observation.setCode(obsCode);

  Quantity quantity = new Quantity();
  quantity.setValue(new java.math.BigDecimal("123.45"));
  quantity.setUnit("mm[Hg]");
  quantity.setSystem("http://unitsofmeasure.org");
  observation.setValue(quantity);

  ObservationComponentComponent component = observation.addComponent();

  CodeableConcept code = new CodeableConcept()
      .addCoding(new Coding()
          .setCode("abc")
          .setSystem("PLACEHOLDER"));

  component.setCode(code);

  return observation;
}
 
Example #16
Source File: NarrativeGenerator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public String genType(Type type) throws DefinitionException {
  if (type instanceof Coding)
    return gen((Coding) type);
  if (type instanceof CodeableConcept)
    return displayCodeableConcept((CodeableConcept) type);
  if (type instanceof Quantity)
    return displayQuantity((Quantity) type);
  if (type instanceof Range)
    return displayRange((Range) type);
  return null;
}
 
Example #17
Source File: NarrativeGenerator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private String displayQuantity(Quantity q) {
  StringBuilder s = new StringBuilder();

  s.append("(system = '").append(describeSystem(q.getSystem()))
      .append("' code ").append(q.getCode())
      .append(" = '").append(lookupCode(q.getSystem(), q.getCode())).append("')");

  return s.toString();
}
 
Example #18
Source File: NarrativeGenerator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private void renderQuantity(Quantity q, XhtmlNode x, boolean showCodeDetails) {
  if (q.hasComparator())
    x.addText(q.getComparator().toCode());
  x.addText(q.getValue().toString());
  if (q.hasUnit())
    x.tx(" "+q.getUnit());
  else if (q.hasCode())
    x.tx(" "+q.getCode());
  if (showCodeDetails && q.hasCode()) {
    x.span("background: LightGoldenRodYellow", null).tx(" (Details: "+describeSystem(q.getSystem())+" code "+q.getCode()+" = '"+lookupCode(q.getSystem(), q.getCode())+"')");
  }
}
 
Example #19
Source File: ObservationStatsBuilder.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private static Type makeQty(double val, String human, String ucum) {
  Quantity q = new Quantity();
  q.setCode(ucum);
  q.setSystem("http://unitsofmeasure.org");
  q.setUnit(human);
  q.setValue(new BigDecimal(val));
  return q;
}
 
Example #20
Source File: FhirStu3.java    From synthea with Apache License 2.0 4 votes vote down vote up
/**
 * Add a MedicationAdministration if needed for the given medication.
 * 
 * @param personEntry       The Entry for the Person
 * @param bundle            Bundle to add the MedicationAdministration to
 * @param encounterEntry    Current Encounter entry
 * @param medication        The Medication
 * @param medicationRequest The related medicationRequest
 * @return The added Entry
 */
private static BundleEntryComponent medicationAdministration(
    BundleEntryComponent personEntry, Bundle bundle, BundleEntryComponent encounterEntry,
    Medication medication, MedicationRequest medicationRequest) {

  MedicationAdministration medicationResource = new MedicationAdministration();

  medicationResource.setSubject(new Reference(personEntry.getFullUrl()));
  medicationResource.setContext(new Reference(encounterEntry.getFullUrl()));

  Code code = medication.codes.get(0);
  String system = code.system.equals("SNOMED-CT") ? SNOMED_URI : RXNORM_URI;

  medicationResource.setMedication(mapCodeToCodeableConcept(code, system));
  medicationResource.setEffective(new DateTimeType(new Date(medication.start)));

  medicationResource.setStatus(MedicationAdministrationStatus.fromCode("completed"));

  if (medication.prescriptionDetails != null) {
    JsonObject rxInfo = medication.prescriptionDetails;
    MedicationAdministrationDosageComponent dosage =
        new MedicationAdministrationDosageComponent();

    // as_needed is true if present
    if ((rxInfo.has("dosage")) && (!rxInfo.has("as_needed"))) {
      Quantity dose = new SimpleQuantity().setValue(
          rxInfo.get("dosage").getAsJsonObject().get("amount").getAsDouble());
      dosage.setDose((SimpleQuantity) dose);

      if (rxInfo.has("instructions")) {
        for (JsonElement instructionElement : rxInfo.get("instructions").getAsJsonArray()) {
          JsonObject instruction = instructionElement.getAsJsonObject();

          dosage.setText(instruction.get("display").getAsString());
        }
      }
    }
    medicationResource.setDosage(dosage);
  }

  if (!medication.reasons.isEmpty()) {
    // Only one element in list
    Code reason = medication.reasons.get(0);
    for (BundleEntryComponent entry : bundle.getEntry()) {
      if (entry.getResource().fhirType().equals("Condition")) {
        Condition condition = (Condition) entry.getResource();
        // Only one element in list
        Coding coding = condition.getCode().getCoding().get(0);
        if (reason.code.equals(coding.getCode())) {
          medicationResource.addReasonReference().setReference(entry.getFullUrl());
        }
      }
    }
  }

  BundleEntryComponent medicationAdminEntry = newEntry(bundle, medicationResource);
  return medicationAdminEntry;
}
 
Example #21
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
private boolean isCoded(Type fixed) {
  return (fixed instanceof Coding) || (fixed instanceof CodeableConcept) || (fixed instanceof CodeType) || (fixed instanceof Quantity);
}
 
Example #22
Source File: Convert.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
public Quantity makeQuantityFromPQ(Element pq) throws Exception {
return makeQuantityFromPQ(pq, null);
}
 
Example #23
Source File: ConceptRepository.java    From careconnect-reference-implementation with Apache License 2.0 votes vote down vote up
ConceptEntity findAddCode(Quantity quantity);