001package org.hl7.fhir.r5.utils;
002
003import org.hl7.fhir.r5.model.Observation;
004import org.hl7.fhir.r5.model.ObservationDefinition;
005import org.hl7.fhir.r5.model.ObservationDefinition.ObservationDataType;
006
007public class ObservationUtilities {
008
009  public static Observation fromDefinition(ObservationDefinition def) {
010    Observation obs = new Observation();
011    obs.setCode(def.getCode());
012    obs.getCategory().addAll(def.getCategory());
013    if (def.hasBodySite()) {
014      obs.setBodySite(def.getBodySite());
015    }
016    if (def.hasMethod()) {
017      obs.setMethod(def.getMethod());
018    }
019    if (def.hasPermittedDataType(ObservationDataType.QUANTITY) && def.getPermittedUnit().size() == 1 && def.getPermittedUnitFirstRep().getSystem().equals("http://unitsofmeasure.org")) {
020      obs.getValueQuantity().setSystem("http://unitsofmeasure.org").setCode(def.getPermittedUnitFirstRep().getCode());
021    }
022    // todo: set up reference ranges 
023    return obs;
024  }
025
026}