private void createUpdateList(List<AnalysisItem> analysisItems, boolean areListeners) {

    List<String> analysisIdList = new ArrayList<String>();

    for (AnalysisItem analysisItem : analysisItems) {
      if (!analysisItem.isReadOnly() && analysisItemWillBeUpdated(analysisItem)) {

        AnalysisService analysisService = new AnalysisService(analysisItem.getAnalysisId());
        Analysis analysis = analysisService.getAnalysis();
        NoteService noteService = new NoteService(analysis);

        analysis.setSysUserId(currentUserId);

        if (!analysisIdList.contains(analysis.getId())) {

          if (analysisItem.getIsAccepted()) {
            analysis.setStatusId(StatusService.getInstance().getStatusID(AnalysisStatus.Finalized));
            analysis.setReleasedDate(new java.sql.Date(Calendar.getInstance().getTimeInMillis()));
            analysisIdList.add(analysis.getId());
            analysisUpdateList.add(analysis);
          }

          if (analysisItem.getIsRejected()) {
            analysis.setStatusId(
                StatusService.getInstance().getStatusID(AnalysisStatus.BiologistRejected));
            analysisIdList.add(analysis.getId());
            analysisUpdateList.add(analysis);
          }
        }

        createNeededNotes(analysisItem, noteService);

        if (areResults(analysisItem)) {
          List<Result> results =
              createResultFromAnalysisItem(analysisItem, analysisService, noteService);
          for (Result result : results) {
            resultUpdateList.add(result);

            if (areListeners) {
              addResultSets(analysis, result);
            }
          }
        }
      }
    }
  }
  private List<Result> createResultFromAnalysisItem(
      AnalysisItem analysisItem, AnalysisService analysisService, NoteService noteService) {

    ResultSaveBean bean = ResultSaveBeanAdapter.fromAnalysisItem(analysisItem);
    ResultSaveService resultSaveService =
        new ResultSaveService(analysisService.getAnalysis(), currentUserId);
    List<Result> results = resultSaveService.createResultsFromTestResultItem(bean, deletableList);
    if (analysisService.patientReportHasBeenDone() && resultSaveService.isUpdatedResult()) {
      analysisService.getAnalysis().setCorrectedSincePatientReport(true);
      noteUpdateList.add(
          noteService.createSavableNote(
              NoteType.EXTERNAL,
              StringUtil.getMessageForKey("note.corrected.result"),
              RESULT_SUBJECT,
              currentUserId));
    }
    return results;
  }
  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;
  }