Пример #1
0
  /**
   * @param id appointment id to search for
   * @return if the appointment exists it is returned, otherwise null is the result.
   * @throws PersistenceException if there are complications with the persitance layer
   */
  @Override
  public Appointment searchByID(int id) throws PersistenceException {
    LOGGER.info("Searching record in appointment table with id: " + id + "..");

    try {
      searchByIDStm.setInt(1, id);
      ResultSet rs_searchByID = searchByIDStm.executeQuery();
      rs_searchByID.next();

      Appointment foundAppointment = null;
      foundAppointment =
          new Appointment(
              rs_searchByID.getInt(1),
              rs_searchByID.getDate(2),
              rs_searchByID.getInt(3),
              rs_searchByID.getInt(4),
              rs_searchByID.getBoolean(5),
              rs_searchByID.getBoolean(6));

      if (foundAppointment != null) {
        foundAppointment.setSessionName(
            trainingsSessionDAO.searchByID(foundAppointment.getSession_id()).getName());
        String setNames = "";
        if (trainingsSessionDAO.searchByID(foundAppointment.getSession_id()).getExerciseSets()
            != null) {
          for (ExerciseSet exerciseSet :
              trainingsSessionDAO.searchByID(foundAppointment.getSession_id()).getExerciseSets()) {
            setNames +=
                (exerciseSet.getRepeat() + " " + exerciseSet.getExercise().getName() + '\n');
          }
        }
        foundAppointment.setSetNames(setNames);

        foundAppointment.setSession(
            trainingsSessionDAO.searchByID(foundAppointment.getSession_id()));
      }

      LOGGER.info(
          "Appointment with id: " + id + ", successfully read from database." + foundAppointment);
      return foundAppointment;
    } catch (SQLException e) {
      LOGGER.error("Failed to search for an appointment with id: " + id + ". - " + e.getMessage());
      throw new PersistenceException("Failed to search for an appointment with id: " + id + ".");
    }
  }
Пример #2
0
  /**
   * Finds all appointments in the persistence, deleted appointments are ignored.
   *
   * @return List of all appointments stored in the persistence
   * @throws PersistenceException if there are complications with the persitance layer
   */
  @Override
  public List<Appointment> findAll() throws PersistenceException {
    LOGGER.info("Finding all records in appointment table..");
    try {
      ResultSet rs_findAll = findAllStm.executeQuery();
      List<Appointment> result = new ArrayList<>();

      while (rs_findAll.next()) {
        Appointment appointment =
            new Appointment(
                rs_findAll.getInt(1),
                rs_findAll.getDate(2),
                rs_findAll.getInt(3),
                rs_findAll.getInt(4),
                rs_findAll.getBoolean(5),
                rs_findAll.getBoolean(6));

        appointment.setSessionName(
            trainingsSessionDAO.searchByID(appointment.getSession_id()).getName());
        String setNames = "";

        if (trainingsSessionDAO.searchByID(appointment.getSession_id()).getExerciseSets() != null) {
          for (ExerciseSet exerciseSet :
              trainingsSessionDAO.searchByID(appointment.getSession_id()).getExerciseSets()) {
            setNames +=
                (exerciseSet.getRepeat() + " " + exerciseSet.getExercise().getName() + '\n');
          }
        }

        appointment.setSetNames(setNames);

        appointment.setSession(trainingsSessionDAO.searchByID(appointment.getSession_id()));

        result.add(appointment);
      }

      LOGGER.info("All record successfully read from appointment table.");
      return result;

    } catch (SQLException e) {
      LOGGER.error("Failed to find all records in appointment table. - " + e.getMessage());
      throw new PersistenceException("Failed to find all records in appointment table.", e);
    }
  }