protected RejectionReportBean createRejectionReportBean(
      String noteText, Analysis analysis, boolean useTestName) {
    RejectionReportBean item = new RejectionReportBean();

    AnalysisService analysisService = new AnalysisService(analysis);
    SampleService sampleService =
        new SampleService(analysisService.getAnalysis().getSampleItem().getSample());
    PatientService patientService = new PatientService(sampleService.getSample());

    List<Result> results = analysisService.getResults();
    for (Result result : results) {
      String signature = new ResultService(result).getSignature();
      if (!GenericValidator.isBlankOrNull(signature)) {
        item.setTechnician(signature);
        break;
      }
    }

    item.setAccessionNumber(sampleService.getAccessionNumber().substring(PREFIX_LENGTH));
    item.setReceivedDate(sampleService.getTwoYearReceivedDateForDisplay());
    item.setCollectionDate(
        DateUtil.convertTimestampToTwoYearStringDate(
            analysisService.getAnalysis().getSampleItem().getCollectionDate()));
    item.setRejectionReason(noteText);

    StringBuilder nameBuilder = new StringBuilder(patientService.getLastName().toUpperCase());
    if (!GenericValidator.isBlankOrNull(patientService.getNationalId())) {
      if (nameBuilder.length() > 0) {
        nameBuilder.append(" / ");
      }
      nameBuilder.append(patientService.getNationalId());
    }

    if (useTestName) {
      item.setPatientOrTestName(analysisService.getTest().getLocalizedName());
      item.setNonPrintingPatient(nameBuilder.toString());
    } else {
      item.setPatientOrTestName(nameBuilder.toString());
    }

    return item;
  }
  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;
  }