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()));
}
}
}
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();
}
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();
}
}
Aggregations