public DementialManualClassAssemblyVoCollection listPatientsManualAssembley(
      DementiaWorklistSearchCriteriaVo searchCriteria) {
    String hqlSB = "";
    PatIdType dispIdType =
        PatIdType.getNegativeInstance(ConfigFlag.UI.DISPLAY_PATID_TYPE.getValue());
    hqlSB =
        "select new ims.clinical.helper.DementialManualClass( dem.id, pattitle.text, pat.name.surname, pat.name.forename,"
            + "(select max(p21_1.value) from Patient as p11_1 left join p11_1.identifiers as p21_1 where (p21_1.type = "
            + dispIdType.getID()
            + " and p11_1.id = pat.id)) as HOSPNUM, "
            + " pat.dob, "
            + " admward.name, mostitle.text, mos.name.surname, mos.name.forename, demstatus.id, demstatus.text,"
            + " demstatexcl.text, dem.isExcludedFromWorklist, "
            + " dem.dischargeDateTime,"
            + " dem.breachDateTime, "
            + "dem.deliriumConfirmed, dem.dementiaConfirmed ) ";

    hqlSB +=
        "from Dementia as dem left join dem.patient as pat left join pat.name.title as pattitle left join pat.identifiers as patident "
            + "left join dem.currentWorklistStatus as demstat left join demstat.status as demstatus left join demstat.reasonForExclusion as demstatexcl "
            + "left join dem.admissionDetail as adm left join adm.ward as admward left join adm.consultant as cons left join cons.mos as mos left join mos.name.title as mostitle left join adm.specialty as spec";

    StringBuffer hqlConditions = new StringBuffer();
    ArrayList<String> markers = new ArrayList<String>();
    ArrayList<Object> values = new ArrayList<Object>();

    String andStr = "";

    // work list should not list patient with DOD or Dementia is RIE
    hqlConditions.append(andStr);
    hqlConditions.append(" pat.dod is null and dem.isRIE is null ");
    andStr = " and ";

    if (searchCriteria.getWardIsNotNull()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" admward.id=:ward ");
      markers.add("ward");
      values.add(searchCriteria.getWard().getID_Location());
      andStr = " and ";
    } else if (searchCriteria.getHospitalIsNotNull()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" admward.parentLocation.id = :hospital");
      markers.add("hospital");
      values.add(searchCriteria.getHospital().getID_Location());
      andStr = " and ";
    }

    if (searchCriteria.getAdmissionDateFromIsNotNull()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" dem.admissionDateTime >= :dateFrom ");
      markers.add("dateFrom");
      values.add(searchCriteria.getAdmissionDateFrom().getJavaDate());
      andStr = " and ";
    }

    if (searchCriteria.getAdmissionDateToIsNotNull()) {
      hqlConditions.append(andStr);
      hqlConditions.append("dem.admissionDateTime <= :dateTo ");
      markers.add("dateTo");
      values.add(searchCriteria.getAdmissionDateTo().getJavaDate());
      andStr = " and ";
    }

    if (searchCriteria.getConsultantIsNotNull()) {
      hqlConditions.append(andStr);
      hqlConditions.append("cons.id = :consultantId");
      markers.add("consultantId");
      values.add(searchCriteria.getConsultant().getIMosHcpId());
      andStr = " and ";
    }

    if (searchCriteria.getSpecialtyIsNotNull()) {
      hqlConditions.append(andStr);
      hqlConditions.append("spec = :specialty ");
      markers.add("specialty");
      values.add(getDomLookup(searchCriteria.getSpecialty()));
      andStr = " and ";
    }

    if (searchCriteria.getDementiaStatusIsNotNull()) {
      hqlConditions.append(andStr);
      hqlConditions.append("demstatus.id = :Status ");
      markers.add("Status");
      values.add(getDomLookup(searchCriteria.getDementiaStatus()).getId());
      andStr = " and ";
    }

    if (searchCriteria.getSurnameIsNotNull()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" pat.name.upperSurname like :msurname ");
      markers.add("msurname");
      // WDEV-16806
      String formatedSurname = searchCriteria.getSurname().replaceAll("[^a-zA-Z]+", "");
      values.add(formatedSurname.toUpperCase() + "%");
      andStr = " and ";
    }

    String ret = "";
    if (searchCriteria != null && searchCriteria.getForenameIsNotNull())
      ret = searchCriteria.getForename().replaceAll("%", "");

    if (searchCriteria.getForenameIsNotNull() && ret.length() != 0) {
      hqlConditions.append(andStr);
      hqlConditions.append(" pat.name.upperForename like :mforename ");
      markers.add("mforename");
      // WDEV-16806
      String formatedForename = searchCriteria.getForename().replaceAll("[^a-zA-Z]+", "");
      values.add(formatedForename.toUpperCase() + "%");
      andStr = " and ";
    }

    if (searchCriteria.getPatIdTypeIsNotNull() && searchCriteria.getPatIdValueIsNotNull()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" patident.type = :idType and patident.value = :idValue ");
      markers.add("idType");
      values.add(getDomLookup(searchCriteria.getPatIdType()));
      markers.add("idValue");
      String idVal = searchCriteria.getPatIdValue().trim();
      if (searchCriteria.getPatIdType().equals(PatIdType.NHSN)) // wdev-16377
      values.add(searchCriteria.getPatIdValue().replace(" ", ""));
      else values.add(searchCriteria.getPatIdValue());
      andStr = " and ";
    }

    if (searchCriteria.getDOB() != null
        && !searchCriteria.getDOB().toString().equals("__/__/____")) {
      hqlConditions.append(andStr);
      hqlConditions.append(" pat.dob  between :dobFrom and :dobTo ");

      markers.add("dobFrom");
      markers.add("dobTo");
      Integer[] range = searchCriteria.getDOB().toSearchRange();
      values.add(range[0]);
      values.add(range[1]);
      andStr = " and ";
    }

    if (searchCriteria.getInpatientOnly()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" dem.dischargeDateTime is null");
      andStr = " and ";
    }

    if (searchCriteria.getDementiaConfirmed()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" dem.dementiaConfirmed = :demConfirmed");
      markers.add("demConfirmed");
      values.add(searchCriteria.getDementiaConfirmed());
      andStr = " and ";
    }

    if (!searchCriteria.getExcludeRemoved()
        && !searchCriteria.getDementiaConfirmed()
        && !searchCriteria.getDeliriumConfirmed()) {
      hqlConditions.append(andStr);
      hqlConditions.append(
          " (dem.isExcludedFromWorklist = 0 or dem.isExcludedFromWorklist is null)) ");
      andStr = " and ";
    }

    if (searchCriteria.getDeliriumConfirmed()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" dem.deliriumConfirmed = :delirumConfirmed");
      markers.add("delirumConfirmed");
      values.add(searchCriteria.getDeliriumConfirmed());
      andStr = " and ";
    }

    if (hqlConditions.length() > 0) {
      hqlConditions.insert(0, " where (");
      hqlConditions.append(" ) ");
    }

    DomainFactory factory = getDomainFactory();
    List<?> refList =
        factory.find(
            (hqlSB.toString() + hqlConditions.toString() + " ").toString(), markers, values, 2000);

    DementialManualClassAssemblyVo voItem = null;
    DementialManualClassAssemblyVoCollection voColl =
        new DementialManualClassAssemblyVoCollection();
    for (int i = 0; i < refList.size(); i++) {
      DementialManualClass doDem = (DementialManualClass) refList.get(i);
      voItem = new DementialManualClassAssemblyVo();

      voItem.setDementiaId(doDem.getDementiaId());
      voItem.setPatientSurname(doDem.getPatientSurnameName());
      voItem.setPatientForename(doDem.getPatientForename());
      voItem.setPatientIdentifier(doDem.getPatientIdentifier());
      voItem.setPatientDOB(
          doDem.getPatientDOB() != null
              ? new ims.framework.utils.PartialDate(doDem.getPatientDOB())
              : null);
      voItem.setWard(doDem.getWard());
      voItem.setConsultant(doDem.getConsultant());
      voItem.setConsultantSort(doDem.getConsultantSort());
      voItem.setCurrentStatus(
          doDem.getStatusID() != null
              ? new LookupInstVo(doDem.getStatusID(), doDem.getStatusText(), Boolean.TRUE)
              : null);
      voItem.setReasonForExclusion(doDem.getReasonForExclusion());
      voItem.setIsExcludedFromWorklist(doDem.getisExcludedFromWorklist());
      voItem.setDischargeDateTime(
          doDem
              .getDischargeDateTime()); // != null ? new
                                        // ims.framework.utils.DateTime(doDem.getDischargeDateTime().getDate(), doDem.ge) : null);
      voItem.setBreachDateTime(
          doDem
              .getBreechDateTime()); // != null ? new ims.framework.utils.DateTime
                                     //	(doDem.getBreachDateTime()) : null);
      voItem.setDeliriumConfirmed(doDem.getisDeliriumConfirmed());
      voItem.setDementiaConfirmed(doDem.getisDementiaConfirmed());

      voColl.add(voItem);
    }

    return voColl;
  }
  public DementiaForWorklistVoCollection listPatients(
      DementiaWorklistSearchCriteriaVo searchCriteria) {
    if (searchCriteria == null)
      throw new CodingRuntimeException("At least one search criteria must be provided");

    DomainFactory factory = getDomainFactory();

    StringBuffer hqlConditions = new StringBuffer();

    /*
    String hql = "select dementia from Dementia as dementia left join dementia.patient as patient left join patient.identifiers as identifiers " +
    			//"left join dementia.currentWorklistStatus as currentWorklistStatus left join currentWorklistStatus.status as status " +
    			"left join dementia.admissionDetail as admDet left join admDet.consultant as consultant left join admDet.specialty as specialty " +
    			"left join admDet.ward as ward ";
     */

    StringBuffer hql =
        new StringBuffer(
            "select dementia from Dementia as dementia left join dementia.patient as patient left join dementia.admissionDetail as admDet ");

    ArrayList<String> markers = new ArrayList<String>();
    ArrayList<Object> values = new ArrayList<Object>();

    String andStr = "";

    // work list should not list patient with DOD or Dementia is RIE
    hqlConditions.append(andStr);
    hqlConditions.append(" patient.dod is null and dementia.isRIE is null ");
    andStr = " and ";

    if (searchCriteria.getWardIsNotNull()) {
      hql.append(" left join admDet.ward as ward ");
      hqlConditions.append(andStr);
      hqlConditions.append(" ward.id=:ward ");
      markers.add("ward");
      values.add(searchCriteria.getWard().getID_Location());
      andStr = " and ";
    } else if (searchCriteria.getHospitalIsNotNull()) {
      hql.append(" left join admDet.ward as ward ");
      hqlConditions.append(andStr);
      hqlConditions.append(" ward.parentLocation.id = :hospital");
      markers.add("hospital");
      values.add(searchCriteria.getHospital().getID_Location());
      andStr = " and ";
    }

    if (searchCriteria.getAdmissionDateFromIsNotNull()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" dementia.admissionDateTime >= :dateFrom ");
      markers.add("dateFrom");
      values.add(searchCriteria.getAdmissionDateFrom().getJavaDate());
      andStr = " and ";
    }

    if (searchCriteria.getAdmissionDateToIsNotNull()) {
      hqlConditions.append(andStr);
      hqlConditions.append("dementia.admissionDateTime <= :dateTo ");
      markers.add("dateTo");
      values.add(searchCriteria.getAdmissionDateTo().getJavaDate());
      andStr = " and ";
    }

    if (searchCriteria.getConsultantIsNotNull()) {
      hql.append(" left join admDet.consultant as consultant ");
      hqlConditions.append(andStr);
      hqlConditions.append("consultant.id = :consultantId");
      markers.add("consultantId");
      values.add(searchCriteria.getConsultant().getIMosHcpId());
      andStr = " and ";
    }

    if (searchCriteria.getSpecialtyIsNotNull()) {
      hql.append(" left join admDet.specialty as specialty ");
      hqlConditions.append(andStr);
      hqlConditions.append("specialty = :specialty ");
      markers.add("specialty");
      values.add(getDomLookup(searchCriteria.getSpecialty()));
      andStr = " and ";
    }

    if (searchCriteria.getDementiaStatusIsNotNull()) {
      hql.append(
          " left join dementia.currentWorklistStatus as currentWorklistStatus left join currentWorklistStatus.status as status ");
      hqlConditions.append(andStr);
      hqlConditions.append("status = :Status ");
      markers.add("Status");
      values.add(getDomLookup(searchCriteria.getDementiaStatus()));
      andStr = " and ";
    }

    if (searchCriteria.getSurnameIsNotNull()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" patient.name.upperSurname like :msurname ");
      markers.add("msurname");
      // WDEV-16806
      String formatedSurname = searchCriteria.getSurname().replaceAll("[^a-zA-Z]+", "");
      values.add(formatedSurname.toUpperCase() + "%");
      andStr = " and ";
    }

    String ret = "";
    if (searchCriteria != null && searchCriteria.getForenameIsNotNull())
      ret = searchCriteria.getForename().replaceAll("%", "");

    if (searchCriteria.getForenameIsNotNull() && ret.length() != 0) {
      hqlConditions.append(andStr);
      hqlConditions.append(" patient.name.upperForename like :mforename ");
      markers.add("mforename");
      // WDEV-16806
      String formatedForename = searchCriteria.getForename().replaceAll("[^a-zA-Z]+", "");
      values.add(formatedForename.toUpperCase() + "%");
      andStr = " and ";
    }

    if (searchCriteria.getPatIdTypeIsNotNull() && searchCriteria.getPatIdValueIsNotNull()) {
      hql.append(" left join patient.identifiers as identifiers ");
      hqlConditions.append(andStr);
      hqlConditions.append(" identifiers.type = :idType and identifiers.value = :idValue ");
      markers.add("idType");
      values.add(getDomLookup(searchCriteria.getPatIdType()));
      markers.add("idValue");
      values.add(searchCriteria.getPatIdValue());
      andStr = " and ";
    }

    if (searchCriteria.getDOB() != null
        && !searchCriteria.getDOB().toString().equals("__/__/____")) {
      hqlConditions.append(andStr);
      hqlConditions.append(" patient.dob  between :dobFrom and :dobTo ");

      markers.add("dobFrom");
      markers.add("dobTo");
      Integer[] range = searchCriteria.getDOB().toSearchRange();
      values.add(range[0]);
      values.add(range[1]);
      andStr = " and ";
    }

    if (searchCriteria.getInpatientOnly()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" dementia.dischargeDateTime is null");
      andStr = " and ";
    }

    if (searchCriteria.getDementiaConfirmed()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" dementia.dementiaConfirmed = :demConfirmed");
      markers.add("demConfirmed");
      values.add(searchCriteria.getDementiaConfirmed());
      andStr = " and ";
    }

    if (searchCriteria.getDeliriumConfirmed()) {
      hqlConditions.append(andStr);
      hqlConditions.append(" dementia.deliriumConfirmed = :delirumConfirmed");
      markers.add("delirumConfirmed");
      values.add(searchCriteria.getDeliriumConfirmed());
      andStr = " and ";
    }

    if (!searchCriteria.getExcludeRemoved()) {
      hqlConditions.append(andStr);
      hqlConditions.append(
          " (dementia.isExcludedFromWorklist = 0 or dementia.isExcludedFromWorklist is null)) ");
      andStr = " and ";
    }

    if (hqlConditions.length() > 0) {
      hqlConditions.insert(0, " where (");
      hqlConditions.append(" ) ");
    }

    List<?> list =
        factory.find(
            (hql.toString() + hqlConditions.toString() + " ").toString(), markers, values, 2000);

    if (list == null || list.size() == 0) return null;

    return DementiaForWorklistVoAssembler.createDementiaForWorklistVoCollectionFromDementia(list);
  }