private boolean areResults(AnalysisItem item) {
   return !(isBlankOrNull(item.getResult())
           || (ResultType.DICTIONARY.matches(item.getResultType())
               && "0".equals(item.getResult())))
       || (ResultType.isMultiSelectVariant(item.getResultType())
           && !isBlankOrNull(item.getMultiSelectResultValues()));
 }
  private void fillInDictionaryValuesForReferralItems(List<ReferralItem> referralItems) {
    for (ReferralItem referralItem : referralItems) {
      String referredResultType = referralItem.getReferredResultType();
      if (ResultType.isDictionaryVariant(referredResultType)) {
        referralItem.setDictionaryResults(
            getDictionaryValuesForTest(referralItem.getReferredTestId()));
      }

      if (referralItem.getAdditionalTests() != null) {
        for (ReferredTest test : referralItem.getAdditionalTests()) {
          if (ResultType.isDictionaryVariant(test.getReferredResultType())) {
            test.setDictionaryResults(getDictionaryValuesForTest(test.getReferredTestId()));
          }
        }
      }
    }
  }
  private List<NonNumericTests> getNonNumericTests(List<ReferralItem> referralItems) {
    DictionaryDAO dictionaryDAO = new DictionaryDAOImpl();
    Set<String> testIdSet = new HashSet<String>();

    for (ReferralItem item : referralItems) {
      for (IdValuePair pair : item.getTestSelectionList()) {
        testIdSet.add(pair.getId());
      }
    }

    List<NonNumericTests> nonNumericTestList = new ArrayList<NonNumericTests>();
    TestResultDAO testResultDAO = new TestResultDAOImpl();
    for (String testId : testIdSet) {
      List<TestResult> testResultList = testResultDAO.getActiveTestResultsByTest(testId);

      if (!(testResultList == null || testResultList.isEmpty())) {
        NonNumericTests nonNumericTests = new NonNumericTests();

        nonNumericTests.testId = testId;
        nonNumericTests.testType = testResultList.get(0).getTestResultType();
        boolean isSelectList = ResultType.isDictionaryVariant(nonNumericTests.testType);

        if (isSelectList) {
          List<IdValuePair> dictionaryValues = new ArrayList<IdValuePair>();
          for (TestResult testResult : testResultList) {
            if (ResultType.isDictionaryVariant(testResult.getTestResultType())) {
              String resultName =
                  dictionaryDAO.getDictionaryById(testResult.getValue()).getLocalizedName();
              dictionaryValues.add(new IdValuePair(testResult.getValue(), resultName));
            }
          }

          nonNumericTests.dictionaryValues = dictionaryValues;
        }

        if (nonNumericTests.testType != null) {
          nonNumericTestList.add(nonNumericTests);
        }
      }
    }

    return nonNumericTestList;
  }
  /**
   * Move everything appropriate to the referralItem including one or more of the referralResults
   * from the given list. Note: This method removes an item from the referralResults list.
   *
   * @param referralItem The source item
   * @param referralResults The created list
   */
  private List<ReferralResult> setReferralItemForNextTest(
      IReferralResultTest referralItem, List<ReferralResult> referralResults) {

    ReferralResult nextTestFirstResult = referralResults.remove(0);
    List<ReferralResult> resultsForOtherTests = new ArrayList<ReferralResult>(referralResults);

    referralItem.setReferredTestId(nextTestFirstResult.getTestId());
    referralItem.setReferredTestIdShadow(referralItem.getReferredTestId());
    referralItem.setReferredReportDate(
        DateUtil.convertTimestampToStringDate(nextTestFirstResult.getReferralReportDate()));
    // We can not use ResultService because that assumes the result is for an analysis, not a
    // referral
    Result result = nextTestFirstResult.getResult();

    String resultType = (result != null) ? result.getResultType() : "N";
    referralItem.setReferredResultType(resultType);
    if (!ResultType.isMultiSelectVariant(resultType)) {
      if (result != null) {
        String resultValue =
            GenericValidator.isBlankOrNull(result.getValue()) ? "" : result.getValue();
        referralItem.setReferredResult(resultValue);
        referralItem.setReferredDictionaryResult(resultValue);
      }
    } else {
      ArrayList<Result> resultList = new ArrayList<Result>();
      resultList.add(nextTestFirstResult.getResult());

      for (ReferralResult referralResult : referralResults) {
        if (nextTestFirstResult.getTestId().equals(referralResult.getTestId())
            && !GenericValidator.isBlankOrNull(referralResult.getResult().getValue())) {
          resultList.add(referralResult.getResult());
          resultsForOtherTests.remove(referralResult);
        }
      }

      referralItem.setMultiSelectResultValues(
          ResultService.getJSONStringForMultiSelect(resultList));
    }

    return resultsForOtherTests;
  }
  private String getAppropriateResultValue(List<Result> results) {
    Result result = results.get(0);
    if (ResultType.DICTIONARY.matches(result.getResultType())) {
      Dictionary dictionary = dictionaryDAO.getDictionaryById(result.getValue());
      if (dictionary != null) {
        return dictionary.getLocalizedName();
      }
    } else if (ResultType.isMultiSelectVariant(result.getResultType())) {
      Dictionary dictionary = new Dictionary();
      StringBuilder multiResult = new StringBuilder();

      for (Result subResult : results) {
        dictionary.setId(subResult.getValue());
        dictionaryDAO.getData(dictionary);

        if (dictionary.getId() != null) {
          multiResult.append(dictionary.getLocalizedName());
          multiResult.append(", ");
        }
      }

      if (multiResult.length() > 0) {
        multiResult.setLength(multiResult.length() - 2); // remove last ", "
      }

      return multiResult.toString();
    } else {
      String resultValue =
          GenericValidator.isBlankOrNull(result.getValue()) ? "" : result.getValue();

      if (!GenericValidator.isBlankOrNull(resultValue)
          && result.getAnalysis().getTest().getUnitOfMeasure() != null) {
        resultValue += " " + result.getAnalysis().getTest().getUnitOfMeasure().getName();
      }

      return resultValue;
    }

    return "";
  }