private String getSendDateOrDefault(Referral referral) {
   if (referral.getSentDate() == null) {
     return DateUtil.getCurrentDateAsText();
   } else {
     return DateUtil.convertTimestampToStringDate(referral.getSentDate());
   }
 }
  private List<ReferredTest> getAdditionalReferralTests(List<ReferralResult> referralResults) {
    List<ReferredTest> additionalTestList = new ArrayList<ReferredTest>();

    while (!referralResults.isEmpty()) {
      ReferralResult referralResult =
          referralResults.get(0); // use the top one to load various bits of information.
      ReferredTest referralTest = new ReferredTest();
      referralTest.setReferralId(referralResult.getReferralId());
      referralResults =
          setReferralItemForNextTest(
              referralTest,
              referralResults); // remove one or more referralResults from the list as needed (for
                                // multiResults).
      referralTest.setReferredReportDate(
          DateUtil.convertTimestampToStringDate(referralResult.getReferralReportDate()));
      referralTest.setReferralResultId(referralResult.getId());
      additionalTestList.add(referralTest);
    }
    return additionalTestList;
  }
  /**
   * 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 ReferralItem getReferralItem(Referral referral) {
    boolean allReferralResultsHaveResults = true;
    List<ReferralResult> referralResults =
        referralResultDAO.getReferralResultsForReferral(referral.getId());
    for (ReferralResult referralResult : referralResults) {
      if (referralResult.getResult() == null
          || GenericValidator.isBlankOrNull(referralResult.getResult().getValue())) {
        allReferralResultsHaveResults = false;
        break;
      }
    }

    if (allReferralResultsHaveResults) {
      return null;
    }

    ReferralItem referralItem = new ReferralItem();

    AnalysisService analysisService = new AnalysisService(referral.getAnalysis());

    referralItem.setCanceled(false);
    referralItem.setReferredResultType("N");
    referralItem.setAccessionNumber(analysisService.getOrderAccessionNumber());

    TypeOfSample typeOfSample = analysisService.getTypeOfSample();
    referralItem.setSampleType(typeOfSample.getLocalizedName());

    referralItem.setReferringTestName(
        TestService.getUserLocalizedTestName(analysisService.getAnalysis().getTest()));
    List<Result> resultList = analysisService.getResults();
    String resultString = "";

    if (!resultList.isEmpty()) {
      Result result = resultList.get(0);
      resultString = getAppropriateResultValue(resultList);
      referralItem.setInLabResultId(result.getId());
    }

    referralItem.setReferralId(referral.getId());
    if (!referralResults.isEmpty()) {
      referralResults = setReferralItemForNextTest(referralItem, referralResults);
      if (!referralResults.isEmpty()) {
        referralItem.setAdditionalTests(getAdditionalReferralTests(referralResults));
      }
    }
    referralItem.setReferralResults(resultString);
    referralItem.setReferralDate(DateUtil.convertTimestampToStringDate(referral.getRequestDate()));
    referralItem.setReferredSendDate(getSendDateOrDefault(referral));
    referralItem.setReferrer(referral.getRequesterName());
    referralItem.setReferralReasonId(referral.getReferralReasonId());
    referralItem.setTestSelectionList(getTestsForTypeOfSample(typeOfSample));
    referralItem.setReferralId(referral.getId());
    if (referral.getOrganization() != null) {
      referralItem.setReferredInstituteId(referral.getOrganization().getId());
    }
    String notes = analysisService.getNotesAsString(true, true, "<br/>", false);
    if (notes != null) {
      referralItem.setPastNotes(notes);
    }

    return referralItem;
  }