public EncounterListItem(Encounter encounter) {

    if (encounter != null) {
      encounterId = encounter.getEncounterId();
      encounterDateTime = encounter.getEncounterDatetime();
      encounterDateString = Format.format(encounter.getEncounterDatetime());
      PersonName pn = encounter.getPatient().getPersonName();
      if (pn != null) {
        personName = "";
        if (pn.getGivenName() != null) {
          personName += pn.getGivenName();
        }
        if (pn.getMiddleName() != null) {
          personName += " " + pn.getMiddleName();
        }
        if (pn.getFamilyName() != null) {
          personName += " " + pn.getFamilyName();
        }
      }
      if (encounter.getProvider() != null) {
        providerName = encounter.getProvider().getPersonName().getFullName();
      }
      if (encounter.getLocation() != null) {
        location = encounter.getLocation().getName();
      }
      if (encounter.getEncounterType() != null) {
        encounterType = encounter.getEncounterType().getName();
      }
      if (encounter.getForm() != null) {
        formName = encounter.getForm().getName();
        formId = encounter.getForm().getFormId();
      }
      voided = encounter.isVoided();
      if (encounter.getCreator() != null) {
        PersonName entererPersonName = encounter.getCreator().getPersonName();
        if (entererPersonName != null) {
          entererName = "";
          if (entererPersonName.getGivenName() != null) {
            entererName += entererPersonName.getGivenName();
          }
          if (entererPersonName.getMiddleName() != null) {
            entererName += " " + entererPersonName.getMiddleName();
          }
          if (entererPersonName.getFamilyName() != null) {
            entererName += " " + entererPersonName.getFamilyName();
          }
        }
      }
    }
  }
 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()));
       }
     }
   }
 }
    /**
     *
     * Generates all of the data rows
     *
     * @param form
     * @param extraCols
     * @param sb
     * @return
     * @throws Exception
     */
    public static String generateColumnDataFromHtmlForm(List<Encounter> encounters, HtmlForm form, List<String> extraCols, StringBuffer sb, Locale locale,List<PatientIdentifierType> pitList) throws Exception {
        for (Encounter e: encounters){

            sb.append(DEFAULT_QUOTE).append(e.getEncounterId()).append(DEFAULT_QUOTE).append(DEFAULT_COLUMN_SEPARATOR);
            sb.append(DEFAULT_QUOTE).append(DATE_FORMATTER.format(e.getEncounterDatetime())).append(DEFAULT_QUOTE).append(DEFAULT_COLUMN_SEPARATOR);
            sb.append(DEFAULT_QUOTE).append(e.getLocation().getName()).append(DEFAULT_QUOTE).append(DEFAULT_COLUMN_SEPARATOR);
            sb.append(DEFAULT_QUOTE).append(EncounterCompatibility.getProvider(e).getGivenName()+ " " + EncounterCompatibility.getProvider(e).getFamilyName()).append(DEFAULT_QUOTE).append(DEFAULT_COLUMN_SEPARATOR);
            sb.append(DEFAULT_QUOTE).append((e.getPatient() != null ? e.getPatient().getPatientId() : EMPTY)).append(DEFAULT_QUOTE).append(DEFAULT_COLUMN_SEPARATOR);       
            int index = 1;
            for (PatientIdentifierType pit :  pitList){
                sb.append(DEFAULT_QUOTE).append(e.getPatient().getPatientIdentifier(pit)).append(DEFAULT_QUOTE);
                if (index < pitList.size())
                    sb.append(DEFAULT_COLUMN_SEPARATOR);
                index ++;
            }

            FormEntrySession session = new FormEntrySession(e.getPatient(), e, Mode.VIEW, form, null); // session doesn't get HttpSession
            session.getHtmlToDisplay();
            FormSubmissionController  fsa = session.getSubmissionController();
            List<FormSubmissionControllerAction> actions = fsa.getActions();
            for (FormSubmissionControllerAction fsca : actions){
                if (fsca instanceof ObsSubmissionElement){
                    ObsSubmissionElement ose = (ObsSubmissionElement) fsca;
                    sb = appendObsToRow(ose, sb, extraCols, locale);
                } else {
                    //TODO: add programs, orders, logic, etc...
                    // just make sure these are in the headers too...
                }
            }
            session = null;
            sb.append(DEFAULT_LINE_SEPARATOR);
        }
        return sb.toString();
    }
  @Override
  public List<VisitDomainWrapper> getInpatientVisits(Location visitLocation, Location ward) {

    if (visitLocation == null) {
      throw new IllegalArgumentException("Location is required");
    }
    Set<Location> locations = getChildLocationsRecursively(visitLocation, null);
    List<Visit> candidates =
        visitService.getVisits(
            null, null, locations, null, null, null, null, null, null, false, false);

    List<VisitDomainWrapper> inpatientVisits = new ArrayList<VisitDomainWrapper>();
    for (Visit candidate : candidates) {
      VisitDomainWrapper visitDomainWrapper = wrap(candidate);
      if (itBelongsToARealPatient(candidate) && visitDomainWrapper.isAdmitted()) {
        if (ward != null) {
          Encounter latestAdtEncounter = visitDomainWrapper.getLatestAdtEncounter();
          if (latestAdtEncounter != null
              && (latestAdtEncounter.getLocation().getId().compareTo(ward.getId()) == 0)) {
            inpatientVisits.add(visitDomainWrapper);
          }
        } else {
          inpatientVisits.add(visitDomainWrapper);
        }
      }
    }

    return inpatientVisits;
  }
  @Test
  public void shouldChangeObservationsOfPreviousEncounters() throws Exception {
    Date visitStartDate = getDateFromString("2014-06-22 00:00:00");
    Date encounterDate = getDateFromString("2014-06-23 00:00:00");

    List<Document> documents = new ArrayList<>();
    documents.add(
        new Document(
            "/radiology/foo.jpg",
            null,
            "5f596de5-5caa-11e3-a4c0-0800271c1b75",
            "6d0ae386-707a-4629-9850-f15206e63kj0",
            encounterDate,
            false));

    VisitDocumentRequest visitDocumentRequest =
        new VisitDocumentRequest(
            patientUUID,
            firstVisitUuid,
            visitTypeUUID,
            visitStartDate,
            null,
            secondEncounterTypeUUID,
            encounterDate,
            documents,
            secondProviderUuid,
            secondLocationUuid,
            null);
    executeDataSet("visitDocumentData.xml");
    visitDocumentService.upload(visitDocumentRequest);

    Context.flushSession();
    Context.clearSession();

    Encounter encounter = encounterService.getEncounterByUuid(secondEncounterUuid);
    for (Obs obs : encounter.getAllObs(true)) {
      if (obs.getUuid().equals("6d0ae386-707a-4629-9850-f15606e63666")
          || obs.getUuid().equals("6d0ae386-707a-4629-9850-f15206e63kj0")) {
        assertThat(obs.getVoided(), is(true));
      }
    }

    Obs savedDoc = getSavedDocument(encounter.getAllObs(), "5f596de5-5caa-11e3-a4c0-0800271c1b75");

    assertNotNull(savedDoc);
    assertThat(savedDoc.getConcept().getId(), is(333));
    assertThat(
        savedDoc.getGroupMembers().iterator().next().getValueText(), is("/radiology/foo.jpg"));
    assertEquals(FIRST_LOCATION_UUID, encounter.getLocation().getUuid());
  }
  /**
   * This method is synchronized to prevent multiple check-ins in a row at the same location and
   * during the same visit. See #579.
   *
   * @see org.openmrs.module.emrapi.adt.AdtService#checkInPatient(org.openmrs.Patient,
   *     org.openmrs.Location, org.openmrs.Provider, java.util.List, java.util.List, boolean)
   */
  @Override
  @Transactional
  public synchronized Encounter checkInPatient(
      Patient patient,
      Location where,
      Provider checkInClerk,
      List<Obs> obsForCheckInEncounter,
      List<Order> ordersForCheckInEncounter,
      boolean newVisit) {
    if (checkInClerk == null) {
      checkInClerk = getProvider(Context.getAuthenticatedUser());
    }

    Visit activeVisit = getActiveVisitHelper(patient, where);

    if (activeVisit != null && newVisit) {
      closeAndSaveVisit(activeVisit);
      activeVisit = null;
    }

    if (activeVisit == null) {
      activeVisit = ensureActiveVisit(patient, where);
    }

    Encounter lastEncounter = getLastEncounter(patient);
    if (lastEncounter != null
        && activeVisit.equals(lastEncounter.getVisit())
        && emrApiProperties.getCheckInEncounterType().equals(lastEncounter.getEncounterType())
        && where.equals(lastEncounter.getLocation())) {
      log.warn(
          "Patient id:{} tried to check-in twice in a row at id:{} during the same visit",
          patient.getId(),
          where.getId());
      return lastEncounter;
    }

    Encounter encounter =
        buildEncounter(
            emrApiProperties.getCheckInEncounterType(),
            patient,
            where,
            null,
            new Date(),
            obsForCheckInEncounter,
            ordersForCheckInEncounter);
    encounter.addProvider(emrApiProperties.getCheckInClerkEncounterRole(), checkInClerk);
    activeVisit.addEncounter(encounter);
    encounterService.saveEncounter(encounter);
    return encounter;
  }
  @Test
  public void shouldCreateObservations() throws Exception {
    Date visitStartDate = getDateFromString("2014-06-22 00:00:00");
    Date encounterDate = getDateFromString("2014-06-23 00:00:00");
    Date obsDate = getDateFromString("2014-06-24 00:10:00");

    List<Document> documents = new ArrayList<>();
    documents.add(new Document("/radiology/fooo-bar.jpg", null, conceptUuid, null, obsDate, false));

    visitDocumentRequest =
        new VisitDocumentRequest(
            patientUUID,
            secondVisitUuid,
            visitTypeUUID,
            visitStartDate,
            null,
            firstEncounterTypeUUID,
            encounterDate,
            documents,
            firstProviderUuid,
            FIRST_LOCATION_UUID,
            null);

    visitDocumentService.upload(visitDocumentRequest);

    Context.flushSession();
    Context.clearSession();

    Encounter encounter = encounterService.getEncounterByUuid(firstEncounterUuid);

    Obs savedDoc = getSavedDocument(encounter.getAllObs(), conceptUuid);

    assertNotNull(savedDoc);
    assertThat(savedDoc.getConcept().getId(), is(222));
    assertThat(
        savedDoc.getGroupMembers().iterator().next().getValueText(), is("/radiology/fooo-bar.jpg"));
    assertEquals(FIRST_LOCATION_UUID, encounter.getLocation().getUuid());
  }
  /**
   * Adds the column headers and column data to the DataSet
   *
   * @param dataSet
   * @param encounters
   * @param patientIdentifierTypes
   * @param optionalColumns
   * @param columnDisplayFormat
   * @param maxColumnHeaderWidth
   * @param allColumns
   * @param fieldMap
   * @return
   */
  public DataSet addData(
      SimpleDataSet dataSet,
      List<Encounter> encounters,
      List<PatientIdentifierType> patientIdentifierTypes,
      List<EncounterAndObsDataSetDefinition.ObsOptionalColumn> optionalColumns,
      List<EncounterAndObsDataSetDefinition.ColumnDisplayFormat> columnDisplayFormat,
      Integer maxColumnHeaderWidth,
      Set<ObsColumnDescriptor> allColumns,
      Map<Encounter, Map<ObsColumnDescriptor, Obs>> fieldMap) {
    for (Encounter encounter : encounters) {

      DataSetRow row = new DataSetRow();

      List<String> providerNames = new ArrayList<String>();
      for (EncounterProvider ep : encounter.getEncounterProviders()) {
        providerNames.add(ep.getProvider().getName());
      }

      // Add the standard columns for encounters
      DataSetColumn c1 =
          new DataSetColumn(
              ObjectUtil.trimStringIfNeeded("ENCOUNTER_ID", maxColumnHeaderWidth),
              ObjectUtil.trimStringIfNeeded("ENCOUNTER_ID", maxColumnHeaderWidth),
              Integer.class);
      row.addColumnValue(c1, encounter.getEncounterId());
      DataSetColumn c2 =
          new DataSetColumn(
              ObjectUtil.trimStringIfNeeded("ENCOUNTER_DATETIME", maxColumnHeaderWidth),
              ObjectUtil.trimStringIfNeeded("ENCOUNTER_DATETIME", maxColumnHeaderWidth),
              String.class);
      row.addColumnValue(c2, encounter.getEncounterDatetime().toString());
      DataSetColumn c3 =
          new DataSetColumn(
              ObjectUtil.trimStringIfNeeded("LOCATION", maxColumnHeaderWidth),
              ObjectUtil.trimStringIfNeeded("LOCATION", maxColumnHeaderWidth),
              String.class);
      row.addColumnValue(
          c3, (encounter.getLocation() != null) ? encounter.getLocation().getName() : EMPTY);
      DataSetColumn c4 =
          new DataSetColumn(
              ObjectUtil.trimStringIfNeeded("PROVIDER", maxColumnHeaderWidth),
              ObjectUtil.trimStringIfNeeded("PROVIDER", maxColumnHeaderWidth),
              String.class);
      row.addColumnValue(c4, OpenmrsUtil.join(providerNames, ", "));
      DataSetColumn c5 =
          new DataSetColumn(
              ObjectUtil.trimStringIfNeeded("INTERNAL_PATIENT_ID", maxColumnHeaderWidth),
              ObjectUtil.trimStringIfNeeded("INTERNAL_PATIENT_ID", maxColumnHeaderWidth),
              Integer.class);
      row.addColumnValue(
          c5, encounter.getPatient() != null ? encounter.getPatient().getPatientId() : EMPTY);

      if (patientIdentifierTypes != null) {
        for (PatientIdentifierType pit : patientIdentifierTypes) {
          List<PatientIdentifier> patientIdentifiers =
              encounter.getPatient().getPatientIdentifiers(pit);

          StringBuffer sbPatientIdentifiers = new StringBuffer();
          int count = 0;
          for (PatientIdentifier patientIdentifier : patientIdentifiers) {
            if (count > 0) {
              sbPatientIdentifiers.append(", ");
            }
            sbPatientIdentifiers.append(patientIdentifier.toString());
            count++;
          }

          DataSetColumn c6 =
              new DataSetColumn(
                  pit.getName(),
                  ObjectUtil.trimStringIfNeeded(pit.getName(), maxColumnHeaderWidth),
                  String.class);
          row.addColumnValue(c6, sbPatientIdentifiers.toString());
        }
      }

      Map<ObsColumnDescriptor, Obs> obsInEncounter = fieldMap.get(encounter);

      // Look up all obs for a given encounter based on column headers for all encounters
      for (ObsColumnDescriptor columnKey : allColumns) {

        Obs obs = obsInEncounter.get(columnKey);
        String columnName = columnKey.format(columnDisplayFormat, maxColumnHeaderWidth);
        DataSetColumn obsDsc = new DataSetColumn(columnName, columnName, String.class);

        StringBuffer columnValue = new StringBuffer();
        if (obs != null && obs.getValueCoded() != null) {
          if (columnDisplayFormat.contains(
              EncounterAndObsDataSetDefinition.ColumnDisplayFormat.ID)) {
            columnValue.append(obs.getValueCoded());
          }

          if (columnDisplayFormat.contains(EncounterAndObsDataSetDefinition.ColumnDisplayFormat.ID)
              && columnDisplayFormat.contains(
                  EncounterAndObsDataSetDefinition.ColumnDisplayFormat.BEST_SHORT_NAME)) {
            columnValue.append("_");
          }

          if (columnDisplayFormat.contains(
              EncounterAndObsDataSetDefinition.ColumnDisplayFormat.BEST_SHORT_NAME)) {
            String conceptName = obs.getValueAsString(Context.getLocale());
            columnValue.append(
                maxColumnHeaderWidth != null
                        && conceptName.length() > maxColumnHeaderWidth - columnValue.length()
                    ? conceptName.substring(0, maxColumnHeaderWidth - columnValue.length() - 1)
                    : conceptName);
          }
          row.addColumnValue(obsDsc, (obs != null) ? columnValue.toString() : EMPTY);
        } else {
          row.addColumnValue(
              obsDsc, (obs != null) ? obs.getValueAsString(Context.getLocale()) : EMPTY);
        }

        String dateColumnName =
            columnKey.format(
                columnDisplayFormat,
                maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 5 : null);
        DataSetColumn obsDscDate =
            new DataSetColumn(dateColumnName + "_DATE", dateColumnName + "_DATE", String.class);
        row.addColumnValue(obsDscDate, (obs != null) ? obs.getObsDatetime().toString() : EMPTY);

        String parentColumnName =
            columnKey.format(
                columnDisplayFormat,
                maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 7 : null);
        DataSetColumn obsDscParent =
            new DataSetColumn(
                parentColumnName + "_PARENT", parentColumnName + "_PARENT", String.class);
        row.addColumnValue(
            obsDscParent,
            (obs != null && obs.getObsGroup() != null) ? obs.getObsGroup().getId() : EMPTY);

        if (optionalColumns != null) {

          if (optionalColumns.contains(
              EncounterAndObsDataSetDefinition.ObsOptionalColumn.VALUE_MODIFIER)) {
            String valModColumnName =
                columnKey.format(
                    columnDisplayFormat,
                    maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 10 : null);
            DataSetColumn obsDscValueModifier =
                new DataSetColumn(
                    valModColumnName + "_VALUE_MOD", valModColumnName + "_VALUE_MOD", String.class);
            row.addColumnValue(obsDscValueModifier, (obs != null) ? obs.getValueModifier() : EMPTY);
          }
          if (optionalColumns.contains(
              EncounterAndObsDataSetDefinition.ObsOptionalColumn.ACCESSION_NUMBER)) {
            String accessionNumColumnName =
                columnKey.format(
                    columnDisplayFormat,
                    maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 14 : null);
            DataSetColumn obsDscAccessionNumber =
                new DataSetColumn(
                    accessionNumColumnName + "_ACCESSION_NUM",
                    accessionNumColumnName + "_ACCESSION_NUM",
                    String.class);
            row.addColumnValue(
                obsDscAccessionNumber, (obs != null) ? obs.getAccessionNumber() : EMPTY);
          }
          if (optionalColumns.contains(
              EncounterAndObsDataSetDefinition.ObsOptionalColumn.COMMENT)) {
            String commentColumnName =
                columnKey.format(
                    columnDisplayFormat,
                    maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 8 : null);
            DataSetColumn obsDscComment =
                new DataSetColumn(
                    commentColumnName + "_COMMENT", commentColumnName + "_COMMENT", String.class);
            row.addColumnValue(obsDscComment, (obs != null) ? obs.getComment() : EMPTY);
          }
        }
      }

      dataSet.addRow(row);
    }
    return dataSet;
  }
  public void controller(
      @RequestParam(value = "patientId", required = false) Patient patient,
      @RequestParam(value = "headerForm") String headerForm,
      @RequestParam(value = "flowsheets") String[] flowsheets,
      @RequestParam(value = "viewOnly", required = false) Boolean viewOnly,
      @RequestParam(value = "requireEncounter", required = false) Boolean requireEncounter,
      UiUtils ui,
      PageModel model,
      @SpringBean("htmlFormEntryService") HtmlFormEntryService htmlFormEntryService,
      @SpringBean("formService") FormService formService,
      @SpringBean("locationService") LocationService locationService,
      @SpringBean("coreResourceFactory") ResourceFactory resourceFactory,
      @InjectBeans PatientDomainWrapper patientDomainWrapper,
      PageRequest pageRequest) {

    patientDomainWrapper.setPatient(patient);
    model.addAttribute("patient", patientDomainWrapper);
    model.addAttribute("headerForm", headerForm);
    model.addAttribute("flowsheets", flowsheets);
    model.addAttribute("requireEncounter", (requireEncounter == null || requireEncounter));

    Location defaultLocation = null;
    Integer locationId =
        pageRequest
            .getSession()
            .getAttribute(PihMalawiWebConstants.SESSION_LOCATION_ID, Integer.TYPE);
    if (locationId != null) {
      defaultLocation = locationService.getLocation(locationId);
    }

    List<Encounter> allEncounters = new ArrayList<Encounter>();

    List<String> alerts = new ArrayList<String>();

    String headerFormResource = "pihmalawi:htmlforms/" + headerForm + ".xml";

    HtmlForm headerHtmlForm =
        getHtmlFormFromResource(
            headerFormResource, resourceFactory, formService, htmlFormEntryService);
    model.addAttribute("headerForm", headerForm);

    Encounter headerEncounter = null;
    List<Encounter> headerEncounters = getEncountersForForm(patient, headerHtmlForm);
    if (headerEncounters.size() > 0) {
      headerEncounter = headerEncounters.get(headerEncounters.size() - 1); // Most recent
      if (headerEncounters.size() > 1) {
        alerts.add(
            "WARNING:  More than one "
                + headerHtmlForm.getName()
                + " encounters exist for this patient.  Displaying the most recent only.");
      }
      allEncounters.add(headerEncounter);
    }
    model.addAttribute("headerEncounter", headerEncounter);

    Map<String, HtmlForm> flowsheetForms = new LinkedHashMap<String, HtmlForm>();
    Map<String, List<Integer>> flowsheetEncounters = new LinkedHashMap<String, List<Integer>>();
    if (flowsheets != null) {
      for (String flowsheet : flowsheets) {
        String flowsheetResource = "pihmalawi:htmlforms/" + flowsheet + ".xml";
        HtmlForm htmlForm =
            getHtmlFormFromResource(
                flowsheetResource, resourceFactory, formService, htmlFormEntryService);
        flowsheetForms.put(flowsheet, htmlForm);
        List<Integer> encIds = new ArrayList<Integer>();
        List<Encounter> encounters = getEncountersForForm(patient, htmlForm);
        for (Encounter e : encounters) {
          encIds.add(e.getEncounterId());
          allEncounters.add(e);
        }
        flowsheetEncounters.put(flowsheet, encIds);
      }
    }
    model.addAttribute("flowsheetForms", flowsheetForms);
    model.addAttribute("flowsheetEncounters", flowsheetEncounters);

    model.addAttribute("alerts", alerts);

    if (defaultLocation == null) {
      Date maxDate = null;
      if (allEncounters.size() > 0) {
        for (Encounter e : allEncounters) {
          if (maxDate == null || maxDate.compareTo(e.getEncounterDatetime()) < 0) {
            maxDate = e.getEncounterDatetime();
            defaultLocation = e.getLocation();
          }
        }
      }
    }
    model.addAttribute(
        "defaultLocationId", defaultLocation == null ? null : defaultLocation.getLocationId());
    model.addAttribute("viewOnly", viewOnly == Boolean.TRUE);

    model.addAttribute(
        "returnUrl",
        ui.pageLink(
            "pihmalawi",
            "mastercard",
            SimpleObject.create(
                "patientId",
                patient.getId(),
                "headerForm",
                headerForm,
                "flowsheets",
                flowsheets,
                "viewOnly",
                viewOnly)));
  }
    /**
     *
     * 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;
    }
  /** @see org.openmrs.api.EncounterService#saveEncounter(org.openmrs.Encounter) */
  public Encounter saveEncounter(Encounter encounter) throws APIException {

    // if authenticated user is not supposed to edit encounter of certain type
    if (!canEditEncounter(encounter, null)) {
      throw new APIException(
          "Encounter.error.privilege.required.edit",
          new Object[] {encounter.getEncounterType().getEditPrivilege()});
    }

    // If new encounter, try to assign a visit using the registered visit assignment handler.
    if (encounter.getEncounterId() == null) {

      // Am using Context.getEncounterService().getActiveEncounterVisitHandler() instead of just
      // getActiveEncounterVisitHandler() for modules which may want to AOP around this call.
      EncounterVisitHandler encounterVisitHandler =
          Context.getEncounterService().getActiveEncounterVisitHandler();
      if (encounterVisitHandler != null) {
        encounterVisitHandler.beforeCreateEncounter(encounter);

        // If we have been assigned a new visit, persist it.
        if (encounter.getVisit() != null && encounter.getVisit().getVisitId() == null) {
          Context.getVisitService().saveVisit(encounter.getVisit());
        }
      }
    }

    boolean isNewEncounter = false;
    Date newDate = encounter.getEncounterDatetime();
    Date originalDate = null;
    Location newLocation = encounter.getLocation();
    Location originalLocation = null;
    // check permissions
    if (encounter.getEncounterId() == null) {
      isNewEncounter = true;
      Context.requirePrivilege(PrivilegeConstants.ADD_ENCOUNTERS);
    } else {
      Context.requirePrivilege(PrivilegeConstants.EDIT_ENCOUNTERS);
    }

    // This must be done after setting dateCreated etc on the obs because
    // of the way the ORM tools flush things and check for nullity
    // This also must be done before the save encounter so we can use the
    // orig date
    // after the save
    Patient p = encounter.getPatient();

    if (!isNewEncounter) {
      // fetch the datetime from the database prior to saving for this
      // encounter
      // to see if it has changed and change all obs after saving if so
      originalDate = dao.getSavedEncounterDatetime(encounter);
      if (encounter.getLocation() != null) {
        originalLocation = dao.getSavedEncounterLocation(encounter);
      }
      // Our data model duplicates the patient column to allow for
      // observations to
      // not have to look up the parent Encounter to find the patient
      // Therefore, encounter.patient must always equal
      // encounter.observations[0-n].patient

      // If we are changing encounter.encounterDatetime, then we need to
      // also apply that
      // to Obs that inherited their obsDatetime from the encounter in the
      // first place

      for (Obs obs : encounter.getAllObs(true)) {
        // if the date was changed
        if (OpenmrsUtil.compare(originalDate, newDate) != 0
            && OpenmrsUtil.compare(obs.getObsDatetime(), originalDate) == 0) {

          // if the obs datetime is the same as the
          // original encounter datetime, fix it
          obs.setObsDatetime(newDate);
        }

        if (!OpenmrsUtil.nullSafeEquals(newLocation, originalLocation)
            && obs.getLocation().equals(originalLocation)) {
          obs.setLocation(newLocation);
        }

        // if the Person in the obs doesn't match the Patient in the
        // encounter, fix it
        if (!obs.getPerson().getPersonId().equals(p.getPatientId())) {
          obs.setPerson(p);
        }
      }
    }
    // same goes for Orders
    for (Order o : encounter.getOrders()) {
      if (!p.equals(o.getPatient())) {
        o.setPatient(p);
      }
    }

    // do the actual saving to the database
    dao.saveEncounter(encounter);

    // save the new orders
    for (Order o : encounter.getOrders()) {
      if (o.getOrderId() == null) {
        Context.getOrderService().saveOrder(o, null);
      }
    }
    return encounter;
  }