public DementiaForWorklistVo getDementiaForWorklist(DementiaRefVo dementiaRef) {
    if (dementiaRef == null || dementiaRef.getID_Dementia() == null) {
      throw new CodingRuntimeException("Cannot get DementiaVo on null Id ");
    }

    DomainFactory factory = getDomainFactory();

    Dementia domainDementia =
        (Dementia) factory.getDomainObject(Dementia.class, dementiaRef.getID_Dementia());

    return DementiaForWorklistVoAssembler.create(domainDementia);
  }
  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);
  }