Search in sources :

Example 6 with DataInstance

use of org.javarosa.core.model.instance.DataInstance in project javarosa by opendatakit.

the class FormDef method populateDynamicChoices.

/**
 * Identify the itemset in the backend model, and create a set of
 * SelectChoice objects at the current question reference based on the data
 * in the model.
 * <p/>
 * Will modify the itemset binding to contain the relevant choices
 *
 * @param itemset The binding for an itemset, where the choices will be populated
 * @param curQRef A reference to the current question's element, which will be
 *                used to determine the values to be chosen from.
 */
public void populateDynamicChoices(ItemsetBinding itemset, TreeReference curQRef) {
    getEventNotifier().publishEvent(new Event("Dynamic choices", new EvaluationResult(curQRef, null)));
    List<SelectChoice> choices = new ArrayList<SelectChoice>();
    List<TreeReference> matches = itemset.nodesetExpr.evalNodeset(this.getMainInstance(), new EvaluationContext(exprEvalContext, itemset.contextRef.contextualize(curQRef)));
    DataInstance fi = null;
    if (// We're not dealing
    itemset.nodesetRef.getInstanceName() != null) // with the default
    // instance
    {
        fi = getNonMainInstance(itemset.nodesetRef.getInstanceName());
        if (fi == null) {
            throw new XPathException("Instance " + itemset.nodesetRef.getInstanceName() + " not found");
        }
    } else {
        fi = getMainInstance();
    }
    if (matches == null) {
        throw new XPathException("Could not find references depended on by" + itemset.nodesetRef.getInstanceName());
    }
    for (int i = 0; i < matches.size(); i++) {
        TreeReference item = matches.get(i);
        // String label =
        // itemset.labelExpr.evalReadable(this.getMainInstance(), new
        // EvaluationContext(exprEvalContext, item));
        String label = itemset.labelExpr.evalReadable(fi, new EvaluationContext(exprEvalContext, item));
        String value = null;
        TreeElement copyNode = null;
        if (itemset.copyMode) {
            copyNode = this.getMainInstance().resolveReference(itemset.copyRef.contextualize(item));
        }
        if (itemset.valueRef != null) {
            // value = itemset.valueExpr.evalReadable(this.getMainInstance(),
            // new EvaluationContext(exprEvalContext, item));
            value = itemset.valueExpr.evalReadable(fi, new EvaluationContext(exprEvalContext, item));
        }
        // SelectChoice choice = new
        // SelectChoice(labelID,labelInnerText,value,isLocalizable);
        SelectChoice choice = new SelectChoice(label, value != null ? value : "dynamic:" + i, itemset.labelIsItext);
        choice.setIndex(i);
        if (itemset.copyMode)
            choice.copyNode = copyNode;
        choices.add(choice);
    }
    if (choices.size() == 0) {
        // throw new
        // RuntimeException("dynamic select question has no choices! [" +
        // itemset.nodesetRef + "]");
        // When you exit a survey mid way through and want to save it, it seems
        // that Collect wants to
        // go through all the questions. Well of course not all the questions
        // are going to have answers
        // to chose from if the user hasn't filled them out. So I'm just going
        // to make a note of this
        // and not throw an exception.
        Std.out.println("Dynamic select question has no choices! [" + itemset.nodesetRef + "]. If this occurs while filling out a form (and not while saving an incomplete form), the filter condition may have eliminated all the choices. Is that what you intended?\n");
    }
    itemset.clearChoices();
    itemset.setChoices(choices, this.getLocalizer());
}
Also used : DataInstance(org.javarosa.core.model.instance.DataInstance) XPathException(org.javarosa.xpath.XPathException) ArrayList(java.util.ArrayList) EvaluationResult(org.javarosa.debug.EvaluationResult) Constraint(org.javarosa.core.model.condition.Constraint) TreeElement(org.javarosa.core.model.instance.TreeElement) TreeReference(org.javarosa.core.model.instance.TreeReference) Event(org.javarosa.debug.Event) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext)

Example 7 with DataInstance

use of org.javarosa.core.model.instance.DataInstance in project javarosa by opendatakit.

the class XFormParserTest method parsesExternalSecondaryInstanceForm.

@Test
public void parsesExternalSecondaryInstanceForm() throws IOException, XPathSyntaxException {
    FormDef formDef = parse(EXTERNAL_SECONDARY_INSTANCE_XML).formDef;
    assertEquals("Form with external secondary instance", formDef.getTitle());
    TreeReference treeReference = ((XPathPathExpr) parseXPath("instance('towns')/data_set")).getReference();
    EvaluationContext evaluationContext = formDef.getEvaluationContext();
    List<TreeReference> treeReferences = evaluationContext.expandReference(treeReference);
    assertEquals(1, treeReferences.size());
    DataInstance townInstance = formDef.getNonMainInstance("towns");
    AbstractTreeElement tiRoot = townInstance.getRoot();
    assertEquals("towndata", tiRoot.getName());
    assertEquals(1, tiRoot.getNumChildren());
    AbstractTreeElement dataSetChild = tiRoot.getChild("data_set", 0);
    assertEquals("us_east", dataSetChild.getValue().getDisplayText());
}
Also used : AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) DataInstance(org.javarosa.core.model.instance.DataInstance) FormDef(org.javarosa.core.model.FormDef) TreeReference(org.javarosa.core.model.instance.TreeReference) XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) Test(org.junit.Test)

Aggregations

DataInstance (org.javarosa.core.model.instance.DataInstance)7 TreeReference (org.javarosa.core.model.instance.TreeReference)4 ArrayList (java.util.ArrayList)3 FormDef (org.javarosa.core.model.FormDef)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)2 TreeElement (org.javarosa.core.model.instance.TreeElement)2 XPathException (org.javarosa.xpath.XPathException)2 IOException (java.io.IOException)1 List (java.util.List)1 IFormElement (org.javarosa.core.model.IFormElement)1 Condition (org.javarosa.core.model.condition.Condition)1 Constraint (org.javarosa.core.model.condition.Constraint)1 IConditionExpr (org.javarosa.core.model.condition.IConditionExpr)1 Recalculate (org.javarosa.core.model.condition.Recalculate)1 ExternalDataInstance (org.javarosa.core.model.instance.ExternalDataInstance)1 ExternalDataInstance.getPathIfExternalDataInstance (org.javarosa.core.model.instance.ExternalDataInstance.getPathIfExternalDataInstance)1 FormInstance (org.javarosa.core.model.instance.FormInstance)1 Localizer (org.javarosa.core.services.locale.Localizer)1 CodeTimer (org.javarosa.core.util.CodeTimer)1