Search in sources :

Example 1 with ItemsetDbAdapter

use of org.odk.collect.android.fastexternalitemset.ItemsetDbAdapter in project collect by opendatakit.

the class FormLoaderTask method processItemSets.

private void processItemSets(File formMediaDir) {
    // for itemsets.csv, we only check to see if the itemset file has been
    // updated
    final File csv = new File(formMediaDir.getAbsolutePath() + "/" + ITEMSETS_CSV);
    String csvmd5;
    if (csv.exists()) {
        csvmd5 = Md5.getMd5Hash(csv);
        boolean readFile = false;
        final ItemsetDbAdapter ida = new ItemsetDbAdapter();
        ida.open();
        // get the database entry (if exists) for this itemsets.csv, based
        // on the path
        final Cursor c = ida.getItemsets(csv.getAbsolutePath());
        if (c != null) {
            if (c.getCount() == 1) {
                // should be only one, ever, if any
                c.moveToFirst();
                final String oldmd5 = c.getString(c.getColumnIndex("hash"));
                if (oldmd5.equals(csvmd5)) {
                // they're equal, do nothing
                } else {
                    // the csv has been updated, delete the old entries
                    ida.dropTable(ItemsetDbAdapter.getMd5FromString(csv.getAbsolutePath()), csv.getAbsolutePath());
                    // and read the new
                    readFile = true;
                }
            } else {
                // new csv, add it
                readFile = true;
            }
            c.close();
        }
        ida.close();
        if (readFile) {
            readCSV(csv, csvmd5, ItemsetDbAdapter.getMd5FromString(csv.getAbsolutePath()));
        }
    }
}
Also used : ItemsetDbAdapter(org.odk.collect.android.fastexternalitemset.ItemsetDbAdapter) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) Cursor(android.database.Cursor) File(java.io.File)

Example 2 with ItemsetDbAdapter

use of org.odk.collect.android.fastexternalitemset.ItemsetDbAdapter in project collect by opendatakit.

the class FormEntryPromptUtils method getAnswerText.

public static String getAnswerText(FormEntryPrompt fep, Context context, FormController formController) {
    IAnswerData data = fep.getAnswerValue();
    final String appearance = fep.getQuestion().getAppearanceAttr();
    if (data instanceof MultipleItemsData) {
        StringBuilder answerText = new StringBuilder();
        List<Selection> values = (List<Selection>) data.getValue();
        for (Selection value : values) {
            if (fep.getControlType() == Constants.CONTROL_RANK) {
                answerText.append(values.indexOf(value) + 1).append(". ");
            }
            answerText.append(fep.getSelectItemText(value));
            if ((values.size() - 1) > values.indexOf(value)) {
                answerText.append(", ");
            }
        }
        return answerText.toString();
    }
    if (data instanceof DateTimeData) {
        return DateTimeWidgetUtils.getDateTimeLabel((Date) data.getValue(), DateTimeWidgetUtils.getDatePickerDetails(appearance), true, context);
    }
    if (data instanceof DateData) {
        return DateTimeWidgetUtils.getDateTimeLabel((Date) data.getValue(), DateTimeWidgetUtils.getDatePickerDetails(appearance), false, context);
    }
    if (data != null && appearance != null && appearance.contains(Appearances.THOUSANDS_SEP)) {
        try {
            final BigDecimal answerAsDecimal = new BigDecimal(fep.getAnswerText());
            DecimalFormat df = new DecimalFormat();
            df.setGroupingSize(3);
            df.setGroupingUsed(true);
            df.setMaximumFractionDigits(Integer.MAX_VALUE);
            // Use . as decimal marker for consistency with DecimalWidget
            DecimalFormatSymbols customFormat = new DecimalFormatSymbols();
            customFormat.setDecimalSeparator('.');
            if (df.getDecimalFormatSymbols().getGroupingSeparator() == '.') {
                customFormat.setGroupingSeparator(' ');
            }
            df.setDecimalFormatSymbols(customFormat);
            return df.format(answerAsDecimal);
        } catch (NumberFormatException e) {
            return fep.getAnswerText();
        }
    }
    if (data != null && data.getValue() != null && fep.getDataType() == DATATYPE_TEXT && fep.getQuestion().getAdditionalAttribute(null, "query") != null) {
        // ItemsetWidget
        String language = "";
        if (formController.getLanguages() != null && formController.getLanguages().length > 0) {
            language = formController.getLanguage();
        }
        return new ItemsetDao(new ItemsetDbAdapter()).getItemLabel(fep.getAnswerValue().getDisplayText(), formController.getMediaFolder().getAbsolutePath(), language);
    }
    return fep.getAnswerText();
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) ItemsetDbAdapter(org.odk.collect.android.fastexternalitemset.ItemsetDbAdapter) DecimalFormatSymbols(java.text.DecimalFormatSymbols) Selection(org.javarosa.core.model.data.helper.Selection) DecimalFormat(java.text.DecimalFormat) MultipleItemsData(org.javarosa.core.model.data.MultipleItemsData) BigDecimal(java.math.BigDecimal) ItemsetDao(org.odk.collect.android.fastexternalitemset.ItemsetDao) DateData(org.javarosa.core.model.data.DateData) List(java.util.List) DateTimeData(org.javarosa.core.model.data.DateTimeData)

Example 3 with ItemsetDbAdapter

use of org.odk.collect.android.fastexternalitemset.ItemsetDbAdapter in project collect by opendatakit.

the class FormLoaderTask method readCSV.

private void readCSV(File csv, String formHash, String pathHash) {
    CSVReader reader;
    ItemsetDbAdapter ida = new ItemsetDbAdapter();
    ida.open();
    boolean withinTransaction = false;
    try {
        reader = new CSVReader(new FileReader(csv));
        String[] nextLine;
        String[] columnHeaders = null;
        int lineNumber = 0;
        while ((nextLine = reader.readNext()) != null) {
            lineNumber++;
            if (lineNumber == 1) {
                // first line of csv is column headers
                columnHeaders = nextLine;
                ida.createTable(formHash, pathHash, columnHeaders, csv.getAbsolutePath());
                continue;
            }
            // System.out.println(nextLine[4] + "etc...");
            if (lineNumber == 2) {
                // start a transaction for the inserts
                withinTransaction = true;
                ida.beginTransaction();
            }
            ida.addRow(pathHash, columnHeaders, nextLine);
        }
    } catch (IOException | SQLException | CsvValidationException e) {
        warningMsg = e.getMessage();
    } finally {
        if (withinTransaction) {
            ida.commit();
        }
        ida.close();
    }
}
Also used : ItemsetDbAdapter(org.odk.collect.android.fastexternalitemset.ItemsetDbAdapter) CsvValidationException(com.opencsv.exceptions.CsvValidationException) CSVReader(com.opencsv.CSVReader) SQLException(android.database.SQLException) FileReader(java.io.FileReader) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) IOException(java.io.IOException)

Aggregations

ItemsetDbAdapter (org.odk.collect.android.fastexternalitemset.ItemsetDbAdapter)3 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)2 Cursor (android.database.Cursor)1 SQLException (android.database.SQLException)1 CSVReader (com.opencsv.CSVReader)1 CsvValidationException (com.opencsv.exceptions.CsvValidationException)1 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 List (java.util.List)1 DateData (org.javarosa.core.model.data.DateData)1 DateTimeData (org.javarosa.core.model.data.DateTimeData)1 IAnswerData (org.javarosa.core.model.data.IAnswerData)1 MultipleItemsData (org.javarosa.core.model.data.MultipleItemsData)1 Selection (org.javarosa.core.model.data.helper.Selection)1 ItemsetDao (org.odk.collect.android.fastexternalitemset.ItemsetDao)1