Search in sources :

Example 6 with Select

use of com.raizlabs.android.dbflow.sql.language.Select in project pictureapp by EyeSeeTea.

the class CompositeScore method listByProgram.

/**
     * Select all composite score that belongs to a program
     *
     * @param program Program whose composite scores are searched.
     */
public static List<CompositeScore> listByProgram(Program program) {
    if (program == null || program.getId_program() == null) {
        return new ArrayList<>();
    }
    //FIXME: Apparently there is a bug in DBFlow joins that affects here. Question has a
    // column 'uid', and so do CompositeScore, so results are having Questions one, and
    // should keep CompositeScore one. To solve it, we've introduced a last join with
    // CompositeScore again and a HashSet to remove resulting duplicates
    //Take scores associated to questions of the program ('leaves')
    List<CompositeScore> compositeScoresByProgram = new Select().distinct().from(CompositeScore.class).as(compositeScoreName).join(Question.class, Join.JoinType.LEFT_OUTER).as(questionName).on(CompositeScore_Table.id_composite_score.withTable(compositeScoreAlias).eq(Question_Table.id_composite_score_fk.withTable(questionAlias))).join(Header.class, Join.JoinType.LEFT_OUTER).as(headerName).on(Question_Table.id_header_fk.withTable(questionAlias).eq(Header_Table.id_header.withTable(headerAlias))).join(Tab.class, Join.JoinType.LEFT_OUTER).as(tabName).on(Header_Table.id_tab_fk.withTable(headerAlias).eq(Tab_Table.id_tab.withTable(tabAlias))).join(Program.class, Join.JoinType.LEFT_OUTER).as(programName).on(Tab_Table.id_program_fk.withTable(tabAlias).eq(Program_Table.id_program.withTable(programAlias))).join(CompositeScore.class, Join.JoinType.LEFT_OUTER).as(compositeScoreTwoName).on(CompositeScore_Table.id_composite_score.withTable(compositeScoreAlias).eq(CompositeScore_Table.id_composite_score.withTable(compositeScoreTwoAlias))).where(Program_Table.id_program.withTable(programAlias).eq(program.getId_program())).orderBy(CompositeScore_Table.order_pos, true).queryList();
    // remove duplicates
    Set<CompositeScore> uniqueCompositeScoresByProgram = new HashSet<>();
    uniqueCompositeScoresByProgram.addAll(compositeScoresByProgram);
    compositeScoresByProgram.clear();
    compositeScoresByProgram.addAll(uniqueCompositeScoresByProgram);
    //Find parent scores from 'leaves'
    Set<CompositeScore> parentCompositeScores = new HashSet<>();
    for (CompositeScore compositeScore : compositeScoresByProgram) {
        parentCompositeScores.addAll(listParentCompositeScores(compositeScore));
    }
    compositeScoresByProgram.addAll(parentCompositeScores);
    Collections.sort(compositeScoresByProgram, new Comparator() {

        @Override
        public int compare(Object o1, Object o2) {
            CompositeScore cs1 = (CompositeScore) o1;
            CompositeScore cs2 = (CompositeScore) o2;
            return new Integer(cs1.getOrder_pos().compareTo(new Integer(cs2.getOrder_pos())));
        }
    });
    //return all scores
    return compositeScoresByProgram;
}
Also used : ArrayList(java.util.ArrayList) Select(com.raizlabs.android.dbflow.sql.language.Select) HashSet(java.util.HashSet) Comparator(java.util.Comparator)

Example 7 with Select

use of com.raizlabs.android.dbflow.sql.language.Select in project pictureapp by EyeSeeTea.

the class OrgUnit method getPrograms.

public List<Program> getPrograms() {
    if (programs == null) {
        List<OrgUnitProgramRelation> orgUnitProgramRelations = new Select().from(OrgUnitProgramRelation.class).where(OrgUnitProgramRelation_Table.id_org_unit_fk.eq(this.getId_org_unit())).queryList();
        this.programs = new ArrayList<>();
        for (OrgUnitProgramRelation programRelation : orgUnitProgramRelations) {
            programs.add(programRelation.getProgram());
        }
    }
    return programs;
}
Also used : Select(com.raizlabs.android.dbflow.sql.language.Select)

Example 8 with Select

use of com.raizlabs.android.dbflow.sql.language.Select in project pictureapp by EyeSeeTea.

the class Program method getOrgUnits.

public List<OrgUnit> getOrgUnits() {
    if (orgUnits == null) {
        List<OrgUnitProgramRelation> orgUnitProgramRelations = new Select().from(OrgUnitProgramRelation.class).where(OrgUnitProgramRelation_Table.id_program_fk.eq(this.getId_program())).queryList();
        this.orgUnits = new ArrayList<>();
        for (OrgUnitProgramRelation programRelation : orgUnitProgramRelations) {
            orgUnits.add(programRelation.getOrgUnit());
        }
    }
    return orgUnits;
}
Also used : Select(com.raizlabs.android.dbflow.sql.language.Select)

Example 9 with Select

use of com.raizlabs.android.dbflow.sql.language.Select in project pictureapp by EyeSeeTea.

the class Question method findRootQuestion.

/**
     * Find the first root question in the given tab
     *
     * This cannot be done due to a dbflow join bug
     * select q.*
     * from question q
     * left join header h on q.id_header=h.id_header
     * left join questionrelation qr on q.id_question=qr.id_question
     * where h.id_tab=1 and qr.id_question is null
     * order by q.order_pos
     */
public static Question findRootQuestion(Tab tab) {
    //Take every child question
    List<QuestionRelation> questionRelations = QuestionRelation.listAllParentChildRelations();
    if (questionRelations == null || questionRelations.size() == 0) {
        //flow without relations
        return new Select().from(Question.class).as(AppDatabase.questionName).join(Header.class, Join.JoinType.LEFT_OUTER).as(headerName).on(Question_Table.id_header_fk.withTable(questionAlias).eq(Header_Table.id_header.withTable(headerAlias))).join(Tab.class, Join.JoinType.LEFT_OUTER).as(tabName).on(Header_Table.id_tab_fk.withTable(headerAlias).eq(Tab_Table.id_tab.withTable(tabAlias))).where(Header_Table.id_tab_fk.withTable(headerAlias).eq(tab.getId_tab())).and(Tab_Table.type.withTable(tabAlias).eq(Constants.TAB_MULTI_QUESTION)).orderBy(Question_Table.order_pos.withTable(questionAlias), true).querySingle();
    }
    //Build a not in condition
    Iterator<QuestionRelation> questionRelationsIterator = questionRelations.iterator();
    Condition.In in = Question_Table.id_question.withTable(questionAlias).notIn(questionRelationsIterator.next().getQuestion().getId_question());
    while (questionRelationsIterator.hasNext()) {
        in.and(Long.toString(questionRelationsIterator.next().getQuestion().getId_question()));
    }
    return new Select().from(Question.class).as(questionName).join(Header.class, Join.JoinType.LEFT_OUTER).as(headerName).on(Question_Table.id_header_fk.withTable(questionAlias).eq(Header_Table.id_header.withTable(headerAlias))).where(Header_Table.id_tab_fk.withTable(headerAlias).eq(tab.getId_tab())).and(in).orderBy(Question_Table.order_pos, true).querySingle();
}
Also used : Condition(com.raizlabs.android.dbflow.sql.language.Condition) Select(com.raizlabs.android.dbflow.sql.language.Select)

Example 10 with Select

use of com.raizlabs.android.dbflow.sql.language.Select in project pictureapp by EyeSeeTea.

the class Question method getSiblingNoParent.

/**
     * Returns next question from same header considering the order.
     * This should not be a child question.
     */
private Question getSiblingNoParent() {
    //Take every child question
    List<QuestionRelation> questionRelations = QuestionRelation.listAllParentChildRelations();
    //Build a not in condition
    Condition.In in;
    if (questionRelations.size() == 0) {
        //Flow without children
        in = Question_Table.id_question.withTable(questionAlias).notIn(this.getId_question());
    } else {
        Iterator<QuestionRelation> questionRelationsIterator = questionRelations.iterator();
        in = Question_Table.id_question.withTable(questionAlias).notIn(questionRelationsIterator.next().getQuestion().getId_question());
        while (questionRelationsIterator.hasNext()) {
            in.and(Long.toString(questionRelationsIterator.next().getQuestion().getId_question()));
        }
    }
    //Siblings without parents relations
    this.sibling = new Select().from(Question.class).as(questionName).where(Question_Table.order_pos.withTable(questionAlias).greaterThan(this.getOrder_pos())).and(in).orderBy(OrderBy.fromProperty(Question_Table.order_pos).ascending()).querySingle();
    //no question behind this one -> build a null question to use cached value
    if (this.sibling == null) {
        this.sibling = buildNullQuestion();
    }
    return this.sibling;
}
Also used : Condition(com.raizlabs.android.dbflow.sql.language.Condition) Select(com.raizlabs.android.dbflow.sql.language.Select)

Aggregations

Select (com.raizlabs.android.dbflow.sql.language.Select)15 ArrayList (java.util.ArrayList)3 Condition (com.raizlabs.android.dbflow.sql.language.Condition)2 ProgramFlow (org.hisp.dhis.client.sdk.android.api.persistence.flow.ProgramFlow)2 Intent (android.content.Intent)1 User (com.android.example.devsummit.archdemo.vo.User)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 CompositeScore (org.eyeseetea.malariacare.data.database.model.CompositeScore)1 Tab (org.eyeseetea.malariacare.data.database.model.Tab)1 OrganisationUnitToProgramRelationFlow (org.hisp.dhis.client.sdk.android.api.persistence.flow.OrganisationUnitToProgramRelationFlow)1 TrackedEntityDataValueFlow (org.hisp.dhis.client.sdk.android.api.persistence.flow.TrackedEntityDataValueFlow)1 ProgramType (org.hisp.dhis.client.sdk.models.program.ProgramType)1