use of org.eyeseetea.malariacare.data.database.model.Answer 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 = new CSVReader(new InputStreamReader(context.openFileInput(table)), SEPARATOR, QUOTECHAR);
String[] line;
while ((line = reader.readNext()) != null) {
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.setCode(line[1]);
option.setName(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 ORGANISATIONS_CSV:
Organisation organisation = PopulateRow.populateOrganisations(line, null);
organisation.insert();
organisationList.put(Long.parseLong(line[0]), organisation);
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();
}
use of org.eyeseetea.malariacare.data.database.model.Answer in project pictureapp by EyeSeeTea.
the class UpdateDB 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.getCode().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.setCode(orgUnit.getName());
option.setName(orgUnit.getUid());
option.setFactor((float) 0);
option.setId_option((long) 0);
option.setAnswer(Question.getAnswer(PreferencesState.getInstance().getContext().getString(R.string.residenceVillageUID)));
option.save();
}
}
use of org.eyeseetea.malariacare.data.database.model.Answer in project pictureapp by EyeSeeTea.
the class DataValueExtended method findOptionByQuestion.
public Option findOptionByQuestion(Question question) {
if (question == null) {
return null;
}
Answer answer = question.getAnswer();
if (answer == null) {
return null;
}
List<Option> options = answer.getOptions();
List<String> optionCodes = new ArrayList<>();
for (Option option : options) {
String optionName = option.getName();
optionCodes.add(optionName);
if (optionName == null) {
continue;
}
if (optionName.equals(dataValue.getValue())) {
return option;
}
}
//Log.w(TAG,String.format("Cannot find option '%s'",dataValue.getValue()));
return null;
}
use of org.eyeseetea.malariacare.data.database.model.Answer in project pictureapp by EyeSeeTea.
the class UpdateDB method updateAnswers.
/**
* Method to update the old answers and add new ones.
*
* @param context Needed to open the csv with the answers.
* @throws IOException If there is a problem opening the csv.
*/
public static void updateAnswers(Context context) throws IOException {
FileCsvs fileCsvs = new FileCsvs();
fileCsvs.saveCsvFromAssetsToFile(PopulateDB.ANSWERS_CSV);
List<Answer> answers = Answer.getAllAnswers();
CSVReader reader = new CSVReader(new InputStreamReader(context.openFileInput(PopulateDB.ANSWERS_CSV)), PopulateDB.SEPARATOR, PopulateDB.QUOTECHAR);
String[] line;
//Save new answers
int i = 0;
while ((line = reader.readNext()) != null) {
if (i < answers.size()) {
PopulateRow.populateAnswer(line, answers.get(i)).save();
} else {
PopulateRow.populateAnswer(line, null).insert();
}
i++;
}
}
use of org.eyeseetea.malariacare.data.database.model.Answer in project pictureapp by EyeSeeTea.
the class UpdateDB method insertLastLines.
public static void insertLastLines(int numeberLines, Context context) throws IOException {
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);
List<String[]> lines = reader.readAll();
for (int i = lines.size() - numeberLines - 1; i < lines.size(); i++) {
PopulateRow.populateOption(lines.get(i), answersIds, optionAttributeIds, null).insert();
}
}
Aggregations