Search in sources :

Example 56 with FormIndex

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

the class FormController method stepToPreviousEvent.

/**
 * Navigates backward in the form.
 *
 * @return the event that should be handled by a view.
 */
public int stepToPreviousEvent() {
    /*
         * Right now this will always skip to the beginning of a group if that group is represented
         * as a 'field-list'. Should a need ever arise to step backwards by only one step in a
         * 'field-list', this method will have to be updated.
         */
    formEntryController.stepToPreviousEvent();
    if (indexIsInFieldList() && getEvent() == FormEntryController.EVENT_QUESTION) {
        // caption[0..len-1]
        // caption[len-1] == the question itself
        // caption[len-2] == the first group it is contained in.
        FormEntryCaption[] captions = getCaptionHierarchy();
        FormEntryCaption grp = captions[captions.length - 2];
        int event = formEntryController.jumpToIndex(grp.getIndex());
        // and test if this group or at least one of its children is relevant...
        FormIndex idx = grp.getIndex();
        if (!formEntryController.getModel().isIndexRelevant(idx)) {
            return stepToPreviousEvent();
        }
        idx = formEntryController.getModel().incrementIndex(idx, true);
        while (FormIndex.isSubElement(grp.getIndex(), idx)) {
            if (formEntryController.getModel().isIndexRelevant(idx)) {
                return event;
            }
            idx = formEntryController.getModel().incrementIndex(idx, true);
        }
        return stepToPreviousEvent();
    } else if (indexIsInFieldList() && getEvent() == FormEntryController.EVENT_GROUP) {
        FormIndex grpidx = formEntryController.getModel().getFormIndex();
        int event = formEntryController.getModel().getEvent();
        // and test if this group or at least one of its children is relevant...
        if (!formEntryController.getModel().isIndexRelevant(grpidx)) {
            // shouldn't happen?
            return stepToPreviousEvent();
        }
        FormIndex idx = formEntryController.getModel().incrementIndex(grpidx, true);
        while (FormIndex.isSubElement(grpidx, idx)) {
            if (formEntryController.getModel().isIndexRelevant(idx)) {
                return event;
            }
            idx = formEntryController.getModel().incrementIndex(idx, true);
        }
        return stepToPreviousEvent();
    }
    return getEvent();
}
Also used : FormEntryCaption(org.javarosa.form.api.FormEntryCaption) FormIndex(org.javarosa.core.model.FormIndex)

Example 57 with FormIndex

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

the class FormController method isCurrentQuestionFirstInForm.

public boolean isCurrentQuestionFirstInForm() {
    boolean isFirstQuestion = true;
    FormIndex originalFormIndex = getFormIndex();
    try {
        isFirstQuestion = stepToPreviousScreenEvent() == FormEntryController.EVENT_BEGINNING_OF_FORM && stepToNextScreenEvent() != FormEntryController.EVENT_PROMPT_NEW_REPEAT;
    } catch (JavaRosaException e) {
        Timber.d(e);
    }
    jumpToIndex(originalFormIndex);
    return isFirstQuestion;
}
Also used : FormIndex(org.javarosa.core.model.FormIndex) JavaRosaException(org.odk.collect.android.exception.JavaRosaException)

Example 58 with FormIndex

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

the class FormController method isGroupRelevant.

/**
 * @return true if a group contains at least one relevant question, otherwise false
 */
public boolean isGroupRelevant() {
    GroupDef groupDef = (GroupDef) getCaptionPrompt().getFormElement();
    FormIndex currentChildIndex = formEntryController.getModel().incrementIndex(getFormIndex(), true);
    for (FormIndex index : getIndicesForGroup(groupDef, currentChildIndex, true)) {
        if (formEntryController.getModel().isIndexRelevant(index)) {
            return true;
        }
    }
    return false;
}
Also used : FormIndex(org.javarosa.core.model.FormIndex) GroupDef(org.javarosa.core.model.GroupDef)

Example 59 with FormIndex

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

the class FormIndexSavepointTest method saveAndReadFormIndexTest.

@Test
public void saveAndReadFormIndexTest() {
    String instanceName = "test.xml";
    // for loadFormIndexFromFile
    File instancePath = new File(new StoragePathProvider().getOdkDirPath(StorageSubdirectory.INSTANCES) + File.separator + instanceName);
    when(formController.getInstanceFile()).thenReturn(instancePath);
    Collect.getInstance().setFormController(formController);
    FormIndex originalFormIndex = FormIndex.createBeginningOfFormIndex();
    File indexFile = SaveFormToDisk.getFormIndexFile(instanceName);
    SaveFormIndexTask.exportFormIndexToFile(originalFormIndex, indexFile);
    FormIndex readFormIndex = SaveFormIndexTask.loadFormIndexFromFile();
    assertEquals(originalFormIndex, readFormIndex);
    assertNotNull(readFormIndex);
    assertEquals(originalFormIndex.getReference(), readFormIndex.getReference());
}
Also used : StoragePathProvider(org.odk.collect.android.storage.StoragePathProvider) FormIndex(org.javarosa.core.model.FormIndex) File(java.io.File) Test(org.junit.Test)

Example 60 with FormIndex

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

the class RecordingHandler method handleForegroundRecording.

private void handleForegroundRecording(FormController formController, RecordingSession session, Result<File> result) throws JavaRosaException {
    FormIndex formIndex = (FormIndex) session.getId();
    questionMediaManager.replaceAnswerFile(formIndex.toString(), result.getOrNull().getAbsolutePath());
    formController.answerQuestion(formIndex, new StringData(result.getOrNull().getName()));
    session.getFile().delete();
}
Also used : FormIndex(org.javarosa.core.model.FormIndex) StringData(org.javarosa.core.model.data.StringData)

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