Search in sources :

Example 1 with FileCsvs

use of org.eyeseetea.malariacare.data.database.utils.populatedb.FileCsvs in project pictureapp by EyeSeeTea.

the class Migration4RenameOrganisationToPartner method migrate.

@Override
public void migrate(DatabaseWrapper database) {
    String sqlCopy = "INSERT INTO Partner (id_partner, uid_partner, name)" + " SELECT id_organisation, uid_organisation, name" + " FROM Organisation;";
    database.execSQL(sqlCopy);
    String sqlDelete = "DROP TABLE IF EXISTS Organisation;";
    database.execSQL(sqlDelete);
    String sqlUserTemp = "CREATE TABLE UserTemp(id_user INTEGER PRIMARY KEY, uid_user TEXT, name TEXT, " + "partner_fk INTEGER, supervisor_fk INTEGER)";
    database.execSQL(sqlUserTemp);
    String sqlCopyUser = "INSERT INTO UserTemp (id_user, uid_user, name, partner_fk, supervisor_fk)" + " SELECT id_user, uid_user, name ,organisation_fk ,supervisor_fk" + " FROM User;";
    database.execSQL(sqlCopyUser);
    String sqlDeleteUser = "DROP TABLE IF EXISTS User;";
    database.execSQL(sqlDeleteUser);
    String renameUser = "ALTER TABLE UserTemp RENAME TO User";
    database.execSQL(renameUser);
    String sqlTreatmentTemp = "CREATE TABLE TreatmentTemp(id_treatment INTEGER PRIMARY KEY, " + "id_partner_fk INTEGER, diagnosis INTEGER, message INTEGER, type " + "INTEGER)";
    database.execSQL(sqlTreatmentTemp);
    String sqlCopyTreatment = "INSERT INTO TreatmentTemp (id_treatment, id_partner_fk, diagnosis, message, type)" + " SELECT id_treatment, id_organisation_fk, diagnosis, message, type" + " FROM Treatment;";
    database.execSQL(sqlCopyTreatment);
    String sqlDeleteTreatment = "DROP TABLE IF EXISTS Treatment;";
    database.execSQL(sqlDeleteTreatment);
    String renameTreatment = "ALTER TABLE TreatmentTemp RENAME TO Treatment";
    database.execSQL(renameTreatment);
    FileCsvs fileCsvs = new FileCsvs();
    try {
        fileCsvs.copyCsvFile("Organisations.csv", PopulateDB.PARTNER_CSV);
    } catch (IOException e) {
        new MigrateMigrationException(e);
    }
}
Also used : FileCsvs(org.eyeseetea.malariacare.data.database.utils.populatedb.FileCsvs) IOException(java.io.IOException) MigrateMigrationException(org.eyeseetea.malariacare.domain.exception.MigrateMigrationException)

Example 2 with FileCsvs

use of org.eyeseetea.malariacare.data.database.utils.populatedb.FileCsvs in project pictureapp by EyeSeeTea.

the class PopulateDBStrategy 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();
    }
}
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) QuestionOption(org.eyeseetea.malariacare.data.database.model.QuestionOption) Option(org.eyeseetea.malariacare.data.database.model.Option)

Example 3 with FileCsvs

use of org.eyeseetea.malariacare.data.database.utils.populatedb.FileCsvs in project pictureapp by EyeSeeTea.

the class PopulateDBStrategy method init.

@Override
public void init() throws IOException {
    FileCsvs fileCsvs = new FileCsvs();
    fileCsvs.saveCsvsInFileIfNeeded();
    TreatmentTableOperations treatmentTable = new TreatmentTableOperations();
    treatmentTable.generateTreatmentMatrixIFNeeded();
}
Also used : FileCsvs(org.eyeseetea.malariacare.data.database.utils.populatedb.FileCsvs) TreatmentTableOperations(org.eyeseetea.malariacare.data.database.utils.populatedb.TreatmentTableOperations)

Example 4 with FileCsvs

use of org.eyeseetea.malariacare.data.database.utils.populatedb.FileCsvs 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

FileCsvs (org.eyeseetea.malariacare.data.database.utils.populatedb.FileCsvs)4 CSVReader (com.opencsv.CSVReader)2 InputStreamReader (java.io.InputStreamReader)2 Answer (org.eyeseetea.malariacare.data.database.model.Answer)2 Option (org.eyeseetea.malariacare.data.database.model.Option)2 OptionAttribute (org.eyeseetea.malariacare.data.database.model.OptionAttribute)2 OrgUnit (org.eyeseetea.malariacare.data.database.model.OrgUnit)2 IOException (java.io.IOException)1 QuestionOption (org.eyeseetea.malariacare.data.database.model.QuestionOption)1 TreatmentTableOperations (org.eyeseetea.malariacare.data.database.utils.populatedb.TreatmentTableOperations)1 MigrateMigrationException (org.eyeseetea.malariacare.domain.exception.MigrateMigrationException)1