@Override
  public void forSave(Encounter encounter, EncounterTransaction encounterTransaction) {
    List<EncounterTransaction.Diagnosis> diagnoses = encounterTransaction.getDiagnoses();
    if (!diagnoses.isEmpty()) {
      Set<Obs> obsAtTopLevel = encounter.getObsAtTopLevel(false);
      Concept bahmniDiagnosisStatusConcept =
          bahmniDiagnosisMetadata.diagnosisSchemaContainsStatus()
              ? bahmniDiagnosisMetadata.getBahmniDiagnosisStatusConcept()
              : null;
      Concept bahmniInitialDiagnosisConcept =
          bahmniDiagnosisMetadata.getBahmniInitialDiagnosisConcept();
      Concept bahmniDiagnosisRevisedConcept =
          bahmniDiagnosisMetadata.getBahmniDiagnosisRevisedConcept();

      for (EncounterTransaction.Diagnosis diagnosis : diagnoses) {
        BahmniDiagnosisRequest bahmniDiagnosisRequest = (BahmniDiagnosisRequest) diagnosis;
        addExtraMetadata(
            obsAtTopLevel,
            bahmniDiagnosisRequest,
            bahmniInitialDiagnosisConcept,
            bahmniDiagnosisStatusConcept,
            bahmniDiagnosisRevisedConcept);
        updateRevisedFlagOfPreviousDiagnosis(bahmniDiagnosisRequest, bahmniDiagnosisRevisedConcept);
      }
    }
  }
  @Test
  public void shouldGetEncounterTransactionByDate() throws Exception {
    executeDataSet("baseMetaData.xml");
    executeDataSet("dispositionMetaData.xml");
    executeDataSet("diagnosisMetaData.xml");
    executeDataSet("shouldGetEncounterTransactionByDate.xml");
    String dispositionDateTime = "2005-01-01T01:00:00.000+0000";
    String encounter1PostData =
        "{"
            + "\"patientUuid\" : \"a76e8d23-0c38-408c-b2a8-ea5540f01b51\", "
            + "\"visitTypeUuid\" : \"b45ca846-c79a-11e2-b0c0-8e397087571c\", "
            + "\"encounterTypeUuid\": \"4f3c2244-9d6a-439e-b88a-6e8873489ea7\", "
            + "\"encounterDateTime\" : \"2004-01-01T10:00:00.000+0000\" "
            + "}";
    EncounterTransaction encounter1Response =
        deserialize(
            handle(newPostRequest("/rest/emrapi/encounter", encounter1PostData)),
            EncounterTransaction.class);
    String cancerDiagnosisUuid = "d102c80f-1yz9-4da3-bb88-8122ce8868dh";
    String malariaDiagnosisUuid = "604dcce9-bcd9-48a8-b2f5-112743cf1db8";
    String visitUuid = encounter1Response.getVisitUuid();
    String encounter2PostData =
        "{"
            + "\"patientUuid\" : \"a76e8d23-0c38-408c-b2a8-ea5540f01b51\", "
            + "\"visitTypeUuid\" : \"b45ca846-c79a-11e2-b0c0-8e397087571c\", "
            + "\"visitUuid\": \""
            + visitUuid
            + "\", "
            + "\"encounterTypeUuid\": \"2b377dba-62c3-4e53-91ef-b51c68899891\", "
            + "\"encounterDateTime\" : \"2005-01-01T10:00:00.000+0000\", "
            + "\"observations\":["
            + "{\""
            + "concept\":{\"uuid\": \"4f3c2244-9d6a-439e-b88a-6e8873489ea7\"}, "
            + "\"groupMembers\" : [{\"concept\":{\"uuid\": \"82e5f23e-e0b3-4e53-b6bb-c09c1c7fb8b0\"}, \"value\":20, \"comment\":\"overweight\" }] "
            + "}"
            + "],"
            + "\"diagnoses\":["
            + "{\"order\":\"PRIMARY\", \"certainty\": \"CONFIRMED\", \"codedAnswer\": { \"uuid\": \""
            + cancerDiagnosisUuid
            + "\"} },"
            + "{\"order\":\"PRIMARY\", \"certainty\": \"CONFIRMED\", \"codedAnswer\": { \"uuid\": \""
            + malariaDiagnosisUuid
            + "\"} }"
            + "], "
            + "\"disposition\": {"
            + "    \"code\": \"ADMIT\","
            + "    \"dispositionDateTime\": \""
            + dispositionDateTime
            + "\","
            + "    \"additionalObs\": ["
            + "        {"
            + "            \"value\": \"Admit him to ICU.\","
            + "            \"concept\": {"
            + "                \"uuid\": \"9169366f-3c7f-11e3-8f4c-005056823ee5\","
            + "                \"name\": \"Disposition Note\""
            + "            }"
            + "        }"
            + "    ]"
            + "}"
            + "}";
    EncounterTransaction encounter2Response =
        deserialize(
            handle(newPostRequest("/rest/emrapi/encounter", encounter2PostData)),
            EncounterTransaction.class);
    assertEquals(encounter1Response.getVisitUuid(), encounter2Response.getVisitUuid());
    assertNotEquals(encounter1Response.getEncounterUuid(), encounter2Response.getEncounterUuid());

    List<EncounterTransaction> encounterTransactions =
        deserialize(
            handle(
                newGetRequest(
                    "/rest/emrapi/encounter",
                    new Parameter[] {
                      new Parameter("visitUuid", visitUuid),
                      new Parameter("encounterDate", "2005-01-01"),
                      new Parameter("includeAll", "false")
                    })),
            new TypeReference<List<EncounterTransaction>>() {});

    assertEquals(1, encounterTransactions.size());
    EncounterTransaction fetchedEncounterTransaction = encounterTransactions.get(0);
    assertEquals(visitUuid, fetchedEncounterTransaction.getVisitUuid());
    assertEquals(
        "a76e8d23-0c38-408c-b2a8-ea5540f01b51", fetchedEncounterTransaction.getPatientUuid());
    assertEquals(
        "b45ca846-c79a-11e2-b0c0-8e397087571c", fetchedEncounterTransaction.getVisitTypeUuid());
    assertEquals(
        "2b377dba-62c3-4e53-91ef-b51c68899891", fetchedEncounterTransaction.getEncounterTypeUuid());
    assertEquals(
        "2005-01-01",
        new SimpleDateFormat("yyyy-MM-dd")
            .format(fetchedEncounterTransaction.getEncounterDateTime()));
    // Assert Observations
    assertEquals(1, fetchedEncounterTransaction.getObservations().size());
    assertEquals(
        "4f3c2244-9d6a-439e-b88a-6e8873489ea7",
        fetchedEncounterTransaction.getObservations().get(0).getConcept().getUuid());
    assertEquals(1, fetchedEncounterTransaction.getObservations().get(0).getGroupMembers().size());
    assertEquals(
        "82e5f23e-e0b3-4e53-b6bb-c09c1c7fb8b0",
        fetchedEncounterTransaction
            .getObservations()
            .get(0)
            .getGroupMembers()
            .get(0)
            .getConcept()
            .getUuid());
    assertEquals(
        20.0,
        fetchedEncounterTransaction.getObservations().get(0).getGroupMembers().get(0).getValue());
    // Assert Diagnosis data
    assertEquals(2, fetchedEncounterTransaction.getDiagnoses().size());
    EncounterTransaction.Diagnosis cancerDiagnosis =
        getDiagnosisByUuid(fetchedEncounterTransaction.getDiagnoses(), cancerDiagnosisUuid);
    assertNotNull(cancerDiagnosis);
    assertEquals("PRIMARY", cancerDiagnosis.getOrder());
    assertEquals("CONFIRMED", cancerDiagnosis.getCertainty());
    assertEquals(cancerDiagnosisUuid, cancerDiagnosis.getCodedAnswer().getUuid());
    assertNotNull(
        getDiagnosisByUuid(fetchedEncounterTransaction.getDiagnoses(), malariaDiagnosisUuid));
    // Assert Disposition data
    EncounterTransaction.Disposition fetchedDisposition =
        fetchedEncounterTransaction.getDisposition();
    assertEquals("ADMIT", fetchedDisposition.getCode());
    assertEquals(
        DateUtils.parseDate(dispositionDateTime, dateTimeFormat),
        fetchedDisposition.getDispositionDateTime());
    assertNotNull(fetchedDisposition.getExistingObs());
    assertEquals(1, fetchedDisposition.getAdditionalObs().size());
    assertEquals("Admit him to ICU.", fetchedDisposition.getAdditionalObs().get(0).getValue());
    assertEquals(
        "Disposition Note", fetchedDisposition.getAdditionalObs().get(0).getConcept().getName());
  }