Search in sources :

Example 31 with FormIndex

use of org.javarosa.core.model.FormIndex in project collect by opendatakit.

the class AsyncTaskAuditEventWriterTest method getTestFormIndex.

/**
 * Given an XPath path, generate a corresponding {@link TreeReference} and a fake
 * {@link FormIndex} that doesn't correspond to any real form definition. The only thing we care
 * about for the {@link FormIndex} are the instance indexes at every level. Everything else can
 * be faked.
 * <p>
 * TODO: once {@link AuditEvent}'s getXPathPath moves to FormIndex, just use a mock
 */
private FormIndex getTestFormIndex(String xpathPath) {
    String[] nodes = xpathPath.split("/");
    TreeReference treeReference = new TreeReference();
    // take care of leading /
    nodes = Arrays.copyOfRange(nodes, 1, nodes.length);
    ArrayList<Integer> positions = new ArrayList<>();
    for (String node : nodes) {
        String[] parts = node.split("\\[");
        String nodeName = parts[0];
        int position = 0;
        if (parts.length > 1) {
            position = Integer.parseInt(parts[1].replace("]", "")) - 1;
            positions.add(position);
        } else {
            positions.add(-1);
        }
        treeReference.add(nodeName, position);
    }
    FormIndex formIndex = null;
    for (int i = nodes.length - 1; i > 0; i--) {
        // exclude the root node
        formIndex = new FormIndex(formIndex, -1, positions.get(i), treeReference);
    }
    return formIndex;
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) ArrayList(java.util.ArrayList) FormIndex(org.javarosa.core.model.FormIndex)

Example 32 with FormIndex

use of org.javarosa.core.model.FormIndex in project collect by opendatakit.

the class AuditEventCSVLineTest method getTestFormIndex.

private FormIndex getTestFormIndex() {
    TreeReference treeReference = new TreeReference();
    treeReference.add("data", 0);
    treeReference.add("text1", 0);
    return new FormIndex(0, treeReference);
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) FormIndex(org.javarosa.core.model.FormIndex)

Example 33 with FormIndex

use of org.javarosa.core.model.FormIndex in project collect by opendatakit.

the class BearingWidgetTest method clickingBearingButton_whenSensorIsAvailable_setsWidgetWaitingForData.

@Test
public void clickingBearingButton_whenSensorIsAvailable_setsWidgetWaitingForData() {
    FormEntryPrompt prompt = promptWithAnswer(null);
    FormIndex formIndex = mock(FormIndex.class);
    when(prompt.getIndex()).thenReturn(formIndex);
    BearingWidget widget = createWidget(prompt);
    widget.binding.bearingButton.performClick();
    assertThat(fakeWaitingForDataRegistry.waiting.contains(formIndex), is(true));
}
Also used : FormEntryPrompt(org.javarosa.form.api.FormEntryPrompt) FormIndex(org.javarosa.core.model.FormIndex) Test(org.junit.Test)

Example 34 with FormIndex

use of org.javarosa.core.model.FormIndex in project collect by opendatakit.

the class OSMWidgetTest method clickingButton_whenActivityIsAvailable_setsWidgetWaitingForData.

@Test
public void clickingButton_whenActivityIsAvailable_setsWidgetWaitingForData() {
    FormIndex formIndex = mock(FormIndex.class);
    FormEntryPrompt prompt = promptWithAnswer(null);
    when(prompt.getIndex()).thenReturn(formIndex);
    OSMWidget widget = createWidget(prompt);
    widget.binding.launchOpenMapKitButton.performClick();
    assertThat(fakeWaitingForDataRegistry.waiting.contains(formIndex), is(true));
}
Also used : FormEntryPrompt(org.javarosa.form.api.FormEntryPrompt) FormIndex(org.javarosa.core.model.FormIndex) Test(org.junit.Test)

Example 35 with FormIndex

use of org.javarosa.core.model.FormIndex in project collect by opendatakit.

the class FormEntryActivity method loadingComplete.

/**
 * loadingComplete() is called by FormLoaderTask once it has finished
 * loading a form.
 */
@Override
public void loadingComplete(FormLoaderTask task, FormDef formDef) {
    dismissDialog(PROGRESS_DIALOG);
    final FormController formController = task.getFormController();
    // these are bogus if
    int requestCode = task.getRequestCode();
    // pendingActivityResult is
    // false
    int resultCode = task.getResultCode();
    Intent intent = task.getIntent();
    formLoaderTask.setFormLoaderListener(null);
    FormLoaderTask t = formLoaderTask;
    formLoaderTask = null;
    t.cancel(true);
    t.destroy();
    Collect.getInstance().setFormController(formController);
    supportInvalidateOptionsMenu();
    Collect.getInstance().setExternalDataManager(task.getExternalDataManager());
    // Set the language if one has already been set in the past
    String[] languageTest = formController.getLanguages();
    if (languageTest != null) {
        String defaultLanguage = formController.getLanguage();
        String newLanguage = FormsDaoHelper.getFormLanguage(formPath);
        long start = System.currentTimeMillis();
        Timber.i("calling formController.setLanguage");
        try {
            formController.setLanguage(newLanguage);
        } catch (Exception e) {
            // if somehow we end up with a bad language, set it to the default
            Timber.e("Ended up with a bad language. %s", newLanguage);
            formController.setLanguage(defaultLanguage);
        }
        Timber.i("Done in %.3f seconds.", (System.currentTimeMillis() - start) / 1000F);
    }
    boolean pendingActivityResult = task.hasPendingActivityResult();
    if (pendingActivityResult) {
        // set the current view to whatever group we were at...
        refreshCurrentView();
        // process the pending activity request...
        onActivityResult(requestCode, resultCode, intent);
        return;
    }
    // it can be a normal flow for a pending activity result to restore from
    // a savepoint
    // (the call flow handled by the above if statement). For all other use
    // cases, the
    // user should be notified, as it means they wandered off doing other
    // things then
    // returned to ODK Collect and chose Edit Saved Form, but that the
    // savepoint for that
    // form is newer than the last saved version of their form data.
    boolean hasUsedSavepoint = task.hasUsedSavepoint();
    if (hasUsedSavepoint) {
        runOnUiThread(() -> ToastUtils.showLongToast(R.string.savepoint_used));
    }
    // Set saved answer path
    if (formController.getInstanceFile() == null) {
        // Create new answer folder.
        String time = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.ENGLISH).format(Calendar.getInstance().getTime());
        String file = formPath.substring(formPath.lastIndexOf('/') + 1, formPath.lastIndexOf('.'));
        String path = Collect.INSTANCES_PATH + File.separator + file + "_" + time;
        if (FileUtils.createFolder(path)) {
            File instanceFile = new File(path + File.separator + file + "_" + time + ".xml");
            formController.setInstanceFile(instanceFile);
        }
        formController.getTimerLogger().logTimerEvent(TimerLogger.EventTypes.FORM_START, 0, null, false, true);
    } else {
        Intent reqIntent = getIntent();
        boolean showFirst = reqIntent.getBooleanExtra("start", false);
        formController.getTimerLogger().logTimerEvent(TimerLogger.EventTypes.FORM_RESUME, 0, null, false, true);
        if (!showFirst) {
            if (!allowMovingBackwards) {
                FormIndex formIndex = SaveFormIndexTask.loadFormIndexFromFile();
                if (formIndex != null) {
                    formController.jumpToIndex(formIndex);
                    refreshCurrentView();
                    return;
                }
            }
            String formMode = reqIntent.getStringExtra(ApplicationConstants.BundleKeys.FORM_MODE);
            if (formMode == null || ApplicationConstants.FormModes.EDIT_SAVED.equalsIgnoreCase(formMode)) {
                startActivity(new Intent(this, EditFormHierarchyActivity.class));
                // so we don't show the intro screen before jumping to the hierarchy
                return;
            } else {
                if (ApplicationConstants.FormModes.VIEW_SENT.equalsIgnoreCase(formMode)) {
                    startActivity(new Intent(this, ViewFormHierarchyActivity.class));
                }
                finish();
            }
        }
    }
    refreshCurrentView();
    if (formDef != null) {
        final File cachedFormDefFile = FormDefCache.getCacheFile(new File(formPath));
        if (cachedFormDefFile.exists()) {
            Timber.i("FormDef %s is already in the cache", cachedFormDefFile.toString());
        } else {
            Disposable formDefCacheDisposable = writeCacheAsync(formDef, cachedFormDefFile).subscribe(() -> {
            }, Timber::e);
            formDefCacheCompositeDisposable.add(formDefCacheDisposable);
        }
    }
}
Also used : FormController(org.odk.collect.android.logic.FormController) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Timber(timber.log.Timber) Intent(android.content.Intent) FailedConstraint(org.odk.collect.android.logic.FormController.FailedConstraint) GDriveConnectionException(org.odk.collect.android.exception.GDriveConnectionException) JavaRosaException(org.odk.collect.android.exception.JavaRosaException) FormLoaderTask(org.odk.collect.android.tasks.FormLoaderTask) FormIndex(org.javarosa.core.model.FormIndex) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File)

Aggregations

FormIndex (org.javarosa.core.model.FormIndex)63 GroupDef (org.javarosa.core.model.GroupDef)11 JavaRosaException (org.odk.collect.android.exception.JavaRosaException)11 FormController (org.odk.collect.android.javarosawrapper.FormController)11 ArrayList (java.util.ArrayList)10 FormEntryPrompt (org.javarosa.form.api.FormEntryPrompt)10 Test (org.junit.Test)10 IFormElement (org.javarosa.core.model.IFormElement)6 TreeReference (org.javarosa.core.model.instance.TreeReference)6 FormEntryCaption (org.javarosa.form.api.FormEntryCaption)6 File (java.io.File)5 FormController (org.odk.collect.android.logic.FormController)5 HierarchyElement (org.odk.collect.android.logic.HierarchyElement)5 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 HierarchyListAdapter (org.odk.collect.android.adapters.HierarchyListAdapter)4 FormDef (org.javarosa.core.model.FormDef)3 FormEntryModel (org.javarosa.form.api.FormEntryModel)3 Intent (android.content.Intent)2 ObjectInputStream (java.io.ObjectInputStream)2 HashMap (java.util.HashMap)2