public PatientAssessmentVo save(
      PatientAssessmentVo patientAssessment, ConsultationAssessmentsLiteVo consultationAssessment)
      throws StaleObjectException {
    // Check parameters
    if (patientAssessment == null || consultationAssessment == null)
      throw new CodingRuntimeException("Can't save null records.");

    if (patientAssessment.isValidated() == false || consultationAssessment.isValidated() == false)
      throw new CodingRuntimeException("Can't save records not validated.");

    DomainFactory factory = getDomainFactory();

    // Ensure the latest Consultation Assessments record is used (to avoid StaleObject)
    if (consultationAssessment.getID_ConsultationAssessments() != null) {
      // If the Consultation Assessment was saved then retrieve the latest version of it
      consultationAssessment =
          ConsultationAssessmentsLiteVoAssembler.create(
              (ConsultationAssessments)
                  factory.getDomainObject(
                      ConsultationAssessments.class,
                      consultationAssessment.getID_ConsultationAssessments()));
    } else {
      // IF the Consultation Assessment isn't saved then check database for a saved record
      ConsultationAssessmentsLiteVo recordInDB =
          getConsultationAssessments(consultationAssessment.getCatsReferral());

      if (recordInDB != null) consultationAssessment = recordInDB;
    }

    // Extract domain objects
    PatientAssessment domPatientAssessment =
        PatientAssessmentVoAssembler.extractPatientAssessment(factory, patientAssessment);

    // Commit PatientAssessment to database
    factory.save(domPatientAssessment);

    PatientAssessmentShortVo shortAssessment =
        PatientAssessmentShortVoAssembler.create(domPatientAssessment);
    consultationAssessment.getPatientAssessment().add(shortAssessment);

    ConsultationAssessments domConsultationAssessments =
        ConsultationAssessmentsLiteVoAssembler.extractConsultationAssessments(
            factory, consultationAssessment);

    factory.save(domConsultationAssessments);

    // Return the saved record
    return PatientAssessmentVoAssembler.create(domPatientAssessment);
  }
  public ConsultationAssessmentsLiteVo getConsultationAssessments(CatsReferralRefVo referral) {
    if (referral == null || referral.getID_CatsReferral() == null) return null;

    String query =
        "SELECT ca FROM ConsultationAssessments AS ca WHERE ca.catsReferral.id = :REF_ID AND ca.isRIE is null";

    ArrayList<String> paramNames = new ArrayList<String>();
    ArrayList<Integer> paramValues = new ArrayList<Integer>();

    paramNames.add("REF_ID");
    paramValues.add(referral.getID_CatsReferral());

    return ConsultationAssessmentsLiteVoAssembler.create(
        (ConsultationAssessments) getDomainFactory().findFirst(query, paramNames, paramValues));
  }