Search in sources :

Example 21 with IFormElement

use of org.javarosa.core.model.IFormElement in project javarosa by opendatakit.

the class FormEntryModel method getCaptionHierarchy.

/**
 * Returns a hierarchical list of FormEntryCaption objects for the given
 * FormIndex
 *
 * @param index
 * @return list of FormEntryCaptions in hierarchical order
 */
public FormEntryCaption[] getCaptionHierarchy(FormIndex index) {
    List<FormEntryCaption> captions = new ArrayList<FormEntryCaption>();
    FormIndex remaining = index;
    while (remaining != null) {
        remaining = remaining.getNextLevel();
        FormIndex localIndex = index.diff(remaining);
        IFormElement element = form.getChild(localIndex);
        if (element != null) {
            FormEntryCaption caption = null;
            if (element instanceof GroupDef)
                caption = new FormEntryCaption(getForm(), localIndex);
            else if (element instanceof QuestionDef)
                caption = new FormEntryPrompt(getForm(), localIndex);
            if (caption != null) {
                captions.add(caption);
            }
        }
    }
    FormEntryCaption[] captionArray = new FormEntryCaption[captions.size()];
    return captions.toArray(captionArray);
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) ArrayList(java.util.ArrayList) FormIndex(org.javarosa.core.model.FormIndex) QuestionDef(org.javarosa.core.model.QuestionDef) GroupDef(org.javarosa.core.model.GroupDef)

Example 22 with IFormElement

use of org.javarosa.core.model.IFormElement in project javarosa by opendatakit.

the class FormEntryModel method createModelIfNecessary.

/**
 * For the current index: Checks whether the index represents a node which
 * should exist given a non-interactive repeat, along with a count for that
 * repeat which is beneath the dynamic level specified.
 *
 * If this index does represent such a node, the new model for the repeat is
 * created behind the scenes and the index for the initial question is
 * returned.
 *
 * Note: This method will not prevent the addition of new repeat elements in
 * the interface, it will merely use the xforms repeat hint to create new
 * nodes that are assumed to exist
 *
 * @param index The index to be evaluated as to whether the underlying model is
 *        hinted to exist
 */
private void createModelIfNecessary(FormIndex index) {
    if (index.isInForm()) {
        IFormElement e = getForm().getChild(index);
        if (e instanceof GroupDef) {
            GroupDef g = (GroupDef) e;
            if (g.getRepeat() && g.getCountReference() != null) {
                // Lu Gram: repeat count XPath needs to be contextualized for nested repeat groups
                TreeReference countRef = FormInstance.unpackReference(g.getCountReference());
                TreeReference contextualized = countRef.contextualize(index.getReference());
                IAnswerData count = getForm().getMainInstance().resolveReference(contextualized).getValue();
                if (count != null) {
                    long fullcount = ((Integer) count.getValue()).intValue();
                    TreeReference ref = getForm().getChildInstanceRef(index);
                    TreeElement element = getForm().getMainInstance().resolveReference(ref);
                    if (element == null) {
                        if (index.getTerminal().getInstanceIndex() < fullcount) {
                            try {
                                getForm().createNewRepeat(index);
                            } catch (InvalidReferenceException ire) {
                                Std.printStack(ire);
                                throw new RuntimeException("Invalid Reference while creting new repeat!" + ire.getMessage());
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) IFormElement(org.javarosa.core.model.IFormElement) TreeReference(org.javarosa.core.model.instance.TreeReference) GroupDef(org.javarosa.core.model.GroupDef) InvalidReferenceException(org.javarosa.core.model.instance.InvalidReferenceException) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 23 with IFormElement

use of org.javarosa.core.model.IFormElement in project javarosa by opendatakit.

the class FormEntryModel method setRepeatNextMultiplicity.

private boolean setRepeatNextMultiplicity(List<IFormElement> elements, List<Integer> multiplicities) {
    // find out if node is repeatable
    TreeReference nodeRef = form.getChildInstanceRef(elements, multiplicities);
    TreeElement node = form.getMainInstance().resolveReference(nodeRef);
    if (node == null || node.isRepeatable()) {
        // node == null if there are no instances of the repeat
        IFormElement lastElement = elements.get(elements.size() - 1);
        if (lastElement instanceof GroupDef && !((GroupDef) lastElement).getRepeat()) {
            // it's a regular group inside a repeatable group
            return false;
        }
        int mult;
        if (node == null) {
            // no repeats; next is 0
            mult = 0;
        } else {
            String name = node.getName();
            TreeElement parentNode = form.getMainInstance().resolveReference(nodeRef.getParentRef());
            mult = parentNode.getChildMultiplicity(name);
        }
        multiplicities.set(multiplicities.size() - 1, repeatStructure == REPEAT_STRUCTURE_NON_LINEAR ? TreeReference.INDEX_REPEAT_JUNCTURE : mult);
        return true;
    } else {
        return false;
    }
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) TreeReference(org.javarosa.core.model.instance.TreeReference) GroupDef(org.javarosa.core.model.GroupDef) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 24 with IFormElement

use of org.javarosa.core.model.IFormElement in project javarosa by opendatakit.

the class FormEntryModel method incrementHelper.

private void incrementHelper(List<Integer> indexes, List<Integer> multiplicities, List<IFormElement> elements, boolean descend) {
    int i = indexes.size() - 1;
    // if exiting a repetition? (i.e., go to next repetition instead of one level up)
    boolean exitRepeat = false;
    if (i == -1 || elements.get(i) instanceof GroupDef) {
        if (i >= 0) {
            // find out whether we're on a repeat, and if so, whether the
            // specified instance actually exists
            GroupDef group = (GroupDef) elements.get(i);
            if (group.getRepeat()) {
                if (repeatStructure == REPEAT_STRUCTURE_NON_LINEAR) {
                    if (multiplicities.get(multiplicities.size() - 1) == TreeReference.INDEX_REPEAT_JUNCTURE) {
                        descend = false;
                        exitRepeat = true;
                    }
                } else {
                    if (form.getMainInstance().resolveReference(form.getChildInstanceRef(elements, multiplicities)) == null) {
                        // repeat instance does not exist; do not descend into it
                        descend = false;
                        exitRepeat = true;
                    }
                }
            }
        }
        if (descend) {
            IFormElement ife = (i == -1) ? null : elements.get(i);
            if ((i == -1) || (ife != null && ife.getChildren() != null && ife.getChildren().size() > 0)) {
                indexes.add(0);
                multiplicities.add(0);
                elements.add((i == -1 ? form : elements.get(i)).getChild(0));
                if (repeatStructure == REPEAT_STRUCTURE_NON_LINEAR) {
                    if (elements.get(elements.size() - 1) instanceof GroupDef && ((GroupDef) elements.get(elements.size() - 1)).getRepeat()) {
                        multiplicities.set(multiplicities.size() - 1, TreeReference.INDEX_REPEAT_JUNCTURE);
                    }
                }
                return;
            }
        }
    }
    while (i >= 0) {
        // will be true)
        if (!exitRepeat && elements.get(i) instanceof GroupDef && ((GroupDef) elements.get(i)).getRepeat()) {
            if (repeatStructure == REPEAT_STRUCTURE_NON_LINEAR) {
                multiplicities.set(i, TreeReference.INDEX_REPEAT_JUNCTURE);
            } else {
                multiplicities.set(i, multiplicities.get(i) + 1);
            }
            return;
        }
        IFormElement parent = (i == 0 ? form : elements.get(i - 1));
        int curIndex = indexes.get(i);
        // increment to the next element on the current level
        if (curIndex + 1 >= parent.getChildren().size()) {
            // at the end of the current level; move up one level and start
            // over
            indexes.remove(i);
            multiplicities.remove(i);
            elements.remove(i);
            i--;
            exitRepeat = false;
        } else {
            indexes.set(i, curIndex + 1);
            multiplicities.set(i, 0);
            elements.set(i, parent.getChild(curIndex + 1));
            if (repeatStructure == REPEAT_STRUCTURE_NON_LINEAR) {
                if (elements.get(elements.size() - 1) instanceof GroupDef && ((GroupDef) elements.get(elements.size() - 1)).getRepeat()) {
                    multiplicities.set(multiplicities.size() - 1, TreeReference.INDEX_REPEAT_JUNCTURE);
                }
            }
            return;
        }
    }
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) GroupDef(org.javarosa.core.model.GroupDef)

Example 25 with IFormElement

use of org.javarosa.core.model.IFormElement in project javarosa by opendatakit.

the class OutputInComputedConstraintTextTest method buildIndexes.

private void buildIndexes() {
    ctrl.jumpToIndex(FormIndex.createBeginningOfFormIndex());
    do {
        FormEntryCaption fep = model.getCaptionPrompt();
        IFormElement formElement = fep.getFormElement();
        if (formElement instanceof QuestionDef)
            formIndexesById.put(formElement.getTextID(), fep.getIndex());
    } while (ctrl.stepToNextEvent() != FormEntryController.EVENT_END_OF_FORM);
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) QuestionDef(org.javarosa.core.model.QuestionDef)

Aggregations

IFormElement (org.javarosa.core.model.IFormElement)26 GroupDef (org.javarosa.core.model.GroupDef)19 QuestionDef (org.javarosa.core.model.QuestionDef)10 ArrayList (java.util.ArrayList)9 TreeReference (org.javarosa.core.model.instance.TreeReference)8 FormIndex (org.javarosa.core.model.FormIndex)6 TreeElement (org.javarosa.core.model.instance.TreeElement)6 FormEntryPrompt (org.javarosa.form.api.FormEntryPrompt)5 IDataReference (org.javarosa.core.model.IDataReference)4 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)3 XPathReference (org.javarosa.model.xform.XPathReference)3 Element (org.kxml2.kdom.Element)3 DataBinding (org.javarosa.core.model.DataBinding)2 Constraint (org.javarosa.core.model.condition.Constraint)2 IAnswerData (org.javarosa.core.model.data.IAnswerData)2 FormEntryModel (org.javarosa.form.api.FormEntryModel)2 Test (org.junit.Test)2 QuestionDetails (org.odk.collect.android.formentry.questions.QuestionDetails)2 MockFormEntryPromptBuilder (org.odk.collect.android.support.MockFormEntryPromptBuilder)2 Activity (android.app.Activity)1