Search in sources :

Example 11 with OptionAttribute

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

the class UpdateDBStrategy method updateOptions.

public static void updateOptions(Context context) throws IOException {
    List<Option> optionToDelete = Question.getOptions(PreferencesState.getInstance().getContext().getString(R.string.residenceVillageUID));
    for (Option option : optionToDelete) {
        if (!option.getName().equals(PreferencesState.getInstance().getContext().getString(R.string.patientResidenceVillageOtherCode))) {
            option.delete();
        }
    }
    FileCsvs fileCsvs = new FileCsvs();
    fileCsvs.saveCsvFromAssetsToFile(PopulateDB.OPTIONS_CSV);
    List<Option> options = Option.getAllOptions();
    HashMap<Long, Answer> answersIds = RelationsIdCsvDB.getAnswerFKRelationCsvDB(context);
    HashMap<Long, OptionAttribute> optionAttributeIds = RelationsIdCsvDB.getOptionAttributeIdRelationCsvDB(context);
    CSVReader reader = new CSVReader(new InputStreamReader(context.openFileInput(PopulateDB.OPTIONS_CSV)), PopulateDB.SEPARATOR, PopulateDB.QUOTECHAR);
    String[] line;
    int i = 0;
    while ((line = reader.readNext()) != null) {
        if (i < options.size()) {
            PopulateRow.populateOption(line, answersIds, optionAttributeIds, options.get(i)).save();
        } else {
            PopulateRow.populateOption(line, answersIds, optionAttributeIds, null).insert();
        }
        i++;
    }
    List<OrgUnit> orgUnits = OrgUnit.getAllOrgUnit();
    for (OrgUnit orgUnit : orgUnits) {
        Option option = new Option();
        option.setName(orgUnit.getName());
        option.setCode(orgUnit.getUid());
        option.setFactor((float) 0);
        option.setId_option((long) 0);
        option.setAnswer(Question.getAnswer(PreferencesState.getInstance().getContext().getString(R.string.residenceVillageUID)));
        option.save();
    }
}
Also used : OrgUnit(org.eyeseetea.malariacare.data.database.model.OrgUnit) InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) OptionAttribute(org.eyeseetea.malariacare.data.database.model.OptionAttribute) Answer(org.eyeseetea.malariacare.data.database.model.Answer) FileCsvs(org.eyeseetea.malariacare.data.database.utils.populatedb.FileCsvs) Option(org.eyeseetea.malariacare.data.database.model.Option)

Aggregations

OptionAttribute (org.eyeseetea.malariacare.data.database.model.OptionAttribute)11 CSVReader (com.opencsv.CSVReader)10 InputStreamReader (java.io.InputStreamReader)10 Option (org.eyeseetea.malariacare.data.database.model.Option)8 QuestionOption (org.eyeseetea.malariacare.data.database.model.QuestionOption)7 Answer (org.eyeseetea.malariacare.data.database.model.Answer)6 OrgUnit (org.eyeseetea.malariacare.data.database.model.OrgUnit)3 Question (org.eyeseetea.malariacare.data.database.model.Question)3 ArrayList (java.util.ArrayList)2 Drug (org.eyeseetea.malariacare.data.database.model.Drug)2 Match (org.eyeseetea.malariacare.data.database.model.Match)2 QuestionRelation (org.eyeseetea.malariacare.data.database.model.QuestionRelation)2 TreatmentMatch (org.eyeseetea.malariacare.data.database.model.TreatmentMatch)2 PopulateDBStrategy (org.eyeseetea.malariacare.data.database.utils.PopulateDBStrategy)2 FileCsvs (org.eyeseetea.malariacare.data.database.utils.populatedb.FileCsvs)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Header (org.eyeseetea.malariacare.data.database.model.Header)1