public Throwable fhir(SparseArray<GlucoseRecord> records) {

    try {
      Patient p = getPatient();
      Log.d(TAG, "converting glucoserecrods to observations now");

      Device d = initializeDevice(p);
      // do conditional update here so the device is updated if needed

      String ident = getConditionalUrl(d, d.getIdentifierFirstRep());
      String a = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(d);
      IdDt id = (IdDt) client.update().resource(d).conditionalByUrl(ident).execute().getId();
      d.setId(id);

      DeviceMetric metric = initializeDeviceMetric(d);
      metric.setId(
          client
              .create()
              .resource(metric)
              .conditionalByUrl(getConditionalUrl(metric, metric.getIdentifier()))
              .execute()
              .getId());

      for (int i = 0; i < records.size(); i++) {
        int j = records.keyAt(i);
        GlucoseRecord r = records.get(j);

        Log.d(TAG, "creating quantity");

        QuantityDt qdt = initializeQuantityDt(r);

        Observation o = initializeObservation(p, metric, r, qdt);

        Log.d(TAG, "submitting to server...");
        // client.update().resource(o).conditionalByUrl("Observation?identifier=http://...%7Cid").encodedJson().execute();
        IdDt did =
            (IdDt)
                client
                    .update()
                    .resource(o)
                    .conditionalByUrl(getConditionalUrl(o, o.getIdentifierFirstRep()))
                    .execute()
                    .getId();

        o.setId(did);

        String s = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(o);

        Log.d(TAG, "... done");
      }
    } catch (Throwable e) {
      return e;
    }

    return null;
  }
    @Search(queryName = "stitchedInclude")
    public List<DiagnosticReport> stitchedInclude() {
      Practitioner pr1 = new Practitioner();
      pr1.setId("Practitioner/001");
      pr1.getName().addFamily("Pract1");

      Practitioner pr2 = new Practitioner();
      pr2.setId("Practitioner/002");
      pr2.getName().addFamily("Pract2");

      Practitioner pr3 = new Practitioner();
      pr3.setId("Practitioner/003");
      pr3.getName().addFamily("Pract3");

      Observation o1 = new Observation();
      o1.getCode().setText("Obs1");
      o1.addPerformer().setResource(pr1);

      Observation o2 = new Observation();
      o2.getCode().setText("Obs2");
      o2.addPerformer().setResource(pr2);

      Observation o3 = new Observation();
      o3.getCode().setText("Obs3");
      o3.addPerformer().setResource(pr3);

      DiagnosticReport rep = new DiagnosticReport();
      rep.setId("DiagnosticReport/999");
      rep.getName().setText("Rep");
      rep.addResult().setResource(o1);
      rep.addResult().setResource(o2);
      rep.addResult().setResource(o3);

      return Collections.singletonList(rep);
    }
 private Observation newObservation(QuantityDt value) {
   Log.d(TAG, "creating new observation");
   Observation o = new Observation();
   o.setValue(value);
   o.getResourceMetadata()
       .put(
           ResourceMetadataKeyEnum.PROFILES,
           "http://hl7.org/fhir/StructureDefinition/devicemetricobservation");
   o.setStatus(ObservationStatusEnum.FINAL);
   return o;
 }
 public List<FHIRResource> mapToFhirObservation(
     Obs observation, Encounter fhirEncounter, SystemProperties systemProperties) {
   List<FHIRResource> result = new ArrayList<>();
   FHIRResource entry = mapObservation(observation, fhirEncounter, systemProperties);
   Observation fhirObservation = (Observation) entry.getResource();
   fhirObservation.setStatus(ObservationStatusEnum.PRELIMINARY);
   for (Obs member : observation.getGroupMembers()) {
     mapGroupMember(member, fhirEncounter, fhirObservation, result, systemProperties);
   }
   result.add(entry);
   return result;
 }
 private FHIRResource mapObservation(
     Obs openmrsObs, Encounter fhirEncounter, SystemProperties systemProperties) {
   FHIRResource fhirObservationResource =
       observationBuilder.buildObservationResource(
           fhirEncounter,
           systemProperties,
           openmrsObs.getUuid(),
           openmrsObs.getConcept().getName().getName());
   Observation fhirObservation = (Observation) fhirObservationResource.getResource();
   fhirObservation.setStatus(ObservationStatusEnum.PRELIMINARY);
   mapCode(openmrsObs, fhirObservation);
   mapValue(openmrsObs, fhirObservation);
   return fhirObservationResource;
 }
  @Test
  public void testNullFlavorCompositeXml() throws Exception {
    Observation observation = new Observation();
    observation
        .getCode()
        .addCoding()
        .addUndeclaredExtension(
            false,
            "http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor",
            new StringDt("UNK"));
    IParser parser = ctx.newXmlParser().setPrettyPrint(true);
    String xml = parser.encodeResourceToString(observation);

    ourLog.info(xml);

    observation = (Observation) parser.parseResource(xml);
    assertEquals(1, observation.getCode().getCoding().get(0).getUndeclaredExtensions().size());
  }
  @Test
  public void testNullFlavorPrimitiveJson() throws Exception {
    Observation observation = new Observation();
    observation
        .getCode()
        .getCoding()
        .add(new CodingDt().setSystem("http://loinc.org").setCode("3141-9"));
    observation
        .getStatusElement()
        .addUndeclaredExtension(
            false,
            "http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor",
            new StringDt("UNK"));
    IParser parser = ctx.newJsonParser().setPrettyPrint(true);
    String json = parser.encodeResourceToString(observation);

    ourLog.info(json);

    observation = (Observation) parser.parseResource(json);
    assertEquals(1, observation.getStatusElement().getUndeclaredExtensions().size());
  }
  private Observation initializeObservation(
      Patient p, DeviceMetric metric, GlucoseRecord r, QuantityDt qdt) {
    log("Initializing observation");

    Observation o = newObservation(qdt);
    o.setCode(createCode(r.unit));
    o.setSubject(newResourceReference(p.getId()));
    o.setDevice(newResourceReference(metric.getId()));

    // set date and time for observation
    DateTimeDt dt = new DateTimeDt();
    dt.setValue(r.time.getTime());
    o.setEffective(dt);
    o.setIdentifier(
        Arrays.asList(
            getIdentifier(
                "/devices/".concat("" + deviceInfo.getSysID()), "" + r.getSequenceNumber())));

    // set narrative
    o.setText(getNarrative(o));

    log("Initializing observation done");
    return o;
  }
 private void mapCode(Obs openmrsObs, Observation fhirObservation) {
   CodeableConceptDt name = buildCode(openmrsObs);
   if (null != name) {
     fhirObservation.setCode(name);
   }
 }
 private void mapValue(Obs openmrsObs, Observation fhirObservation) {
   IDatatype value = observationValueMapper.map(openmrsObs);
   if (null != value) {
     fhirObservation.setValue(value);
   }
 }