@Override public void swapQuestionPosition(Question questionToSwap, List<Question> questions, boolean up) { Connection conn = null; PreparedStatement stat = null; ResultSet res = null; Question nearQuestionToSwap = null; try { for (int i = 0; i < questions.size(); i++) { Question question = questions.get(i); if (question.getId() == questionToSwap.getId()) { if (up && i > 0) { nearQuestionToSwap = questions.get(i - 1); } else if (!up && i < (questions.size() - 1)) { nearQuestionToSwap = questions.get(i + 1); } break; } } if (null == nearQuestionToSwap) { return; } conn = this.getConnection(); conn.setAutoCommit(false); int initPos = questionToSwap.getPos(); questionToSwap.setPos(nearQuestionToSwap.getPos()); nearQuestionToSwap.setPos(initPos); this.updateQuestionPosition(conn, nearQuestionToSwap); this.updateQuestionPosition(conn, questionToSwap); conn.commit(); } catch (Throwable t) { this.executeRollback(conn); _logger.error("Errore swapping position of two 'choice' objects", t); throw new RuntimeException("Errore swapping position of two 'choice' objects", t); } finally { closeDaoResources(res, stat, conn); } }
private void updateQuestionPosition(Connection conn, Question questionToMove) { PreparedStatement stat = null; try { stat = conn.prepareStatement(MOVE_QUESTION); stat.setInt(1, questionToMove.getPos()); stat.setInt(2, questionToMove.getId()); stat.executeUpdate(); } catch (Throwable t) { this.executeRollback(conn); _logger.error("Error while updating the position of question", t); throw new RuntimeException("Error while updating the position of question", t); } finally { closeDaoResources(null, stat); } }