@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 List<Choice> getQuestionChoices(int id) { List<Choice> list = new ArrayList<Choice>(); try { Question question = this.loadQuestion(id); if (null != question) { list = question.getChoices(); } } catch (Throwable t) { _logger.error("Error while loading the choices belonging to the question of ID {}", id, t); throw new RuntimeException("Error while loading the choices belonging a question", t); } return list; }
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); } }
@Override public Question loadQuestion(int id) { Question question = null; Connection conn = null; PreparedStatement stat = null; ResultSet res = null; try { conn = this.getConnection(); stat = conn.prepareStatement(GET_COMPLETE_QUESTION_BY_ID); stat.setInt(1, id); res = stat.executeQuery(); while (res.next()) { if (null == question) { question = this.buildQuestionRecordFromResultSet(res, 1); } // get extra info: questions need the survey type int questionnaireValue = res.getInt(13); boolean questionnaire = questionnaireValue == 1 ? true : false; ApsProperties titles = new ApsProperties(); titles.loadFromXml(res.getString(14)); question.setExtraInfo(questionnaire, titles); Choice choice = this.buildChoiceRecordFromResultSet(res, 8); if (null == choice) continue; choice.setExtraInfo( question.getSurveyId(), question.isQuestionnaire(), question.getSurveyTitles(), question.getQuestions()); if (null == question.getChoice(choice.getId())) { question.getChoices().add(choice); } } } catch (Throwable t) { _logger.error("Error while loading the question of ID {}", t); throw new RuntimeException("Error while loading a question", t); } finally { closeDaoResources(res, stat, conn); } return question; }
@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); } }