use of org.odk.collect.android.javarosawrapper.FormController in project collect by opendatakit.
the class FormEntryActivity method nonblockingCreateSavePointData.
/**
* Creates save-points asynchronously in order to not affect swiping performance on larger forms.
* If moving backwards through a form is disabled, also saves the index of the form element that
* was last shown to the user so that no matter how the app exits and relaunches, the user can't
* see previous questions.
*/
private void nonblockingCreateSavePointData() {
try {
SavePointTask savePointTask = new SavePointTask(this);
savePointTask.execute();
if (!allowMovingBackwards) {
FormController formController = getFormController();
if (formController != null) {
new SaveFormIndexTask(this, formController.getFormIndex()).execute();
}
}
} catch (Exception e) {
Timber.e("Could not schedule SavePointTask. Perhaps a lot of swiping is taking place?");
}
}
use of org.odk.collect.android.javarosawrapper.FormController in project collect by opendatakit.
the class FormEntryActivity method createLanguageDialog.
/**
* Creates and displays a dialog allowing the user to set the language for
* the form.
*/
private void createLanguageDialog() {
FormController formController = getFormController();
final String[] languages = formController.getLanguages();
int selected = -1;
if (languages != null) {
String language = formController.getLanguage();
for (int i = 0; i < languages.length; i++) {
if (language.equals(languages[i])) {
selected = i;
}
}
}
alertDialog = new MaterialAlertDialogBuilder(this).setSingleChoiceItems(languages, selected, (dialog, whichButton) -> {
Form form = formsRepository.getOneByPath(formPath);
if (form != null) {
formsRepository.save(new Form.Builder(form).language(languages[whichButton]).build());
}
getFormController().setLanguage(languages[whichButton]);
dialog.dismiss();
if (getFormController().currentPromptIsQuestion()) {
saveAnswersForCurrentScreen(DO_NOT_EVALUATE_CONSTRAINTS);
}
onScreenRefresh();
}).setTitle(getString(R.string.change_language)).setNegativeButton(getString(R.string.do_not_change), null).create();
alertDialog.show();
}
use of org.odk.collect.android.javarosawrapper.FormController in project collect by opendatakit.
the class InstancesDaoHelper method isInstanceComplete.
/**
* Checks the database to determine if the current instance being edited has
* already been 'marked completed'. A form can be 'unmarked' complete and
* then resaved.
*
* @return true if form has been marked completed, false otherwise.
* <p>
* TODO: replace with method in {@link InstancesRepository}
* that returns an {@link Instance} object from a path.
*/
public static boolean isInstanceComplete(boolean end, boolean completedByDefault) {
// default to false if we're mid form
boolean complete = false;
FormController formController = Collect.getInstance().getFormController();
if (formController != null && formController.getInstanceFile() != null) {
// First check if we're at the end of the form, then check the preferences
complete = end && completedByDefault;
// Then see if we've already marked this form as complete before
String path = formController.getInstanceFile().getAbsolutePath();
Instance instance = new InstancesRepositoryProvider(Collect.getInstance()).get().getOneByPath(path);
if (instance != null && instance.getStatus().equals(Instance.STATUS_COMPLETE)) {
complete = true;
}
} else {
Timber.w("FormController or its instanceFile field has a null value");
}
return complete;
}
use of org.odk.collect.android.javarosawrapper.FormController in project collect by opendatakit.
the class ItemsetDao method getItems.
public List<SelectChoice> getItems(FormEntryPrompt formEntryPrompt, XPathParseTool pathParseTool) throws FileNotFoundException, XPathSyntaxException {
String nodesetString = getNodesetString(formEntryPrompt);
List<String> arguments = new ArrayList<>();
String selectionString = getSelectionStringAndPopulateArguments(getQueryString(nodesetString), arguments);
FormController formController = Collect.getInstance().getFormController();
String[] selectionArgs = getSelectionArgs(arguments, nodesetString, formController, pathParseTool, formEntryPrompt);
return selectionArgs == null ? null : getItemsFromDatabase(selectionString, selectionArgs, formController, adapter);
}
use of org.odk.collect.android.javarosawrapper.FormController in project collect by opendatakit.
the class ExternalDataUtil method populateExternalChoices.
public static ArrayList<SelectChoice> populateExternalChoices(FormEntryPrompt formEntryPrompt, XPathFuncExpr xpathfuncexpr) throws FileNotFoundException {
try {
List<SelectChoice> selectChoices = formEntryPrompt.getSelectChoices();
ArrayList<SelectChoice> returnedChoices = new ArrayList<>();
for (SelectChoice selectChoice : selectChoices) {
String value = selectChoice.getValue();
if (isAnInteger(value)) {
// treat this as a static choice
returnedChoices.add(selectChoice);
} else {
String displayColumns = formEntryPrompt.getSelectChoiceText(selectChoice);
String imageColumn = formEntryPrompt.getSpecialFormSelectChoiceText(selectChoice, FormEntryCaption.TEXT_FORM_IMAGE);
if (imageColumn != null && imageColumn.startsWith(JR_IMAGES_PREFIX)) {
imageColumn = imageColumn.substring(JR_IMAGES_PREFIX.length());
}
// if (displayColumns == null || displayColumns.trim().length() == 0) {
// throw new InvalidSyntaxException("The label column in the choices sheet
// appears to be empty (or has been calculated as empty).");
// }
ExternalDataManager externalDataManager = Collect.getInstance().getExternalDataManager();
FormInstance formInstance = Collect.getInstance().getFormController().getFormDef().getInstance();
EvaluationContext baseEvaluationContext = new EvaluationContext(formInstance);
EvaluationContext evaluationContext = new EvaluationContext(baseEvaluationContext, formEntryPrompt.getIndex().getReference());
// we can only add only the appropriate by querying the xPathFuncExpr.id.name
evaluationContext.addFunctionHandler(new ExternalDataHandlerSearch(externalDataManager, displayColumns, value, imageColumn));
Object eval = xpathfuncexpr.eval(formInstance, evaluationContext);
if (eval.getClass().isAssignableFrom(ArrayList.class)) {
@SuppressWarnings("unchecked") List<SelectChoice> dynamicChoices = (ArrayList<SelectChoice>) eval;
for (SelectChoice dynamicChoice : dynamicChoices) {
returnedChoices.add(dynamicChoice);
}
} else {
throw new ExternalDataException(getLocalizedString(Collect.getInstance(), R.string.ext_search_return_error, eval.getClass().getName()));
}
}
}
return returnedChoices;
} catch (Exception e) {
String fileName = String.valueOf(xpathfuncexpr.args[0].eval(null, null));
if (!fileName.endsWith(".csv")) {
fileName = fileName + ".csv";
}
FormController formController = Collect.getInstance().getFormController();
String filePath = fileName;
if (formController != null) {
filePath = Collect.getInstance().getFormController().getMediaFolder() + File.separator + fileName;
}
if (!new File(filePath).exists()) {
throw new FileNotFoundException(filePath);
}
throw new ExternalDataException(e.getMessage(), e);
}
}
Aggregations