private ResultLimit defaultResultLimit(List<ResultLimit> resultLimits) {

    for (ResultLimit limit : resultLimits) {
      if (GenericValidator.isBlankOrNull(limit.getGender()) && limit.ageLimitsAreDefault()) {
        return limit;
      }
    }
    return new ResultLimit();
  }
  private ResultLimit ageBasedResultLimit(List<ResultLimit> resultLimits, Patient patient) {

    ResultLimit resultLimit = null;

    // First we look for a limit with no gender
    for (ResultLimit limit : resultLimits) {
      if (GenericValidator.isBlankOrNull(limit.getGender())
          && !limit.ageLimitsAreDefault()
          && getCurrPatientAge(patient) >= limit.getMinAge()
          && getCurrPatientAge(patient) <= limit.getMaxAge()) {

        resultLimit = limit;
        break;
      }
    }

    // if none is found then drop the no gender requirement
    if (resultLimit == null) {
      for (ResultLimit limit : resultLimits) {
        if (!limit.ageLimitsAreDefault()
            && getCurrPatientAge(patient) >= limit.getMinAge()
            && getCurrPatientAge(patient) <= limit.getMaxAge()) {

          resultLimit = limit;
          break;
        }
      }
    }

    return resultLimit == null ? defaultResultLimit(resultLimits) : resultLimit;
  }
  /*
   * We only get here if patient has age and gender
   */
  private ResultLimit ageAndGenderBasedResultLimit(
      List<ResultLimit> resultLimits, Patient patient) {

    ResultLimit resultLimit = null;

    List<ResultLimit> fullySpecifiedLimits = new ArrayList<ResultLimit>();
    // first age and gender matter
    for (ResultLimit limit : resultLimits) {
      if (patient.getGender().equals(limit.getGender()) && !limit.ageLimitsAreDefault()) {

        // if fully qualified don't retest for only part of the
        // qualification
        fullySpecifiedLimits.add(limit);

        if (patientInAgeRange(patient, limit)) {

          resultLimit = limit;
          break;
        }
      }
    }

    resultLimits.removeAll(fullySpecifiedLimits);

    // second only age matters
    if (resultLimit == null) {
      for (ResultLimit limit : resultLimits) {
        if (!limit.ageLimitsAreDefault() && patientInAgeRange(patient, limit)) {

          resultLimit = limit;
          break;
        }
      }
    }

    // third only gender matters
    return resultLimit == null ? genderBasedResultLimit(resultLimits, patient) : resultLimit;
  }
  private ResultLimit genderBasedResultLimit(List<ResultLimit> resultLimits, Patient patient) {

    ResultLimit resultLimit = null;

    // First we look for a limit with no age
    for (ResultLimit limit : resultLimits) {
      if (limit.ageLimitsAreDefault() && patient.getGender().equals(limit.getGender())) {

        resultLimit = limit;
        break;
      }
    }

    // drop the age limit
    if (resultLimit == null) {
      for (ResultLimit limit : resultLimits) {
        if (patient.getGender().equals(limit.getGender())) {
          resultLimit = limit;
          break;
        }
      }
    }
    return resultLimit == null ? defaultResultLimit(resultLimits) : resultLimit;
  }
 public static String getDisplayReferenceRange(
     ResultLimit resultLimit, String significantDigits, String separator) {
   String range = "";
   if (resultLimit != null && !GenericValidator.isBlankOrNull(resultLimit.getResultTypeId())) {
     if (NUMERIC_RESULT_TYPE_ID.equals(resultLimit.getResultTypeId())) {
       range =
           getDisplayNormalRange(
               resultLimit.getLowNormal(),
               resultLimit.getHighNormal(),
               significantDigits,
               separator);
     } else if (SELECT_LIST_RESULT_TYPE_IDS.contains(resultLimit.getResultTypeId())
         && !GenericValidator.isBlankOrNull(resultLimit.getDictionaryNormalId())) {
       return dictionaryDAO.getDataForId(resultLimit.getDictionaryNormalId()).getLocalizedName();
     }
   }
   return range;
 }
 private boolean patientInAgeRange(Patient patient, ResultLimit limit) {
   return getCurrPatientAge(patient) >= limit.getMinAge()
       && getCurrPatientAge(patient) <= limit.getMaxAge();
 }