/** Saves the default schedule for current date to a provided client ClientImmunisationSchedule */
  public ClientImmunisationScheduleVo saveDefaultScheduleToClient(
      PatientRefVo clientRef, MemberOfStaffLiteVo mos)
      throws DomainInterfaceException, StaleObjectException, ForeignKeyViolationException,
          UniqueKeyViolationException {
    if (clientRef == null)
      throw new DomainInterfaceException(
          "A client must be selected before a schedule can be added");

    // Look for a client in the database
    DomainFactory factory = getDomainFactory();
    ClientLiteVo clientLiteVo =
        ClientLiteVoAssembler.create(
            (Patient) factory.getDomainObject(Patient.class, clientRef.getID_Patient()));

    if (clientLiteVo == null) throw new DomainInterfaceException("Desired client does not exist");

    Date dateOfBirth = new Date();
    dateOfBirth.setDay(
        clientLiteVo.getDob().getDay() == null ? new Integer(1) : clientLiteVo.getDob().getDay());
    dateOfBirth.setMonth(
        clientLiteVo.getDob().getMonth() == null
            ? new Integer(1)
            : clientLiteVo.getDob().getMonth());
    dateOfBirth.setYear(
        clientLiteVo.getDob().getYear() == null
            ? new Integer(new Date().getYear())
            : clientLiteVo.getDob().getYear());

    ScheduleConfigurationLiteVo defaultScheduleConfiguration =
        getDefaultScheduleConfiguration(dateOfBirth);

    if (defaultScheduleConfiguration == null)
      throw new DomainInterfaceException(
          "There is no default schedule for the current date. Please define a default schedule for this date or select a schedule");

    ClientImmunisationScheduleVo clientSchedule =
        addScheduleToClient(clientRef, defaultScheduleConfiguration, mos);

    String[] errors = clientSchedule.validate();

    if (errors != null && errors.length != 0) {
      throw new DomainInterfaceException(
          "Can not add a default schedule to client. Errors present");
    }

    return saveClientImmunisationSchedule(clientSchedule);
  }
  private ClientImmunisationScheduleVo addScheduleToClient(
      PatientRefVo clientRef, ScheduleConfigurationRefVo scheduleRef, MemberOfStaffLiteVo mos)
      throws DomainInterfaceException {
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //	PRELIMINARY CHECKS
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // Check for a client
    if (clientRef == null)
      throw new DomainInterfaceException("Can not add schedule. Client was not found in records");

    // Look for a client in the database
    DomainFactory factory = getDomainFactory();
    ClientLiteVo clientLiteVo =
        ClientLiteVoAssembler.create(
            (Patient) factory.getDomainObject(Patient.class, clientRef.getID_Patient()));

    if (clientLiteVo == null) throw new DomainInterfaceException("Desired client does not exist");

    Date dateOfBirth = new Date();
    dateOfBirth.setDay(
        clientLiteVo.getDob().getDay() == null ? new Integer(1) : clientLiteVo.getDob().getDay());
    dateOfBirth.setMonth(
        clientLiteVo.getDob().getMonth() == null
            ? new Integer(1)
            : clientLiteVo.getDob().getMonth());
    dateOfBirth.setYear(
        clientLiteVo.getDob().getYear() == null
            ? new Integer(new Date().getYear())
            : clientLiteVo.getDob().getYear());

    // Check for a member of staff
    if (mos == null)
      throw new DomainInterfaceException(
          "An immunisation schedule for a client can be added only by a member of staff");

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //	RETRIVE - ClientImmunisationScheduleVo, ScheduleConfigurationVo
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // Get the client schedule
    ClientImmunisationScheduleVo clientSchedule = getClientImmunisationSchedule(clientRef);

    // Get the schedule configuration to add (return the client schedule as it is - null if there is
    // none - if the schedule configuration is null)
    ScheduleConfigurationVo schedule;
    if (scheduleRef == null) return clientSchedule;
    else {
      // Get the schedule configuration
      schedule =
          ScheduleConfigurationVoAssembler.create(
              (ScheduleConfiguration)
                  factory.getDomainObject(
                      ScheduleConfiguration.class, scheduleRef.getID_ScheduleConfiguration()));

      if (schedule == null) return clientSchedule;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //	START BUILDING ClientImmunisationSchedule (create a new one if needed)
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // At this point if there was no schedule configuration create a new one
    if (clientSchedule == null) {
      clientSchedule = new ClientImmunisationScheduleVo();
    }

    // Set the client (if not already set)
    if (clientSchedule.getClient() == null) {
      clientSchedule.setClient(clientLiteVo);
    }

    try {
      clientSchedule.addSchedule(schedule, mos, new DateTime());
    } catch (IllegalArgumentException e) {
      //			throw new DomainInterfaceException("Can not add default schedule configuration to client
      // immunisation schedule as it conflicts withs existing schedule.");
      throw new DomainInterfaceException(e.getMessage());
    }

    return clientSchedule;
  }