Пример #1
0
 private void validate(Vote vote) {
   if (!vote.getAnswer().isSaved()) {
     throw new IllegalStateException("Answer of " + vote + " should be saved first");
   }
   if (vote.getAuthor() != null && !vote.getAuthor().isSaved()) {
     throw new IllegalStateException("Author of " + vote + " should be saved first");
   }
 }
Пример #2
0
 @Override
 protected Vote loadObject(ResultSet resultSet) throws SQLException, MapperException {
   Vote vote =
       new Vote(
           Mappers.getForClass(User.class).loadById(resultSet.getLong("user_id")),
           Mappers.getForClass(Answer.class).loadById(resultSet.getLong("answer_id")),
           resultSet.getTimestamp("creation_datetime"));
   vote.setId(resultSet.getLong("id"));
   return vote;
 }
Пример #3
0
 @Override
 protected PreparedStatement getDeleteStatement(Vote vote) throws SQLException {
   PreparedStatement statement = getConnection().prepareStatement(DELETE_QUERY);
   try {
     statement.setLong(1, vote.getId());
     return statement;
   } catch (SQLException e) {
     statement.close();
     throw e;
   }
 }
Пример #4
0
 @Override
 protected PreparedStatement getInsertStatement(Vote vote) throws SQLException {
   validate(vote);
   PreparedStatement statement =
       getConnection().prepareStatement(INSERT_QUERY, Statement.RETURN_GENERATED_KEYS);
   try {
     User author = vote.getAuthor();
     if (author == null) {
       statement.setNull(1, Types.INTEGER);
     } else {
       statement.setLong(1, author.getId());
     }
     statement.setLong(2, vote.getAnswer().getId());
     statement.setTimestamp(3, DatabaseUtil.dateToTimestamp(vote.getCreationDate()));
     return statement;
   } catch (SQLException e) {
     statement.close();
     throw e;
   }
 }