public InvestigationAttendenceDetailVo inactivateInvestigationAttendenceDetail(
      InvestigationAttendenceDetailVo record, AttendDiagInvTreatStatusVo attendDiagInvTreatStatus)
      throws StaleObjectException {
    if (record == null)
      throw new CodingRuntimeException("Cannot save a null InvestigationAttendenceDetailVo.");

    if (!record.isValidated())
      throw new CodingRuntimeException("InvestigationAttendenceDetailVo is not validated.");

    DomainFactory factory = getDomainFactory();

    InvestigationAttendenceDetail doRecord =
        InvestigationAttendenceDetailVoAssembler.extractInvestigationAttendenceDetail(
            factory, record);
    factory.save(doRecord);

    if (attendDiagInvTreatStatus != null) {
      AttendDiagInvTreatStatus doAttendDiagInvTreat =
          AttendDiagInvTreatStatusVoAssembler.extractAttendDiagInvTreatStatus(
              factory, attendDiagInvTreatStatus);
      factory.save(doAttendDiagInvTreat);
    }

    return record;
  }
  public AttendDiagInvTreatStatusVo saveAttendDiagInvTreatStatus(
      AttendDiagInvTreatStatusVo attendDiagInvTreatStatus)
      throws StaleObjectException, UniqueKeyViolationException {
    if (attendDiagInvTreatStatus == null)
      throw new CodingRuntimeException("Cannot save a null AttendDiagInvTreatStatusVo.");

    if (!attendDiagInvTreatStatus.isValidated())
      throw new CodingRuntimeException("AttendDiagInvTreatStatusVo is not validated.");

    DomainFactory factory = getDomainFactory();
    AttendDiagInvTreatStatus doAttendDiagInvTreatStatus =
        AttendDiagInvTreatStatusVoAssembler.extractAttendDiagInvTreatStatus(
            factory, attendDiagInvTreatStatus);

    factory.save(doAttendDiagInvTreatStatus);

    return AttendDiagInvTreatStatusVoAssembler.create(doAttendDiagInvTreatStatus);
  }
  public AttendDiagInvTreatStatusVo getAttendDiagInvTreatStatus(CareContextRefVo careContextRef) {
    if (careContextRef == null || careContextRef.getID_CareContext() == null) return null;

    DomainFactory factory = getDomainFactory();

    String queryNoKnownInvestigationSaved =
        "SELECT attendDiagInvTreatStatus  from AttendDiagInvTreatStatus as attendDiagInvTreatStatus left join attendDiagInvTreatStatus.attendance as at where (at.id = :AttendenceId and (attendDiagInvTreatStatus.isRIE is null OR attendDiagInvTreatStatus.isRIE = 0) ) ";
    AttendDiagInvTreatStatus doAttendDiagInvTreatStatus =
        (AttendDiagInvTreatStatus)
            factory.findFirst(
                queryNoKnownInvestigationSaved,
                new String[] {"AttendenceId"},
                new Object[] {careContextRef.getID_CareContext()});

    return AttendDiagInvTreatStatusVoAssembler.create(doAttendDiagInvTreatStatus);
  }
  public InterventionsTreatmentsForAttendenceVo save(
      InterventionsTreatmentsForAttendenceVo record, Boolean addedDuringCoding)
      throws StaleObjectException {
    if (record == null)
      throw new CodingRuntimeException(
          "Cannot save a null InterventionsTreatmentsForAttendenceVo.");

    if (!record.isValidated())
      throw new CodingRuntimeException("InterventionsTreatmentsForAttendenceVo is not validated.");

    DomainFactory factory = getDomainFactory();

    InterventionsTreatmentsForAttendence doRecord =
        InterventionsTreatmentsForAttendenceVoAssembler.extractInterventionsTreatmentsForAttendence(
            factory, record);

    if (record.getID_InterventionsTreatmentsForAttendence() == null
        && record.getAttendance() != null) {
      InterventionsTreatmentsForAttendenceVo staleRecord =
          getInterventionsTreatmentsForAttendence(record.getAttendance());
      if (staleRecord != null) throw new StaleObjectException(doRecord);
    }

    factory.save(doRecord);

    if (!Boolean.TRUE.equals(addedDuringCoding)) {
      AttendDiagInvTreatStatusVo attendDiagInvTreatStatus =
          getAttendDiagInvTreatStatus(record.getAttendance());
      if (attendDiagInvTreatStatus != null
          && (Boolean.TRUE.equals(attendDiagInvTreatStatus.getNoTreatments())
              || Boolean.TRUE.equals(attendDiagInvTreatStatus.getTreatmentsNotRecorded()))) {
        attendDiagInvTreatStatus.setNoTreatments(null);
        attendDiagInvTreatStatus.setTreatmentsNotRecorded(null);

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

    return InterventionsTreatmentsForAttendenceVoAssembler.create(doRecord);
  }
  public InterventionsTreatmentsForAttendenceVo save(
      InterventionsTreatmentsForAttendenceVo record,
      AttendDiagInvTreatStatusVo attendDiagInvTreatStatus,
      Boolean addedDuringCoding)
      throws StaleObjectException {
    InterventionsTreatments impl =
        (InterventionsTreatments) getDomainImpl(InterventionsTreatmentsImpl.class);
    InterventionsTreatmentsForAttendenceVo newRecord = impl.save(record, addedDuringCoding);

    if (attendDiagInvTreatStatus != null && !Boolean.TRUE.equals(addedDuringCoding)) {
      DomainFactory factory = getDomainFactory();

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

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