public static Encounter getLatestEncounterByTreatment(Patient patient, String treatmentType) {
   List<Encounter> encounters = Context.getEncounterService().getEncountersByPatient(patient);
   Encounter treatmentEncounter = null;
   for (Encounter encounter : encounters) {
     if (!encounter.isVoided() && treatmentType.equals(encounter.getEncounterType().getName())) {
       if (treatmentEncounter == null) treatmentEncounter = encounter;
       else if (treatmentEncounter.getDateCreated().before(encounter.getDateCreated()))
         treatmentEncounter = encounter;
     }
   }
   return treatmentEncounter;
 }
 public void printEncounterCreated() {
   if (encounterCreated == null) {
     System.out.println("No encounter created");
   } else {
     System.out.println("=== Encounter created ===");
     System.out.println(
         "Created: "
             + encounterCreated.getDateCreated()
             + "  Edited: "
             + encounterCreated.getDateChanged());
     System.out.println("Date: " + encounterCreated.getEncounterDatetime());
     System.out.println("Location: " + encounterCreated.getLocation().getName());
     System.out.println("Provider: " + encounterCreated.getProvider().getPersonName());
     System.out.println("    (obs)");
     Collection<Obs> obs = encounterCreated.getAllObs(false);
     if (obs == null) {
       System.out.println("None");
     } else {
       for (Obs o : obs) {
         System.out.println(
             o.getConcept().getName() + " -> " + o.getValueAsString(Context.getLocale()));
       }
     }
   }
 }
  @Test
  public void testEvaluate() throws Exception {
    EncounterEvaluationContext context = new EncounterEvaluationContext();
    context.setBaseEncounters(new EncounterIdSet(encounter.getId()));

    EvaluatedEncounterData result =
        encounterDataService.evaluate(new AuditInfoEncounterDataDefinition(), context);
    assertThat(result.getData().size(), is(1));
    AuditInfo auditInfo = (AuditInfo) result.getData().get(encounter.getId());
    assertThat(auditInfo.getCreator(), is(encounter.getCreator()));
    assertThat(auditInfo.getDateCreated(), is(encounter.getDateCreated()));
    assertThat(auditInfo.getChangedBy(), is(encounter.getChangedBy()));
    assertThat(auditInfo.getDateChanged(), is(encounter.getDateChanged()));
    assertThat(auditInfo.getVoided(), is(encounter.getVoided()));
    assertThat(auditInfo.getVoidedBy(), is(encounter.getVoidedBy()));
    assertThat(auditInfo.getDateVoided(), is(encounter.getDateVoided()));
    assertThat(auditInfo.getVoidReason(), is(encounter.getVoidReason()));
  }
    /**
     *
     * Returns the encounter with the obs that aren't used when populating form are removed.
     * This *doesn't* save the encounter.
     * TODO: handle Orders?
     *
     * @param e
     * @param htmlform
     * @return
     * @throws Exception
     */
    public static Encounter trimEncounterToMatchForm(Encounter e, HtmlForm htmlform) throws Exception {

       //this should move existing obs from session to tag handlers.
        FormEntrySession session = new FormEntrySession(e.getPatient(), e, FormEntryContext.Mode.VIEW, htmlform, null); // session gets a null HttpSession
        session.getHtmlToDisplay();

        if (log.isDebugEnabled()){
            Map<Concept, List<Obs>>  map = session.getContext().getExistingObs();
            if (map != null){
                for (Map.Entry<Concept, List<Obs>> existingObs : map.entrySet()){
                    List<Obs> oList = existingObs.getValue();
                    for (Obs oInner : oList)
                        log.debug("Obs in existingObs " + existingObs.getKey() + " " + oInner.getConcept());
                }
            }
            Map<Obs, Set<Obs>> map2 = session.getContext().getExistingObsInGroups();
            if (map2 != null){
                for (Map.Entry<Obs, Set<Obs>> existingObsInGroups : map2.entrySet()){
                    Set<Obs> oList = existingObsInGroups.getValue();
                    for (Obs oInner : oList)
                        log.debug("Obs in existingObsInGroups " + existingObsInGroups.getKey().getConcept() + " " + oInner.getConcept());
                }
            }
        }
        Encounter ret = new Encounter();
        ret.setCreator(e.getCreator());
        ret.setEncounterDatetime(e.getEncounterDatetime());
        EncounterCompatibility.setProvider(ret, EncounterCompatibility.getProvider(e));
        ret.setLocation(e.getLocation());
        ret.setDateCreated(e.getDateCreated());
        ret.setPatient(e.getPatient());
        //renders new encounter unsave-able:
        ret.setEncounterId(e.getEncounterId());

        for (Obs oTest : e.getAllObs()){
            boolean found = false;
            if (session.getContext().getExistingObs() != null && !oTest.isObsGrouping()){
                List<Obs> obsList = session.getContext().getExistingObs().get(oTest.getConcept());
                if (obsList != null && obsList.size() > 0){
                    for (Obs o : obsList){
                        if (o.getObsId().equals(oTest.getObsId())){
                            found = true;
                            continue;
                        }
                    }
                }
            }
            if (!found && session.getContext().getExistingObsInGroups() != null){
                for (Map.Entry<Obs, Set<Obs>> mapEntry : session.getContext().getExistingObsInGroups().entrySet()){
                    if (mapEntry.getKey().equals(oTest)){
                        found = true;
                        continue;
                    } else {
                        Set<Obs> oSet = mapEntry.getValue();
                        //note: oSet.contains fails for some reason
                        for (Obs o:oSet){
                              if (o.getObsId().equals(oTest.getObsId())){
                                  found = true;
                                  continue;
                              }
                        }
                    }
                }
            }
            if (!found)
                ret.addObs(oTest);
        }
        session = null;
        return ret;
    }