Search in sources :

Example 1 with QuestionRelation

use of org.eyeseetea.malariacare.data.database.model.QuestionRelation in project pictureapp by EyeSeeTea.

the class PopulateDB method populateDB.

public static void populateDB(Context context) throws IOException {
    // Reset inner references
    cleanInnerLists();
    for (String table : tables2populate) {
        Log.i(TAG, "Loading csv: " + table);
        CSVReader reader = null;
        try {
            reader = new CSVReader(new InputStreamReader(new PopulateDBStrategy().openFile(context, table)), SEPARATOR, QUOTECHAR);
        } catch (FileNotFoundException e) {
            tableNotExistLog(e, table);
        } catch (IOException e) {
            tableNotExistLog(e, table);
        }
        if (reader == null) {
            continue;
        }
        String[] line;
        while ((line = reader.readNext()) != null && !line[0].isEmpty()) {
            switch(table) {
                case PROGRAMS_CSV:
                    Program program = new Program();
                    program.setUid(line[1]);
                    program.setName(line[2]);
                    program.setStageUid(line[3]);
                    program.save();
                    programList.put(Integer.valueOf(line[0]), program);
                    break;
                case TABS_CSV:
                    Tab tab = new Tab();
                    tab.setName(line[1]);
                    tab.setOrder_pos(Integer.valueOf(line[2]));
                    tab.setProgram(programList.get(Integer.valueOf(line[3])));
                    tab.setType(Integer.valueOf(line[4]));
                    tab.save();
                    tabList.put(Integer.valueOf(line[0]), tab);
                    break;
                case HEADERS_CSV:
                    Header header = new Header();
                    header.setShort_name(line[1]);
                    header.setName(line[2]);
                    header.setOrder_pos(Integer.valueOf(line[3]));
                    header.setTab(tabList.get(Integer.valueOf(line[4])));
                    header.save();
                    headerList.put(Integer.valueOf(line[0]), header);
                    break;
                case ANSWERS_CSV:
                    Answer answer = new Answer();
                    answer.setName(line[1]);
                    answer.save();
                    answerList.put(Integer.valueOf(line[0]), answer);
                    break;
                case OPTION_ATTRIBUTES_CSV:
                    OptionAttribute optionAttribute = new OptionAttribute();
                    optionAttribute.setBackground_colour(line[1]);
                    optionAttribute.setPath(line[2]);
                    if (line.length > 3 && !line[3].equals("")) {
                        optionAttribute.setHorizontal_alignment(Integer.valueOf(line[3]));
                    } else {
                        optionAttribute.setHorizontal_alignment(OptionAttribute.DEFAULT_HORIZONTAL_ALIGNMENT);
                    }
                    if (line.length > 4 && !line[4].equals("")) {
                        optionAttribute.setVertical_alignment(Integer.valueOf(line[4]));
                    } else {
                        optionAttribute.setHorizontal_alignment(OptionAttribute.DEFAULT_VERTICAL_ALIGNMENT);
                    }
                    if (line.length > 5 && !line[5].equals("")) {
                        optionAttribute.setText_size(Integer.valueOf(line[5]));
                    } else {
                        optionAttribute.setText_size(Integer.parseInt(PreferencesState.getInstance().getContext().getResources().getString(R.string.default_option_text_size)));
                    }
                    if (line.length > 6 && !line[6].equals("")) {
                        optionAttribute.setDefaultOption(Integer.valueOf(line[6]));
                    } else {
                        optionAttribute.setDefaultOption(0);
                    }
                    optionAttribute.save();
                    optionAttributeList.put(Integer.valueOf(line[0]), optionAttribute);
                    break;
                case OPTIONS_CSV:
                    Option option = new Option();
                    option.setName(line[1]);
                    option.setCode(line[2]);
                    option.setFactor(Float.valueOf(line[3]));
                    option.setAnswer(answerList.get(Integer.valueOf(line[4])));
                    if (line[5] != null && !line[5].isEmpty()) {
                        option.setOptionAttribute(optionAttributeList.get(Integer.valueOf(line[5])));
                    }
                    option.save();
                    optionList.put(Integer.valueOf(line[0]), option);
                    break;
                case QUESTIONS_CSV:
                    Question question = new Question();
                    question.setCode(line[1]);
                    question.setDe_name(line[2]);
                    question.setHelp_text(line[3]);
                    question.setForm_name(line[4]);
                    question.setUid(line[5]);
                    question.setOrder_pos(Integer.valueOf(line[6]));
                    question.setNumerator_w(Float.valueOf(line[7]));
                    question.setDenominator_w(Float.valueOf(line[8]));
                    question.setHeader(headerList.get(Integer.valueOf(line[9])));
                    if (!line[10].equals("")) {
                        question.setAnswer(answerList.get(Integer.valueOf(line[10])));
                    }
                    if (!line[11].equals("")) {
                        question.setQuestion(questionList.get(Integer.valueOf(line[11])));
                    }
                    question.setOutput(Integer.valueOf(line[12]));
                    question.setTotalQuestions(Integer.valueOf(line[13]));
                    question.setVisible(Integer.valueOf(line[14]));
                    if (line.length > 15 && !line[15].equals("")) {
                        question.setPath((line[15]));
                    }
                    if (line.length > 16 && !line[16].equals("")) {
                        question.setCompulsory(Integer.valueOf(line[16]));
                    } else {
                        question.setCompulsory(Question.QUESTION_NOT_COMPULSORY);
                    }
                    question.save();
                    questionList.put(Integer.valueOf(line[0]), question);
                    break;
                case QUESTION_RELATIONS_CSV:
                    QuestionRelation questionRelation = new QuestionRelation();
                    questionRelation.setOperation(Integer.valueOf(line[1]));
                    questionRelation.setQuestion(questionList.get(Integer.valueOf(line[2])));
                    questionRelation.save();
                    questionRelationList.put(Integer.valueOf(line[0]), questionRelation);
                    break;
                case MATCHES:
                    Match match = new Match();
                    match.setQuestionRelation(questionRelationList.get(Integer.valueOf(line[1])));
                    match.save();
                    matchList.put(Long.valueOf(line[0]), match);
                    break;
                case QUESTION_OPTIONS_CSV:
                    QuestionOption questionOption = new QuestionOption();
                    questionOption.setQuestion(questionList.get(Integer.valueOf(line[1])));
                    if (!line[2].equals("")) {
                        questionOption.setOption(optionList.get(Integer.valueOf(line[2])));
                    }
                    if (!line[3].equals("")) {
                        questionOption.setMatch(matchList.get(Long.valueOf(line[3])));
                    }
                    questionOption.save();
                    break;
                case QUESTION_THRESHOLDS_CSV:
                    QuestionThreshold questionThreshold = new QuestionThreshold();
                    questionThreshold.setMatch(matchList.get(Long.valueOf(line[1])));
                    questionThreshold.setQuestion(questionList.get(Integer.valueOf(line[2])));
                    if (!line[3].equals("")) {
                        questionThreshold.setMinValue(Integer.valueOf(line[3]));
                    }
                    if (!line[4].equals("")) {
                        questionThreshold.setMaxValue(Integer.valueOf(line[4]));
                    }
                    questionThreshold.save();
                    break;
                case DRUGS_CSV:
                    Drug drug = PopulateRow.populateDrugs(line, null);
                    drug.insert();
                    drugList.put(Long.parseLong(line[0]), drug);
                    break;
                case PARTNER_CSV:
                    Partner partner = PopulateRow.populateOrganisations(line, null);
                    partner.insert();
                    organisationList.put(Long.parseLong(line[0]), partner);
                    break;
                case TREATMENT_CSV:
                    Treatment treatment = PopulateRow.populateTreatments(line, organisationList, stringKeyList, null);
                    treatment.insert();
                    treatmentList.put(Long.parseLong(line[0]), treatment);
                    break;
                case DRUG_COMBINATIONS_CSV:
                    PopulateRow.populateDrugCombinations(line, drugList, treatmentList, null).insert();
                    break;
                case TREATMENT_MATCHES_CSV:
                    PopulateRow.populateTreatmentMatches(line, treatmentList, matchList, null).insert();
                    break;
                case STRING_KEY_CSV:
                    StringKey stringKey = PopulateRow.populateStringKey(line, null);
                    stringKey.insert();
                    stringKeyList.put(Long.valueOf(line[0]), stringKey);
                    break;
                case TRANSLATION_CSV:
                    PopulateRow.populateTranslation(line, stringKeyList, null).insert();
                    break;
            }
        }
        reader.close();
    }
    // Free references since the maps are static
    cleanInnerLists();
}
Also used : Drug(org.eyeseetea.malariacare.data.database.model.Drug) QuestionThreshold(org.eyeseetea.malariacare.data.database.model.QuestionThreshold) QuestionOption(org.eyeseetea.malariacare.data.database.model.QuestionOption) Program(org.eyeseetea.malariacare.data.database.model.Program) InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) QuestionRelation(org.eyeseetea.malariacare.data.database.model.QuestionRelation) StringKey(org.eyeseetea.malariacare.data.database.model.StringKey) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) PopulateDBStrategy(org.eyeseetea.malariacare.data.database.utils.PopulateDBStrategy) OptionAttribute(org.eyeseetea.malariacare.data.database.model.OptionAttribute) Match(org.eyeseetea.malariacare.data.database.model.Match) TreatmentMatch(org.eyeseetea.malariacare.data.database.model.TreatmentMatch) Answer(org.eyeseetea.malariacare.data.database.model.Answer) Treatment(org.eyeseetea.malariacare.data.database.model.Treatment) Tab(org.eyeseetea.malariacare.data.database.model.Tab) Header(org.eyeseetea.malariacare.data.database.model.Header) QuestionOption(org.eyeseetea.malariacare.data.database.model.QuestionOption) Option(org.eyeseetea.malariacare.data.database.model.Option) Question(org.eyeseetea.malariacare.data.database.model.Question) Partner(org.eyeseetea.malariacare.data.database.model.Partner)

Example 2 with QuestionRelation

use of org.eyeseetea.malariacare.data.database.model.QuestionRelation in project pictureapp by EyeSeeTea.

the class UpdateDB method updateMatches.

public static void updateMatches(Context context, boolean updateCSV) throws IOException {
    if (updateCSV) {
        FileCsvs fileCsvs = new FileCsvs();
        fileCsvs.saveCsvFromAssetsToFile(PopulateDB.MATCHES);
    }
    List<Match> matches = Match.listAll();
    HashMap<Long, QuestionRelation> questionsrelationIds = RelationsIdCsvDB.getQuestionRelationIdRelationCsvDB(context);
    CSVReader reader = new CSVReader(new InputStreamReader(context.openFileInput(PopulateDB.MATCHES)), SEPARATOR, QUOTECHAR);
    String[] line;
    int i = 0;
    while ((line = reader.readNext()) != null) {
        if (i < matches.size()) {
            populateMatch(line, questionsrelationIds, matches.get(i)).save();
        } else {
            Match match = populateMatch(line, questionsrelationIds, null);
            match.insert();
        }
        i++;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) PopulateRow.populateQuestionRelation(org.eyeseetea.malariacare.data.database.utils.populatedb.PopulateRow.populateQuestionRelation) QuestionRelation(org.eyeseetea.malariacare.data.database.model.QuestionRelation) PopulateRow.populateMatch(org.eyeseetea.malariacare.data.database.utils.populatedb.PopulateRow.populateMatch) Match(org.eyeseetea.malariacare.data.database.model.Match) TreatmentMatch(org.eyeseetea.malariacare.data.database.model.TreatmentMatch)

Example 3 with QuestionRelation

use of org.eyeseetea.malariacare.data.database.model.QuestionRelation in project pictureapp by EyeSeeTea.

the class PopulateDB method addNotTestedRemminder.

public static void addNotTestedRemminder(Context context) throws IOException {
    // Reset inner references
    cleanInnerLists();
    List<Option> actualOptions = Option.getAllOptions();
    List<Question> actualQuestions = Question.getAllQuestions();
    questionList = new LinkedHashMap<Integer, Question>();
    optionAttributeList = new LinkedHashMap<Integer, OptionAttribute>();
    optionList = new LinkedHashMap<Integer, Option>();
    questionRelationList = new LinkedHashMap();
    matchList = new LinkedHashMap();
    int updateQRFromPosition = 24;
    int updateMatchFromPosition = 25;
    int updateQOFromPosition = 51;
    int QRRow = 0;
    int MatchRow = 0;
    int QORow = 0;
    for (String table : tables2updateQuestions) {
        Log.i(TAG, "Loading csv: " + table);
        CSVReader reader = new CSVReader(new InputStreamReader(context.openFileInput(table)), SEPARATOR, QUOTECHAR);
        String[] line;
        while ((line = reader.readNext()) != null) {
            boolean isNew = true;
            switch(table) {
                case OPTION_ATTRIBUTES_CSV:
                    OptionAttribute optionAttribute = new OptionAttribute();
                    optionAttribute.setBackground_colour(line[1]);
                    optionAttribute.setPath(line[2]);
                    if (line.length > 3 && !line[3].equals("")) {
                        optionAttribute.setHorizontal_alignment(Integer.valueOf(line[3]));
                    } else {
                        optionAttribute.setHorizontal_alignment(OptionAttribute.DEFAULT_HORIZONTAL_ALIGNMENT);
                    }
                    if (line.length > 4 && !line[4].equals("")) {
                        optionAttribute.setVertical_alignment(Integer.valueOf(line[4]));
                    } else {
                        optionAttribute.setHorizontal_alignment(OptionAttribute.DEFAULT_VERTICAL_ALIGNMENT);
                    }
                    if (line.length > 5 && !line[5].equals("")) {
                        optionAttribute.setText_size(Integer.valueOf(line[5]));
                    } else {
                        optionAttribute.setText_size(Integer.parseInt(PreferencesState.getInstance().getContext().getResources().getString(R.string.default_option_text_size)));
                    }
                    if (line.length > 6 && !line[6].equals("")) {
                        optionAttribute.setDefaultOption(Integer.valueOf(line[6]));
                    } else {
                        optionAttribute.setDefaultOption(0);
                    }
                    optionAttributeList.put(Integer.valueOf(line[0]), optionAttribute);
                    break;
                case OPTIONS_CSV:
                    // Ignore if the option already exists.
                    for (Option option : actualOptions) {
                        if (String.valueOf(option.getId_option()).equals(line[0])) {
                            isNew = false;
                        }
                    }
                    Option option;
                    if (isNew) {
                        option = new Option();
                        option.setName(line[1]);
                        option.setCode(line[2]);
                        option.setFactor(Float.valueOf(line[3]));
                        option.setAnswer(Answer.findById(Long.valueOf(line[4])));
                        if (line[5] != null && !line[5].isEmpty()) {
                            OptionAttribute localOptionAttribute = OptionAttribute.findById(Long.valueOf(line[5]));
                            if (localOptionAttribute == null) {
                                localOptionAttribute = optionAttributeList.get(Integer.valueOf(line[5]));
                                localOptionAttribute.save();
                            }
                            option.setOptionAttribute(localOptionAttribute);
                        }
                        option.save();
                    } else {
                        option = Option.findById(Long.valueOf(line[0]));
                    }
                    optionList.put(Integer.valueOf(line[0]), option);
                    break;
                case QUESTIONS_CSV:
                    // Ignore if the question already exists.
                    for (Question question : actualQuestions) {
                        if (String.valueOf(question.getId_question()).equals(line[0])) {
                            isNew = false;
                        }
                    }
                    Question question;
                    if (isNew) {
                        question = new Question();
                        question.setCode(line[1]);
                        question.setDe_name(line[2]);
                        question.setHelp_text(line[3]);
                        question.setForm_name(line[4]);
                        question.setUid(line[5]);
                        question.setOrder_pos(Integer.valueOf(line[6]));
                        question.setNumerator_w(Float.valueOf(line[7]));
                        question.setDenominator_w(Float.valueOf(line[8]));
                        question.setHeader(Header.findById(Long.valueOf(line[9])));
                        if (!line[10].equals("")) {
                            question.setAnswer(Answer.findById(Long.valueOf(line[10])));
                        }
                        if (!line[11].equals("")) {
                            question.setQuestion(questionList.get(Integer.valueOf(line[11])));
                        }
                        question.setOutput(Integer.valueOf(line[12]));
                        question.setTotalQuestions(Integer.valueOf(line[13]));
                        question.setVisible(Integer.valueOf(line[14]));
                        if (line.length > 15 && !line[15].equals("")) {
                            question.setPath((line[15]));
                        }
                        if (line.length > 16 && !line[16].equals("")) {
                            question.setCompulsory(Integer.valueOf(line[16]));
                        } else {
                            question.setCompulsory(Question.QUESTION_NOT_COMPULSORY);
                        }
                        question.save();
                    } else {
                        question = Question.findByUID(line[5]);
                    }
                    questionList.put(Integer.valueOf(line[0]), question);
                    break;
                case QUESTION_RELATIONS_CSV:
                    // Ignore if the option already exists.
                    QRRow++;
                    if (updateQRFromPosition > QRRow) {
                        break;
                    }
                    QuestionRelation questionRelation = new QuestionRelation();
                    questionRelation.setOperation(Integer.valueOf(line[1]));
                    questionRelation.setQuestion(questionList.get(Integer.valueOf(line[2])));
                    questionRelation.save();
                    questionRelationList.put(Integer.valueOf(line[0]), questionRelation);
                    break;
                case MATCHES:
                    // Ignore if the match already exists.
                    MatchRow++;
                    if (updateMatchFromPosition > MatchRow) {
                        break;
                    }
                    Match match = new Match();
                    match.setQuestionRelation(questionRelationList.get(Integer.valueOf(line[1])));
                    match.save();
                    matchList.put(Long.valueOf(line[0]), match);
                    break;
                case QUESTION_OPTIONS_CSV:
                    // Ignore if the question option already exists.
                    QORow++;
                    if (updateQOFromPosition > QORow) {
                        break;
                    }
                    QuestionOption questionOption = new QuestionOption();
                    questionOption.setQuestion(questionList.get(Integer.valueOf(line[1])));
                    questionOption.setOption(optionList.get(Integer.valueOf(line[2])));
                    if (!line[3].equals("")) {
                        questionOption.setMatch(matchList.get(Integer.valueOf(line[3])));
                    }
                    questionOption.save();
                    break;
            }
        }
    }
}
Also used : QuestionOption(org.eyeseetea.malariacare.data.database.model.QuestionOption) InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) QuestionRelation(org.eyeseetea.malariacare.data.database.model.QuestionRelation) OptionAttribute(org.eyeseetea.malariacare.data.database.model.OptionAttribute) LinkedHashMap(java.util.LinkedHashMap) Match(org.eyeseetea.malariacare.data.database.model.Match) TreatmentMatch(org.eyeseetea.malariacare.data.database.model.TreatmentMatch) QuestionOption(org.eyeseetea.malariacare.data.database.model.QuestionOption) Option(org.eyeseetea.malariacare.data.database.model.Option) Question(org.eyeseetea.malariacare.data.database.model.Question)

Example 4 with QuestionRelation

use of org.eyeseetea.malariacare.data.database.model.QuestionRelation in project pictureapp by EyeSeeTea.

the class NavigationController method visit.

/**
 * Annotates next question in screen
 */
private void visit(QuestionNode nextNode) {
    // Self references are neither moving the counter nor adding a new position
    if (isALoop(nextNode)) {
        Log.d(TAG, String.format("visit(%s) -> Not advancing", nextNode.getQuestion().getCode()));
        return;
    }
    // This node wont be shown in previous move (warnings)
    if (!nextNode.isVisibleInReview()) {
        // If the survey is sent we need hide the reminder question.
        if (Session.getMalariaSurvey() != null && !Session.getMalariaSurvey().isInProgress()) {
            for (QuestionRelation questionRelation : nextNode.getQuestion().getQuestionRelations()) {
                if (questionRelation.isAReminder()) {
                    // is a reminder -> next
                    nonVisibleNode = nextNode;
                    if (nextNode == null) {
                        return;
                    }
                    next(null);
                    return;
                }
            }
        }
        Log.d(TAG, String.format("visit(%s) -> In position %d", nextNode.getQuestion().getCode(), currentPosition));
        // Requires a rewind leaving its parent as last visited
        rewindVisited(nextNode);
        // Annotate 'floating' node
        nonVisibleNode = nextNode;
        return;
    }
    // annotate new current node and advance counter
    nonVisibleNode = null;
    currentPosition++;
    Log.d(TAG, String.format("visit(%s) -> In position %d", nextNode.getQuestion().getCode(), currentPosition));
    visited.add(nextNode);
}
Also used : QuestionRelation(org.eyeseetea.malariacare.data.database.model.QuestionRelation)

Example 5 with QuestionRelation

use of org.eyeseetea.malariacare.data.database.model.QuestionRelation in project pictureapp by EyeSeeTea.

the class WarningStatusChecker method initWarningTrigger.

private void initWarningTrigger(Question reminderQuestion) {
    // Look for a WARNING relation which origin questionOption + questionThreshold activates this
    for (QuestionRelation questionRelation : reminderQuestion.getQuestionRelations()) {
        if (!questionRelation.isAWarning()) {
            continue;
        }
        // Find QuestionOption for this relation
        QuestionOption questionOption = findQuestionOption(questionRelation);
        if (questionOption == null) {
            continue;
        }
        // Annotate questionOption and threshold to check
        this.questionOption = questionOption;
        this.questionThreshold = questionOption.getQuestionThreshold();
        return;
    }
}
Also used : QuestionOption(org.eyeseetea.malariacare.data.database.model.QuestionOption) QuestionRelation(org.eyeseetea.malariacare.data.database.model.QuestionRelation)

Aggregations

QuestionRelation (org.eyeseetea.malariacare.data.database.model.QuestionRelation)11 CSVReader (com.opencsv.CSVReader)5 InputStreamReader (java.io.InputStreamReader)5 QuestionOption (org.eyeseetea.malariacare.data.database.model.QuestionOption)5 Match (org.eyeseetea.malariacare.data.database.model.Match)4 TreatmentMatch (org.eyeseetea.malariacare.data.database.model.TreatmentMatch)4 Question (org.eyeseetea.malariacare.data.database.model.Question)3 ArrayList (java.util.ArrayList)2 Option (org.eyeseetea.malariacare.data.database.model.Option)2 OptionAttribute (org.eyeseetea.malariacare.data.database.model.OptionAttribute)2 PopulateRow.populateQuestionRelation (org.eyeseetea.malariacare.data.database.utils.populatedb.PopulateRow.populateQuestionRelation)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Answer (org.eyeseetea.malariacare.data.database.model.Answer)1 Drug (org.eyeseetea.malariacare.data.database.model.Drug)1 Header (org.eyeseetea.malariacare.data.database.model.Header)1 Partner (org.eyeseetea.malariacare.data.database.model.Partner)1 Program (org.eyeseetea.malariacare.data.database.model.Program)1