private void handleEncounterProvider(Provider provider) {
    EncounterProvider encounterProvider = mock(EncounterProvider.class);
    when(encounterProvider.getProvider()).thenReturn(provider);

    Set<EncounterProvider> providerSet = new HashSet<EncounterProvider>();
    providerSet.add(encounterProvider);

    when(encounter.getEncounterProviders()).thenReturn(providerSet);
  }
 @PropertyGetter("encounterProviders")
 public static Set<EncounterProvider> getActiveEncounterProviders(Encounter instance) {
   Set<EncounterProvider> encounterProviders = instance.getEncounterProviders();
   Set<EncounterProvider> providers = new LinkedHashSet<EncounterProvider>();
   for (EncounterProvider encounterProvider : encounterProviders) {
     if (!encounterProvider.isVoided()) {
       providers.add(encounterProvider);
     }
   }
   return providers;
 }
 @Override
 public Encounter save(Encounter delegate) {
   // This is a hack to save encounterProviders correctly. Without this they are created without
   // encounter_id in
   // the database.
   for (EncounterProvider ep : delegate.getEncounterProviders()) {
     ep.setEncounter(delegate);
   }
   Context.getEncounterService().saveEncounter(delegate);
   return delegate;
 }
  @SuppressWarnings("unused")
  public static JSONObject createJsonObservation(Obs obs) {
    JSONObject jsonObs = new JSONObject();
    jsonObs.put("observation_id", obs.getObsId());
    jsonObs.put("concept_name", obs.getConcept().getDisplayString());

    Date obsDate = obs.getObsDatetime() == null ? new Date() : obs.getObsDatetime();

    SimpleDateFormat formatDateJava = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    String dateStr = obsDate.getTime() + "";

    jsonObs.put("date", dateStr);

    if (obs.getConcept().getDatatype().isNumeric()) { // ADD MORE DATATYPES

      ConceptNumeric conceptNumeric =
          Context.getConceptService().getConceptNumeric(obs.getConcept().getId());
      jsonObs.put("units_of_measurement", conceptNumeric.getUnits());
      jsonObs.put("absolute_high", conceptNumeric.getHiAbsolute());
      jsonObs.put("absolute_low", conceptNumeric.getLowAbsolute());
      jsonObs.put("critical_high", conceptNumeric.getHiCritical());
      jsonObs.put("critical_low", conceptNumeric.getLowCritical());
      jsonObs.put("normal_high", conceptNumeric.getHiNormal());
      jsonObs.put("normal_low", conceptNumeric.getLowNormal());
    }
    jsonObs.put("value_type", obs.getConcept().getDatatype().getName());

    jsonObs.put("value", obs.getValueAsString(Context.getLocale()));
    jsonObs.put("location", obs.getLocation().getDisplayString());
    jsonObs.put("creator", obs.getCreator().getDisplayString());
    Set<EncounterProvider> encounterProviders = obs.getEncounter().getEncounterProviders();

    if (encounterProviders != null && encounterProviders.iterator().hasNext()) {
      EncounterProvider provider = encounterProviders.iterator().next();
      if (provider.getProvider() != null) {
        jsonObs.put("provider", provider.getProvider().getName());
      }
    }

    SearchAPI searchAPI = SearchAPI.getInstance();
    if (!searchAPI.getSearchPhrase().getPhrase().equals("")
        && !searchAPI.getSearchPhrase().getPhrase().equals("*")) {
      for (ChartListItem item : searchAPI.getResults()) {
        if (item != null && item instanceof ObsItem && ((ObsItem) item).getObsId() != null) {
          if (((ObsItem) item).getObsId() == obs.getObsId()) {
            jsonObs.put("chosen", "true");
          }
        }
      }
    }

    return jsonObs;
  }
  public static Set<String> generateProvidersFromResults() {
    Set<String> res = new HashSet<String>();
    SearchAPI searchAPI = SearchAPI.getInstance();

    for (ChartListItem item : searchAPI.getResults()) {
      if (item != null && item instanceof ObsItem && ((ObsItem) item).getObsId() != null) {
        int itemObsId = ((ObsItem) item).getObsId();

        Obs obs = Context.getObsService().getObs(itemObsId);
        if (obs != null) {
          Set<EncounterProvider> encounterProviders = obs.getEncounter().getEncounterProviders();

          if (encounterProviders != null && encounterProviders.iterator().hasNext()) {
            EncounterProvider provider = encounterProviders.iterator().next();
            if (provider.getProvider() != null) {
              res.add(provider.getProvider().getName());
            }
          }
        }
      }
    }
    return res;
  }
  /**
   * 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;
  }
 /**
  * @return copied encounter provider
  * @should copy all EncounterProvider data
  */
 public EncounterProvider copy() {
   EncounterProvider target = new EncounterProvider();
   target.setChangedBy(getChangedBy());
   target.setCreator(getCreator());
   target.setDateChanged(getDateChanged());
   target.setDateCreated(getDateCreated());
   target.setDateVoided(getDateVoided());
   target.setVoided(getVoided());
   target.setVoidedBy(getVoidedBy());
   target.setVoidReason(getVoidReason());
   target.setEncounter(getEncounter());
   target.setEncounterRole(getEncounterRole());
   target.setProvider(getProvider());
   return target;
 }