public Boolean isStaleOnDiagnosis(ValueObject diagnosis) // WDEV-19239
      {
    if (diagnosis == null) return false;

    DomainFactory factory = getDomainFactory();

    List<?> appts = null;

    if (diagnosis instanceof PatientDiagnosisAtConsultationVo) {
      PatientDiagnosisAtConsultationVo patDiag = (PatientDiagnosisAtConsultationVo) diagnosis;
      appts =
          factory.find(
              "select a.id from PatientDiagnosis as a where a.id = :DiagnosisId and a.version > :DiagnosisVersion",
              new String[] {"DiagnosisId", "DiagnosisVersion"},
              new Object[] {
                patDiag.getID_PatientDiagnosis(), patDiag.getVersion_PatientDiagnosis()
              });
    } else if (diagnosis instanceof CodedDiagnosesForAttendanceVo) {
      CodedDiagnosesForAttendanceVo codedDiag = (CodedDiagnosesForAttendanceVo) diagnosis;
      appts =
          factory.find(
              "select a.id from CodedDiagForAttendance as a where a.id = :DiagnosisId and a.version > :DiagnosisVersion",
              new String[] {"DiagnosisId", "DiagnosisVersion"},
              new Object[] {
                codedDiag.getID_CodedDiagForAttendance(),
                codedDiag.getVersion_CodedDiagForAttendance()
              });
    }

    if (appts != null && appts.size() > 0) return true;

    return false;
  }
  public PatientDiagnosisAtConsultationVoCollection saveDiagnosis(
      PatientDiagnosisAtConsultationVoCollection diagnosis, CareContextRefVo careContext)
      throws StaleObjectException {
    if (diagnosis == null)
      throw new CodingRuntimeException(
          "Cannot save a null PatientDiagnosisAtConsultationVoCollection.");

    if (!diagnosis.isValidated())
      throw new CodingRuntimeException(
          "PatientDiagnosisAtConsultationVoCollection is not validated.");

    DomainFactory factory = getDomainFactory();
    PatientDiagnosisAtConsultationVoCollection coll =
        new PatientDiagnosisAtConsultationVoCollection();

    // WDEV-19239
    boolean isPatientDiagPrimary = false;
    CareContextRefVo careContextRef = null;
    for (PatientDiagnosisAtConsultationVo diagnose : diagnosis) {
      if (diagnose == null) continue;

      careContextRef = diagnose.getCareContext();
      if (isPrimary(diagnose)) isPatientDiagPrimary = true;

      PatientDiagnosis doDiagnose =
          PatientDiagnosisAtConsultationVoAssembler.extractPatientDiagnosis(factory, diagnose);

      factory.save(doDiagnose);
      coll.add(PatientDiagnosisAtConsultationVoAssembler.create(doDiagnose));
    }

    CodedDiagnosesForAttendanceVoCollection collCodedDiag =
        listCodedDiagnosesForAttendance(careContextRef, true);

    if (isPatientDiagPrimary && collCodedDiag != null && collCodedDiag.size() > 0) {
      for (CodedDiagnosesForAttendanceVo codedDiag : collCodedDiag) {
        codedDiag.setIsMain(false);

        CodedDiagForAttendance doCodedDiag =
            CodedDiagnosesForAttendanceVoAssembler.extractCodedDiagForAttendance(
                factory, codedDiag);
        factory.save(doCodedDiag);
      }
    }

    if (careContext != null) {
      AttendDiagInvTreatStatusVo attendDiagInvTreatStatus =
          getAttendDiagInvTreatStatus(careContext);

      if (attendDiagInvTreatStatus != null) {
        attendDiagInvTreatStatus.setNoDiagnoses(null);
        attendDiagInvTreatStatus.setDiagnosesNotRecorded(null);

        AttendDiagInvTreatStatus doAttendDiagInvTreatStatus =
            AttendDiagInvTreatStatusVoAssembler.extractAttendDiagInvTreatStatus(
                factory, attendDiagInvTreatStatus);
        factory.save(doAttendDiagInvTreatStatus);
      }
    }

    return coll;
  }