@Override public void saveQuestionInSortedPosition(Question question) { Connection conn = null; PreparedStatement stat = null; ResultSet res = null; try { conn = this.getConnection(); conn.setAutoCommit(false); stat = conn.prepareStatement(GET_QUESTION_GREATER_POS); stat.setInt(1, question.getSurveyId()); res = stat.executeQuery(); if (res.next()) { int lastPosition = res.getInt(1); question.setPos(++lastPosition); } else { question.setPos(0); } this.saveQuestion(conn, question); conn.commit(); } catch (Throwable t) { this.executeRollback(conn); _logger.error("Error while saving a question in a sorted position", t); throw new RuntimeException("Error while saving a question in a sorted position", t); } finally { closeDaoResources(res, stat, conn); } }
@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); } }