Search in sources :

Example 46 with FormController

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?");
    }
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) SaveFormIndexTask(org.odk.collect.android.tasks.SaveFormIndexTask) SavePointTask(org.odk.collect.android.tasks.SavePointTask) RepeatsInFieldListException(org.odk.collect.android.javarosawrapper.RepeatsInFieldListException) JavaRosaException(org.odk.collect.android.exception.JavaRosaException)

Example 47 with FormController

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();
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) Form(org.odk.collect.forms.Form) MaterialAlertDialogBuilder(com.google.android.material.dialog.MaterialAlertDialogBuilder) FailedConstraint(org.odk.collect.android.javarosawrapper.FormController.FailedConstraint)

Example 48 with FormController

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;
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) InstancesRepositoryProvider(org.odk.collect.android.utilities.InstancesRepositoryProvider) Instance(org.odk.collect.forms.instances.Instance)

Example 49 with FormController

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);
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) ArrayList(java.util.ArrayList)

Example 50 with FormController

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);
    }
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) SelectChoice(org.javarosa.core.model.SelectChoice) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) ExternalDataException(org.odk.collect.android.exception.ExternalDataException) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) ExternalDataHandlerSearch(org.odk.collect.android.externaldata.handler.ExternalDataHandlerSearch) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) FileNotFoundException(java.io.FileNotFoundException) ExternalDataException(org.odk.collect.android.exception.ExternalDataException) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) FormInstance(org.javarosa.core.model.instance.FormInstance) File(java.io.File)

Aggregations

FormController (org.odk.collect.android.javarosawrapper.FormController)53 FormIndex (org.javarosa.core.model.FormIndex)11 File (java.io.File)9 JavaRosaException (org.odk.collect.android.exception.JavaRosaException)9 FormDef (org.javarosa.core.model.FormDef)4 FormEntryPrompt (org.javarosa.form.api.FormEntryPrompt)4 Collect (org.odk.collect.android.application.Collect)4 RepeatsInFieldListException (org.odk.collect.android.javarosawrapper.RepeatsInFieldListException)4 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)4 TextView (android.widget.TextView)3 ArrayList (java.util.ArrayList)3 FormInstance (org.javarosa.core.model.instance.FormInstance)3 TreeReference (org.javarosa.core.model.instance.TreeReference)3 FormEntryCaption (org.javarosa.form.api.FormEntryCaption)3 Before (org.junit.Before)3 Test (org.junit.Test)3 Form (org.odk.collect.forms.Form)3 Intent (android.content.Intent)2 View (android.view.View)2 WebView (android.webkit.WebView)2