public static List<Question> listAllByTabs(List<Tab> tabs) { if (tabs == null || tabs.size() == 0) { return new ArrayList(); } Iterator<Tab> iterator = tabs.iterator(); In in = Condition.column(ColumnAlias.columnWithTable("t", Tab$Table.ID_TAB)) .in(Long.toString(iterator.next().getId_tab())); while (iterator.hasNext()) { in.and(Long.toString(iterator.next().getId_tab())); } return new Select() .from(Question.class) .as("q") .join(Header.class, Join.JoinType.LEFT) .as("h") .on( Condition.column(ColumnAlias.columnWithTable("q", Question$Table.ID_HEADER)) .eq(ColumnAlias.columnWithTable("h", Header$Table.ID_HEADER))) .join(Tab.class, Join.JoinType.LEFT) .as("t") .on( Condition.column(ColumnAlias.columnWithTable("h", Header$Table.ID_TAB)) .eq(ColumnAlias.columnWithTable("t", Tab$Table.ID_TAB))) .where(in) .orderBy(Tab$Table.ORDER_POS) .orderBy(Question$Table.ORDER_POS) .queryList(); }
/** * Returns all the questions that belongs to a program * * @param tabGroup * @return */ public static List<Question> listByTabGroup(TabGroup tabGroup) { if (tabGroup == null || tabGroup.getId_tab_group() == null) { return new ArrayList(); } // return Question.findWithQuery(Question.class, LIST_ALL_BY_PROGRAM, // program.getId().toString()); return new Select() .all() .from(Question.class) .as("q") .join(Header.class, Join.JoinType.LEFT) .as("h") .on( Condition.column(ColumnAlias.columnWithTable("q", Question$Table.ID_HEADER)) .eq(ColumnAlias.columnWithTable("h", Header$Table.ID_HEADER))) .join(Tab.class, Join.JoinType.LEFT) .as("t") .on( Condition.column(ColumnAlias.columnWithTable("h", Header$Table.ID_TAB)) .eq(ColumnAlias.columnWithTable("t", Tab$Table.ID_TAB))) .join(TabGroup.class, Join.JoinType.LEFT) .as("tg") .on( Condition.column(ColumnAlias.columnWithTable("t", Tab$Table.ID_TAB_GROUP)) .eq(ColumnAlias.columnWithTable("tg", TabGroup$Table.ID_TAB_GROUP))) .where( Condition.column(ColumnAlias.columnWithTable("tg", TabGroup$Table.ID_TAB_GROUP)) .eq(tabGroup.getId_tab_group())) .orderBy(Tab$Table.ORDER_POS) .orderBy(Question$Table.ORDER_POS) .queryList(); }
public List<Match> getMatches() { if (matches == null) { matches = new Select() .from(Match.class) .as("m") .join(QuestionOption.class, Join.JoinType.LEFT) .as("qo") .on( Condition.column(ColumnAlias.columnWithTable("m", Match$Table.ID_MATCH)) .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_MATCH))) .where( Condition.column( ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_QUESTION)) .eq(this.getId_question())) .queryList(); } return matches; }
public List<Question> getChildren() { if (this.children == null) { List<Match> matches = getMatches(); if (matches.size() == 0) { this.children = new ArrayList<>(); return this.children; } Iterator<Match> matchesIterator = matches.iterator(); In in = Condition.column(ColumnAlias.columnWithTable("m", Match$Table.ID_MATCH)) .in(Long.toString(matchesIterator.next().getId_match())); while (matchesIterator.hasNext()) { in.and(Long.toString(matchesIterator.next().getId_match())); } // Select question from questionrelation where operator=1 and id_match in (..) this.children = new Select() .from(Question.class) .as("q") // Question + QuestioRelation .join(QuestionRelation.class, Join.JoinType.LEFT) .as("qr") .on( Condition.column(ColumnAlias.columnWithTable("q", Question$Table.ID_QUESTION)) .eq(ColumnAlias.columnWithTable("qr", QuestionRelation$Table.ID_QUESTION))) // +Match .join(Match.class, Join.JoinType.LEFT) .as("m") .on( Condition.column( ColumnAlias.columnWithTable( "qr", QuestionRelation$Table.ID_QUESTION_RELATION)) .eq(ColumnAlias.columnWithTable("m", Match$Table.ID_QUESTION_RELATION))) // Parent child relationship .where(in) // In clause .and( Condition.column( ColumnAlias.columnWithTable("qr", QuestionRelation$Table.OPERATION)) .eq(QuestionRelation.PARENT_CHILD)) .queryList(); } return this.children; }
/** * Checks if this question is triggered according to the current values of the given survey. Only * applies to question with answers DROPDOWN_DISABLED * * @param idSurvey * @return */ public boolean isTriggered(float idSurvey) { // Only disabled dropdowns if (this.getOutput() != Constants.DROPDOWN_LIST_DISABLED) { return false; } // Find questionoptions for q1 and q2 and check same match List<QuestionOption> questionOptions = new Select() .from(QuestionOption.class) .as("qo") .join(Match.class, Join.JoinType.LEFT) .as("m") .on( Condition.column(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_MATCH)) .eq(ColumnAlias.columnWithTable("m", Match$Table.ID_MATCH))) .join(QuestionRelation.class, Join.JoinType.LEFT) .as("qr") .on( Condition.column(ColumnAlias.columnWithTable("m", Match$Table.ID_QUESTION_RELATION)) .eq( ColumnAlias.columnWithTable( "qr", QuestionRelation$Table.ID_QUESTION_RELATION))) .join(Value.class, Join.JoinType.LEFT) .as("v") .on( Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_QUESTION)) .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_QUESTION)), Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_OPTION)) .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_OPTION))) .where( Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_SURVEY)) .eq(idSurvey)) .and( Condition.column( ColumnAlias.columnWithTable("qr", QuestionRelation$Table.ID_QUESTION)) .eq(this.getId_question())) .and( Condition.column( ColumnAlias.columnWithTable("qr", QuestionRelation$Table.OPERATION)) .eq(Constants.OPERATION_TYPE_MATCH)) .queryList(); // No values no match if (questionOptions.size() != 2) { return false; } // Match is triggered if questionoptions have same matchid long idmatchQ1 = questionOptions.get(0).getMatch().getId_match(); long idmatchQ2 = questionOptions.get(1).getMatch().getId_match(); return idmatchQ1 == idmatchQ2; }
/** * Returns a Question by a UID * * @param uid * @return */ public static Question getQuestionByUid(String uid) { return new Select() .from(Question.class) .where(Condition.column(ColumnAlias.column(Question$Table.UID)).eq(uid)) .querySingle(); }
/** * Gets all the children compulsory questions, and returns the number of active children * * @param * @return */ public static int countChildrenCompulsoryBySurvey(Long id_survey) { int numActiveChildrens = 0; // This query returns a list of children compulsory questions // But the id_question is not correct, because is the the parent id_questions from the // questionOption relation. List<Question> questions = new Select() .from(Question.class) .as("q") .join(QuestionRelation.class, Join.JoinType.LEFT) .as("qr") .on( Condition.column(ColumnAlias.columnWithTable("q", Question$Table.ID_QUESTION)) .eq(ColumnAlias.columnWithTable("qr", QuestionRelation$Table.ID_QUESTION))) .join(Match.class, Join.JoinType.LEFT) .as("m") .on( Condition.column( ColumnAlias.columnWithTable( "qr", QuestionRelation$Table.ID_QUESTION_RELATION)) .eq(ColumnAlias.columnWithTable("m", Match$Table.ID_QUESTION_RELATION))) .join(QuestionOption.class, Join.JoinType.LEFT) .as("qo") .on( Condition.column(ColumnAlias.columnWithTable("m", Match$Table.ID_MATCH)) .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_MATCH))) .join(Value.class, Join.JoinType.LEFT) .as("v") .on( Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_QUESTION)) .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_QUESTION)), Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_OPTION)) .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_OPTION))) // Parent Child relationship .where( Condition.column( ColumnAlias.columnWithTable("qr", QuestionRelation$Table.OPERATION)) .eq(1)) // For the given survey .and( Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_SURVEY)) .eq(id_survey)) // The child question requires an answer .and( Condition.column(ColumnAlias.columnWithTable("q", Question$Table.OUTPUT)) .isNot(Constants.NO_ANSWER)) .and( Condition.column(ColumnAlias.columnWithTable("q", Question$Table.COMPULSORY)) .eq(true)) .queryList(); // checks if the children questions are active by UID // Note: the question id_question is wrong because dbflow query overwrites the children // id_question with the parent id_question. for (Question question : questions) { if (!Question.isHiddenQuestionByUidAndSurvey(question.getUid(), id_survey)) { numActiveChildrens++; } } // Return number of active compulsory children return (int) (numActiveChildrens); }
/** * Counts the number of compulsory questions (without a parent question). * * @param tabGroup * @return */ public static int countCompulsoryByProgram(TabGroup tabGroup) { if (tabGroup == null || tabGroup.getId_tab_group() == null) { return 0; } // Count all the quesions that may have an answer long totalAnswerableQuestions = new Select() .count() .from(Question.class) .as("q") .join(Header.class, Join.JoinType.LEFT) .as("h") .on( Condition.column(ColumnAlias.columnWithTable("q", Question$Table.ID_HEADER)) .eq(ColumnAlias.columnWithTable("h", Header$Table.ID_HEADER))) .join(Tab.class, Join.JoinType.LEFT) .as("t") .on( Condition.column(ColumnAlias.columnWithTable("h", Header$Table.ID_TAB)) .eq(ColumnAlias.columnWithTable("t", Tab$Table.ID_TAB))) .where( Condition.column(ColumnAlias.columnWithTable("q", Question$Table.COMPULSORY)) .is(true)) .and( Condition.column(ColumnAlias.columnWithTable("t", Tab$Table.ID_TAB_GROUP)) .eq(tabGroup.getId_tab_group())) .count(); // Count children questions from the given taggroup long numChildrenQuestion = new Select() .count() .from(QuestionRelation.class) .as("qr") .join(Question.class, Join.JoinType.LEFT) .as("q") .on( Condition.column( ColumnAlias.columnWithTable("qr", QuestionRelation$Table.ID_QUESTION)) .eq(ColumnAlias.columnWithTable("q", Question$Table.ID_QUESTION))) .join(Header.class, Join.JoinType.LEFT) .as("h") .on( Condition.column(ColumnAlias.columnWithTable("q", Question$Table.ID_HEADER)) .eq(ColumnAlias.columnWithTable("h", Header$Table.ID_HEADER))) .join(Tab.class, Join.JoinType.LEFT) .as("t") .on( Condition.column(ColumnAlias.columnWithTable("h", Header$Table.ID_TAB)) .eq(ColumnAlias.columnWithTable("t", Tab$Table.ID_TAB))) .where( Condition.column(ColumnAlias.columnWithTable("q", Question$Table.COMPULSORY)) .is(true)) .and( Condition.column(ColumnAlias.columnWithTable("t", Tab$Table.ID_TAB_GROUP)) .eq(tabGroup.getId_tab_group())) .and( Condition.column( ColumnAlias.columnWithTable("qr", QuestionRelation$Table.OPERATION)) .eq(Constants.OPERATION_TYPE_PARENT)) .count(); // Return number of parents (total - children) return (int) (totalAnswerableQuestions - numChildrenQuestion); }
/** * Checks if a unique question UID is shown according to the values of the given survey * * @param uid * @param id_survey * @return */ public static boolean isHiddenQuestionByUidAndSurvey(String uid, Long id_survey) { // get question by uid Question question = getQuestionByUid(uid); if (question == null) return false; Long id_question = question.getId_question(); long hasParentOptionActivated = new Select() .count() .from(Value.class) .as("v") .join(QuestionOption.class, Join.JoinType.LEFT) .as("qo") .on( Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_QUESTION)) .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_QUESTION)), Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_OPTION)) .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_OPTION))) .join(Match.class, Join.JoinType.LEFT) .as("m") .on( Condition.column(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_MATCH)) .eq(ColumnAlias.columnWithTable("m", Match$Table.ID_MATCH))) .join(QuestionRelation.class, Join.JoinType.LEFT) .as("qr") .on( Condition.column(ColumnAlias.columnWithTable("m", Match$Table.ID_QUESTION_RELATION)) .eq( ColumnAlias.columnWithTable( "qr", QuestionRelation$Table.ID_QUESTION_RELATION))) // Parent child relationship .where( Condition.column( ColumnAlias.columnWithTable("qr", QuestionRelation$Table.OPERATION)) .eq(1)) // For the given survey .and( Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_SURVEY)) .eq(id_survey)) // The child question in the relationship is 'this' .and( Condition.column( ColumnAlias.columnWithTable("qr", QuestionRelation$Table.ID_QUESTION)) .eq(id_question)) .count(); return hasParentOptionActivated > 0 ? false : true; }
/** * Checks if this question is shown according to the values of the given survey * * @param idSurvey * @return */ public boolean isHiddenBySurvey(float idSurvey) { // No question relations if (!hasParent()) { return false; } long hasParentOptionActivated = new Select() .count() .from(Value.class) .as("v") .join(QuestionOption.class, Join.JoinType.LEFT) .as("qo") .on( Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_QUESTION)) .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_QUESTION)), Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_OPTION)) .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_OPTION))) .join(Match.class, Join.JoinType.LEFT) .as("m") .on( Condition.column(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_MATCH)) .eq(ColumnAlias.columnWithTable("m", Match$Table.ID_MATCH))) .join(QuestionRelation.class, Join.JoinType.LEFT) .as("qr") .on( Condition.column(ColumnAlias.columnWithTable("m", Match$Table.ID_QUESTION_RELATION)) .eq( ColumnAlias.columnWithTable( "qr", QuestionRelation$Table.ID_QUESTION_RELATION))) // Parent child relationship .where( Condition.column( ColumnAlias.columnWithTable("qr", QuestionRelation$Table.OPERATION)) .eq(1)) // For the given survey .and( Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_SURVEY)) .eq(idSurvey)) // The child question in the relationship is 'this' .and( Condition.column( ColumnAlias.columnWithTable("qr", QuestionRelation$Table.ID_QUESTION)) .eq(this.getId_question())) .count(); // Parent with the right value -> not hidden return hasParentOptionActivated > 0 ? false : true; }