Search in sources :

Example 1 with GroupDef

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

the class FormController method getQuestionPrompts.

/**
 * Returns an array of question promps.
 */
public FormEntryPrompt[] getQuestionPrompts() throws RuntimeException {
    // For questions, there is only one.
    // For groups, there could be many, but we set that below
    FormEntryPrompt[] questions = new FormEntryPrompt[1];
    IFormElement element = formEntryController.getModel().getForm().getChild(getFormIndex());
    if (element instanceof GroupDef) {
        GroupDef gd = (GroupDef) element;
        // we only display relevant questions
        List<FormEntryPrompt> questionList = new ArrayList<>();
        for (FormIndex index : getIndicesForGroup(gd)) {
            if (getEvent(index) != FormEntryController.EVENT_QUESTION) {
                String errorMsg = "Only questions and regular groups are allowed in 'field-list'.  Bad node is: " + index.getReference().toString(false);
                RuntimeException e = new RuntimeException(errorMsg);
                Timber.w(errorMsg);
                throw e;
            }
            // we only display relevant questions
            if (formEntryController.getModel().isIndexRelevant(index)) {
                questionList.add(getQuestionPrompt(index));
            }
            questions = new FormEntryPrompt[questionList.size()];
            questionList.toArray(questions);
        }
    } else {
        // We have a question, so just get the one prompt
        questions[0] = getQuestionPrompt();
    }
    return questions;
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) FormEntryPrompt(org.javarosa.form.api.FormEntryPrompt) ArrayList(java.util.ArrayList) FormIndex(org.javarosa.core.model.FormIndex) GroupDef(org.javarosa.core.model.GroupDef)

Example 2 with GroupDef

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

the class FormController method groupIsFieldList.

/**
 * A convenience method for determining if the current FormIndex is in a group that is/should
 * be
 * displayed as a multi-question view. This is useful for returning from the formhierarchy view
 * to a selected index.
 */
private boolean groupIsFieldList(FormIndex index) {
    // if this isn't a group, return right away
    IFormElement element = formEntryController.getModel().getForm().getChild(index);
    if (!(element instanceof GroupDef)) {
        return false;
    }
    // exceptions?
    GroupDef gd = (GroupDef) element;
    return (ODKView.FIELD_LIST.equalsIgnoreCase(gd.getAppearanceAttr()));
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) GroupDef(org.javarosa.core.model.GroupDef)

Example 3 with GroupDef

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

the class FormDefConstructionUtils method createSimpleGroupReference.

public static FormDef createSimpleGroupReference() {
    FormDef theform = new FormDef();
    QuestionDef question1 = new QuestionDef();
    GroupDef group1 = new GroupDef();
    QuestionDef question11 = new QuestionDef();
    QuestionDef question12 = new QuestionDef();
    group1.addChild(question11);
    group1.addChild(question12);
    QuestionDef question2 = new QuestionDef();
    theform.addChild(question1);
    theform.addChild(group1);
    theform.addChild(question2);
    return theform;
}
Also used : FormDef(org.javarosa.core.model.FormDef) QuestionDef(org.javarosa.core.model.QuestionDef) GroupDef(org.javarosa.core.model.GroupDef)

Example 4 with GroupDef

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

the class FormInstanceParser method verifyRepeatMemberBindings.

private void verifyRepeatMemberBindings(IFormElement fe, FormInstance instance, GroupDef parentRepeat) {
    if (fe.getChildren() == null)
        return;
    for (int i = 0; i < fe.getChildren().size(); i++) {
        IFormElement child = fe.getChildren().get(i);
        boolean isRepeat = (child instanceof GroupDef && ((GroupDef) child).getRepeat());
        // get bindings of current node and nearest enclosing repeat
        TreeReference repeatBind = (parentRepeat == null ? TreeReference.rootRef() : FormInstance.unpackReference(parentRepeat.getBind()));
        TreeReference childBind = FormInstance.unpackReference(child.getBind());
        // check if current binding is within scope of repeat binding
        if (!repeatBind.isParentOf(childBind, false)) {
            // catch <repeat nodeset="/a/b"><input ref="/a/c" /></repeat>: repeat question is not a child of the repeated node
            throw new XFormParseException("<repeat> member's binding [" + childBind.toString() + "] is not a descendant of <repeat> binding [" + repeatBind.toString() + "]!");
        } else if (repeatBind.equals(childBind) && isRepeat) {
            // catch <repeat nodeset="/a/b"><repeat nodeset="/a/b">...</repeat></repeat> (<repeat nodeset="/a/b"><input ref="/a/b" /></repeat> is ok)
            throw new XFormParseException("child <repeat>s [" + childBind.toString() + "] cannot bind to the same node as their parent <repeat>; only questions/groups can");
        }
        // check that, in the instance, current node is not within the scope of any closer repeat binding
        // build a list of all the node's instance ancestors
        List<TreeElement> repeatAncestry = new ArrayList<>();
        TreeElement repeatNode = (repeatTree == null ? null : repeatTree.getRoot());
        if (repeatNode != null) {
            repeatAncestry.add(repeatNode);
            for (int j = 1; j < childBind.size(); j++) {
                repeatNode = repeatNode.getChild(childBind.getName(j), 0);
                if (repeatNode != null) {
                    repeatAncestry.add(repeatNode);
                } else {
                    break;
                }
            }
        }
        // check that no nodes between the parent repeat and the target are repeatable
        for (int k = repeatBind.size(); k < childBind.size(); k++) {
            TreeElement rChild = (k < repeatAncestry.size() ? repeatAncestry.get(k) : null);
            boolean repeatable = rChild != null && rChild.isRepeatable();
            if (repeatable && !(k == childBind.size() - 1 && isRepeat)) {
                // question's/group's/repeat's most immediate repeat parent in the instance is not its most immediate repeat parent in the form def
                throw new XFormParseException("<repeat> member's binding [" + childBind.toString() + "] is within the scope of a <repeat> that is not its closest containing <repeat>!");
            }
        }
        verifyRepeatMemberBindings(child, instance, (isRepeat ? (GroupDef) child : parentRepeat));
    }
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) TreeReference(org.javarosa.core.model.instance.TreeReference) ArrayList(java.util.ArrayList) Constraint(org.javarosa.core.model.condition.Constraint) GroupDef(org.javarosa.core.model.GroupDef) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 5 with GroupDef

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

the class FormEntryCaption method getRepetitionText.

private String getRepetitionText(String type, FormIndex index, boolean newrep) {
    if (element instanceof GroupDef && ((GroupDef) element).getRepeat() && index.getElementMultiplicity() >= 0) {
        GroupDef g = (GroupDef) element;
        String title = getLongText();
        int ix = index.getElementMultiplicity() + 1;
        int count = getNumRepetitions();
        String caption = null;
        if ("header".equals(type)) {
            caption = g.entryHeader;
        } else if ("choose".equals(type)) {
            caption = g.chooseCaption;
            if (caption == null) {
                caption = g.entryHeader;
            }
        }
        if (caption == null) {
            return title + " " + ix + "/" + count;
        }
        HashMap<String, Object> vars = new HashMap<String, Object>();
        vars.put("name", title);
        vars.put("i", Integer.valueOf(ix));
        vars.put("n", Integer.valueOf(count));
        vars.put("new", new Boolean(newrep));
        return form.fillTemplateString(caption, index.getReference(), vars);
    } else {
        return null;
    }
}
Also used : HashMap(java.util.HashMap) 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