private void searchPatientByAttributes(
      List<Integer> searchingAttributeId, List<String> searchText) {
    // total = patientAttributeValueService.countSearchPatients( searchingAttributeId, searchText );

    // OrganisationUnit orgUnit = null;

    organisationUnit = (isSelectedOrg) ? organisationUnit : null;
    total = patientService.countSearchPatients(searchText, organisationUnit);
    // Collection<Patient> getPatients( String searchText, Integer min, Integer max );

    this.paging = createPaging(total);

    // patients = patientAttributeValueService.searchPatients( searchingAttributeId, searchText,
    // paging.getStartPos(),paging.getPageSize() );
    patients =
        patientService.searchPatients(
            searchText, organisationUnit, paging.getStartPos(), paging.getPageSize());

    /*
    if ( isSelectedOrg )
    {
        Iterator<Patient> patientIterator = patients.iterator();
        while ( patientIterator.hasNext() )
        {
            Patient patient = patientIterator.next();

            OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit( ouIDTB );

            if ( patient.getOrganisationUnit().getId() != orgUnit.getId() )
            {
                patientIterator.remove();
            }
        }
        total = patients.size();
        this.paging = createPaging( total );
    }
    */

    Collection<PatientAttributeValue> attributeValues =
        patientAttributeValueService.getPatientAttributeValues(patients);

    for (Patient patient : patients) {
      // programs.addAll( patient.getPrograms() );

      mapPatientOrgunit.put(patient.getId(), getHierarchyOrgunit(patient.getOrganisationUnit()));

      for (PatientAttributeValue attributeValue : attributeValues) {
        mapPatientPatientAttr.put(
            patient.getId() + "-" + attributeValue.getPatientAttribute().getId(),
            attributeValue.getValue());
      }

      // mapPatientPrograms.put( patient.getId(), getProgramsByPatient( patient ) );
    }
  }
  private void searchPatientByNameAndOrgUnit(
      List<String> searchText, OrganisationUnit organisationUnit) {

    total = patientService.countGetPatientsByOrgUnit(organisationUnit);
    this.paging = createPaging(total);

    // Collection<Patient> tempPatients = new ArrayList<Patient>();
    for (String text : searchText) {
      // tPatients( OrganisationUnit organisationUnit, String searchText, int min, int max )

      List<Patient> tempPatients =
          new ArrayList<Patient>(
              patientService.getPatients(
                  organisationUnit, text, paging.getStartPos(), paging.getPageSize()));
      patients.addAll(tempPatients);
    }

    total = patients.size();
    this.paging = createPaging(total);

    Collection<PatientAttributeValue> attributeValues =
        patientAttributeValueService.getPatientAttributeValues(patients);

    for (Patient patient : patients) {
      // programs.addAll( patient.getPrograms() );

      mapPatientOrgunit.put(patient.getId(), getHierarchyOrgunit(patient.getOrganisationUnit()));

      for (PatientAttributeValue attributeValue : attributeValues) {
        mapPatientPatientAttr.put(
            patient.getId() + "-" + attributeValue.getPatientAttribute().getId(),
            attributeValue.getValue());
      }

      // mapPatientPrograms.put( patient.getId(), getProgramsByPatient( patient ) );
    }
  }
  @Override
  public String execute() throws Exception {
    patient = patientService.getPatient(patientId);

    Collection<Program> programs =
        programService.getProgramsByCurrentUser(Program.MULTIPLE_EVENTS_WITH_REGISTRATION);
    programs.addAll(
        programService.getProgramsByCurrentUser(Program.SINGLE_EVENT_WITH_REGISTRATION));

    // ---------------------------------------------------------------------
    // Get relationship
    // ---------------------------------------------------------------------

    relationships = relationshipService.getRelationshipsForPatient(patient);

    Collection<ProgramInstance> programInstances = patient.getProgramInstances();

    // ---------------------------------------------------------------------
    // Get patient-attribute-values
    // ---------------------------------------------------------------------

    Collection<PatientAttributeValue> _attributeValues =
        patientAttributeValueService.getPatientAttributeValues(patient);
    attributeValues = new HashSet<PatientAttributeValue>();

    for (PatientAttributeValue attributeValue : _attributeValues) {
      String value = attributeValue.getValue();
      if (attributeValue.getPatientAttribute().getValueType().equals(PatientAttribute.TYPE_AGE)) {
        Date date = format.parseDate(value);
        value = PatientAttribute.getAgeFromDate(date) + "";
        attributeValue.setValue(value);
      }

      attributeValues.add(attributeValue);
    }

    // ---------------------------------------------------------------------
    // Get patient-identifiers
    // ---------------------------------------------------------------------

    Collection<PatientIdentifier> _identifiers = patient.getIdentifiers();
    identifiers = new HashSet<PatientIdentifier>();

    for (Program program : programs) {
      Collection<PatientIdentifierType> identifierTypes = program.getIdentifierTypes();
      for (PatientIdentifier identifier : _identifiers) {
        if (!identifierTypes.contains(identifier.getIdentifierType())) {
          identifiers.add(identifier);
        }
      }
    }
    // ---------------------------------------------------------------------
    // Get program enrollment
    // ---------------------------------------------------------------------

    activeProgramInstances = new HashSet<ProgramInstance>();

    completedProgramInstances = new HashSet<ProgramInstance>();

    for (ProgramInstance programInstance : programInstances) {
      if (programs.contains(programInstance.getProgram())) {
        if (programInstance.getStatus() == ProgramInstance.STATUS_ACTIVE) {
          activeProgramInstances.add(programInstance);

          programIndicatorsMap.putAll(
              programIndicatorService.getProgramIndicatorValues(programInstance));
        } else {
          completedProgramInstances.add(programInstance);
        }
      }
    }

    // ---------------------------------------------------------------------
    // Patient-Audit
    // ---------------------------------------------------------------------

    patientAudits = patientAuditService.getPatientAudits(patient);

    Calendar today = Calendar.getInstance();
    PeriodType.clearTimeOfDay(today);
    Date date = today.getTime();
    String visitor = currentUserService.getCurrentUsername();
    PatientAudit patientAudit =
        patientAuditService.getPatientAudit(
            patient.getId(), visitor, date, PatientAudit.MODULE_PATIENT_DASHBOARD);
    if (patientAudit == null) {
      patientAudit =
          new PatientAudit(patient, visitor, date, PatientAudit.MODULE_PATIENT_DASHBOARD);
      patientAuditService.savePatientAudit(patientAudit);
    }

    return SUCCESS;
  }