/** * Removes a question from a survey. Takes care of decrementing all subsequent question indices. */ public void deleteQuestion(int surveyId, int questionIndex) { Preconditions.checkArgument(surveyId != 0); Preconditions.checkArgument(questionIndex >= 0); int size = countQuestions(surveyId); if (questionIndex >= size) { throw new RuntimeException( "Question index " + questionIndex + " out of range (" + size + ")" + " for survey " + surveyId); } delete(SurveyQuestionRecord.getKey(surveyId, questionIndex)); for (int ii = questionIndex + 1; ii < size; ++ii) { updateQuestionIndex(surveyId, ii, ii - 1); } }
/** Inserts a new survey question. */ public void insertQuestion(SurveyQuestionRecord question) { Preconditions.checkArgument(question.surveyId != 0); Preconditions.checkArgument(question.questionIndex == -1); question.questionIndex = countQuestions(question.surveyId); insert(question); }
/** Utility method to update only the index column of a question. */ protected void updateQuestionIndex(int surveyId, int index, int newIndex) { updatePartial( SurveyQuestionRecord.getKey(surveyId, index), SurveyQuestionRecord.QUESTION_INDEX, newIndex); }