Search in sources :

Example 16 with GroupDef

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

the class FormEntryCaption method getRepetitionsText.

public List<String> getRepetitionsText() {
    GroupDef g = (GroupDef) element;
    if (!g.getRepeat()) {
        throw new RuntimeException("not a repeat");
    }
    int numRepetitions = getNumRepetitions();
    List<String> reps = new ArrayList<String>(numRepetitions);
    for (int i = 0; i < numRepetitions; i++) {
        reps.add(getRepetitionText("choose", form.descendIntoRepeat(index, i), false));
    }
    return reps;
}
Also used : ArrayList(java.util.ArrayList) GroupDef(org.javarosa.core.model.GroupDef)

Example 17 with GroupDef

use of org.javarosa.core.model.GroupDef 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 18 with GroupDef

use of org.javarosa.core.model.GroupDef 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 19 with GroupDef

use of org.javarosa.core.model.GroupDef 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 20 with GroupDef

use of org.javarosa.core.model.GroupDef 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)

Aggregations

GroupDef (org.javarosa.core.model.GroupDef)22 IFormElement (org.javarosa.core.model.IFormElement)15 ArrayList (java.util.ArrayList)6 FormIndex (org.javarosa.core.model.FormIndex)6 QuestionDef (org.javarosa.core.model.QuestionDef)5 TreeReference (org.javarosa.core.model.instance.TreeReference)5 TreeElement (org.javarosa.core.model.instance.TreeElement)4 IDataReference (org.javarosa.core.model.IDataReference)3 FormEntryCaption (org.javarosa.form.api.FormEntryCaption)3 HashMap (java.util.HashMap)2 FormDef (org.javarosa.core.model.FormDef)2 Constraint (org.javarosa.core.model.condition.Constraint)2 FormEntryPrompt (org.javarosa.form.api.FormEntryPrompt)2 XPathReference (org.javarosa.model.xform.XPathReference)2 DataBinding (org.javarosa.core.model.DataBinding)1 SelectChoice (org.javarosa.core.model.SelectChoice)1 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)1 IAnswerData (org.javarosa.core.model.data.IAnswerData)1 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)1 InvalidReferenceException (org.javarosa.core.model.instance.InvalidReferenceException)1