public List<SimpleObject> getPatients(
      @RequestParam(value = "searchValue", required = true) String searchValue,
      @RequestParam(value = "excludePatientsOf", required = false) Person excludePatientsOf,
      @RequestParam(value = "existingRelationshipTypeToExclude", required = false)
          RelationshipType existingRelationshipTypeToExclude,
      @RequestParam(value = "resultFields[]", required = true) String[] resultFields,
      UiUtils ui)
      throws PersonIsNotProviderException, InvalidRelationshipTypeException {

    if (resultFields == null || resultFields.length == 0) {
      resultFields = new String[] {"personName"};
    }

    // always want to return the id of the result objects
    resultFields = ArrayUtils.add(resultFields, "id");

    // now fetch the results
    List<Patient> patients = Context.getPatientService().getPatients(searchValue);

    // exclude any patients if specified
    if (excludePatientsOf != null && existingRelationshipTypeToExclude != null) {
      List<Patient> patientsToExclude =
          Context.getService(ProviderManagementService.class)
              .getPatientsOfProvider(
                  excludePatientsOf, existingRelationshipTypeToExclude, new Date());
      patients.removeAll(patientsToExclude);
    }

    return SimpleObject.fromCollection(patients, ui, resultFields);
  }
  SimpleObject simplify(UiUtils ui, EmrApiProperties emrApiProperties, Patient patient) {
    PersonName name = patient.getPersonName();
    SimpleObject preferredName =
        SimpleObject.fromObject(name, ui, "givenName", "middleName", "familyName", "familyName2");
    preferredName.put("name", ui.format(name));

    PatientIdentifierType primaryIdentifierType = emrApiProperties.getPrimaryIdentifierType();
    List<PatientIdentifier> primaryIdentifiers =
        patient.getPatientIdentifiers(primaryIdentifierType);

    SimpleObject o =
        SimpleObject.fromObject(
            patient, ui, "patientId", "gender", "age", "birthdate", "birthdateEstimated");
    o.put("preferredName", preferredName);
    o.put("primaryIdentifiers", SimpleObject.fromCollection(primaryIdentifiers, ui, "identifier"));

    return o;
  }